1. 镜像相关操作
#从dockerhub查找镜像
[root@master1 ~]# docker search centos
#解释说明:
NAME: 镜像仓库源的名称
DESCRIPTION: 镜像的描述
OFFICIAL: 是否 docker 官方发布
STARS: 类似 Github 里面的 SATAR,表示点赞、喜欢的意思。
#下载镜像
[root@master1 ~]# docker pull centos
#查看本地镜像
[root@master1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 2 years ago 231MB
#把镜像做成离线压缩包
[root@master1 ~]# docker save -o cent.tar.gz centos
[root@master1 ~]# ls
anaconda-ks.cfg cent.tar.gz original-ks.cfg
#解压离线镜像包
[root@master1 ~]# docker load -i cent.tar.gz
Loaded image: centos:latest
#删除镜像
[root@master1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 2 years ago 231MB
#删除镜像
[root@master1 ~]# docker rmi -f centos:latest
Untagged: centos:latest
Untagged: centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6
Deleted: sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59
You have new mail in /var/spool/mail/root
[root@master1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
2.容器相关操作
#以交互式方式启动并进入容器
[root@master1 ~]# docker run --name=hello -it centos /bin/bash
Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
[root@97a65bc56535 /]#
ps:检测到当前没有系统没有centos镜像,系统将在线拉取cenos镜像。
输入exit,退出容器,退出之后容器也会停止,不会再前台运行
#docker run运行并创建容器
--name 容器的名字
-i 交互式
-t 分配伪终端
centos: 启动docker需要的镜像
/bin/bash说明你的shell类型为bash,bash shell是最常用的一种shell, 是大多数Linux发行版默认的shell。 此外还有C shell等其它shell。
#以守护进程方式启动容器
[root@master1 ~]# docker run --name=xiaomuzai -td centos
2109b76f1294e808555946ace3c9304e2e455caae84096464615f2e3881584a2
[root@master1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2109b76f1294 centos "/bin/bash" 20 seconds ago Up 20 seconds xiaomuzai
-d在后台运行docker
[root@master1 ~]# docker exec -it xiaomuzai /bin/bash
[root@2109b76f1294 /]#
#查看正在运行的容器
[root@master1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2109b76f1294 centos "/bin/bash" 20 seconds ago Up 20 seconds xiaomuzai
[root@master1 ~]# docker ps -a #查看所有容器,包括运行和退出的容器
[root@master1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2109b76f1294 centos "/bin/bash" 3 minutes ago Up 3 minutes xiaomuzai
708c5da41ae3 centos "/bin/bash" 14 minutes ago Exited (127) 14 minutes ago hello2
901bf27c3468 centos "/bin/bash" 15 minutes ago Exited (0) 2 minutes ago nihao
97a65bc56535 centos "/bin/bash" 21 minutes ago Exited (0) 18 minutes ago hello
# 停止容器
[root@master1 ~]# docker stop xiaomuzai
#启动已经停止的容器
[root@master1 ~]# docker start xiaomuzai
# 进入容器
[root@master1 ~]# docker exec -it xiaomuzai /bin/sh
sh-4.4#
#删除容器
[root@master1 ~]# docker rm -f hello2
#查看docker帮助命令
[root@master1 ~]# docker --help