0


Docker常用命令总结

  • 基础命令

1.启动docker

systemctl start docker

2.关闭docker

systemctl stop docker

3.设置docker为自启动

systemctl enable --now docker

4.重启docker

systemctl restart docker

3.查看docker版本信息

docker version

4.查看docker详细信息

docker info

Client:
Debug Mode: false            #client端是否开启 debug

Server:
Containers: 2                #当前主机运行的容器总数

Running: 0                   #有几个容器是正在运行的

Paused: 0                    #有几个容器是暂停的

Stopped: 2                   #有几个容器是停止的

Images: 4                    #当前服务器的镜像数

Server Version: 19.03.5      #服务端版本

Storage Driver: overlay2     #正在使用的存储引擎

Backing Filesystem: extfs    #后端文件系统,即服务器的磁盘文件系统

Supports d_type: true        #是否支持 d_type

Native Overlay Diff: true    #是否支持差异数据存储

Logging Driver: json-file    
#日志类型,每个容器的标准输出以日志存放在
/var/lib/docker/containers/<CONTAINERID>/<CONTAINER ID>-json.log
                              
Cgroup Driver: cgroupfs      #Cgroups 类型

Plugins:                     #插件

Volume: local                #卷

Network: bridge host ipvlan macvlan null overlay   #overlay 跨主机通信

Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog                                             #日志类型

Swarm: inactive              #是否支持 swarm

Runtimes: runc               #已安装的容器运行时

Default Runtime: runc        #默认使用的容器运行时

Init Binary: docker-init     #初始化容器的守护进程,即 pid 为 1 的进程

containerd version: b34a5c8af56e510852c35414db4c1f4fa6172339 #版本

runc version: 3e425f80a8c931f88e6d94a8c831b9d5aa481657 #runc版本

init version: fec3683        #init 版本

Security Options:            #安全选项

apparmor #安全模块,https://docs.docker.com/engine/security/apparmor/

seccomp #安全计算模块,即制容器操作,https://docs.docker.com/engine/security/seccomp/

Profile: default             #默认的配置文件

Kernel Version: 4.15.0-29-generic #宿主机内核版本

Operating System: Ubuntu 18.04.1 LTS #宿主机操作系统

OSType: linux #宿主机操作系统类型

Architecture: x86_64 #宿主机架构

CPUs: 1 #宿主机CPU数量

Total Memory: 962MiB #宿主机总内存

Name: ubuntu1804.wang.org #宿主机hostname

ID: IZHJ:WPIN:BRMC:XQUI:VVVR:UVGK:NZBM:YQXT:JDWB:33RS:45V7:SQWJ #宿主机ID

Docker Root Dir: /var/lib/docker 
#宿主机关于docker数据的保存目录,建议使用独立SSD的磁
盘,保证性能和空间

Debug Mode: false #server端是否开启debug

Registry: https://index.docker.io/v1/ #仓库路径

Labels:
Experimental: false #是否测试版

Insecure Registries:
127.0.0.0/8 : #非安全的镜像仓库

Registry Mirrors:
https://si7y70hh.mirror.aliyuncs.com/ #镜像仓库

Live Restore Enabled: false #是否开启活动重启 (重启docker-daemon 不关闭容器 )

WARNING: No swap limit support #系统警告信息 (没有开启 swap 资源限制 )

#解决上述SWAP报警提示:

1.修改配置文件

vim /etc/default/grub

----省略---

GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0 swapaccount=1" #修改此行

---省略---

2.生效修改后的grub

#Ubuntu执行

update-grub

#Centos执行

grub2-mkconfig -o /boot/grub2/grub.cfg

3.重启系统

reboot

5.查看docker命令的帮助

docker --help

6.查看docker子命令的帮助

#比如忘记docker pull的选项和可带的参数

docker pull --help

  • 镜像命令

1.拉取镜像

#加:tag(版本号)则是拉取指定版本

#不加tag(版本号)即拉取docker仓库中 该镜像的最新版本latest

格式:docker pull [镜像名]

格式:docker pull [镜像名:tag]

#拉取最新版本
[root@centos7-temp ~]# docker pull nginx
Using default tag: latest #默认下载最新版本
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete #分层下载
a9edb18cadd1: Pull complete 
589b7251471a: Pull complete 
186b1aaa4aa6: Pull complete 
b4df32aa5a72: Pull complete 
a0bcbecc962e: Pull complete 
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31 #摘要
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest #下载的完整地址

#拉取指定版本
[root@centos7-temp ~]# docker pull nginx:1.18.0
1.18.0: Pulling from library/nginx
f7ec5a41d630: Pull complete 
0b20d28b5eb3: Pull complete 
1576642c9776: Pull complete 
c12a848bad84: Pull complete 
03f221d9cf00: Pull complete 
Digest: sha256:e90ac5331fe095cea01b121a3627174b2e33e06e83720e9a934c7b8ccc9c55a0
Status: Downloaded newer image for nginx:1.18.0
docker.io/library/nginx:1.18.0

#查看本地镜像列表
[root@centos7-temp ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              605c77e624dd        12 months ago       141MB
nginx               1.18.0              c2c45d506085        21 months ago       133MB

2.查看镜像分层历史

格式:docker image history [镜像名]

[root@centos7-temp ~]# docker image history nginx
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
605c77e624dd        12 months ago       /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B                  
<missing>           12 months ago       /bin/sh -c #(nop)  STOPSIGNAL SIGQUIT           0B                  
<missing>           12 months ago       /bin/sh -c #(nop)  EXPOSE 80                    0B                  
<missing>           12 months ago       /bin/sh -c #(nop)  ENTRYPOINT ["/docker-entr…   0B                  
<missing>           12 months ago       /bin/sh -c #(nop) COPY file:09a214a3e07c919a…   4.61kB              
<missing>           12 months ago       /bin/sh -c #(nop) COPY file:0fd5fca330dcd6a7…   1.04kB              
<missing>           12 months ago       /bin/sh -c #(nop) COPY file:0b866ff3fc1ef5b0…   1.96kB              
<missing>           12 months ago       /bin/sh -c #(nop) COPY file:65504f71f5855ca0…   1.2kB               
<missing>           12 months ago       /bin/sh -c set -x     && addgroup --system -…   61.1MB              
<missing>           12 months ago       /bin/sh -c #(nop)  ENV PKG_RELEASE=1~bullseye   0B                  
<missing>           12 months ago       /bin/sh -c #(nop)  ENV NJS_VERSION=0.7.1        0B                  
<missing>           12 months ago       /bin/sh -c #(nop)  ENV NGINX_VERSION=1.21.5     0B                  
<missing>           12 months ago       /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B                  
<missing>           12 months ago       /bin/sh -c #(nop)  CMD ["bash"]                 0B                  
<missing>           12 months ago       /bin/sh -c #(nop) ADD file:09675d11695f65c55…   80.4MB 

3.查看镜像的元数据(后面加容器,也可查看容器的元数据)

#以json格式显示

格式:docker inspect [镜像/容器]

#查看nginx的镜像元数据
[root@centos7-temp ~]# docker inspect nginx

4.搜索镜像

格式:docker search [镜像名]

#搜索STARS>9000的mysql镜像
[root@centos7-temp ~]# docker search --filter=STARS=9000 mysql
NAME                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql               MySQL is a widely used, open-source relation…   13639               [OK]          

5.查看本地镜像列表

docker images

#查看本地镜像列表
[root@centos7-temp ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        latest    605c77e624dd   12 months ago   141MB
nginx        1.18.0    c2c45d506085   21 months ago   133MB

##显示完整的ImageID
[root@centos7-temp ~]# docker images --no-trunc
REPOSITORY   TAG       IMAGE ID                                                      CREATED         SIZE
nginx        latest    sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85   12 months ago   141MB
nginx        1.18.0    sha256:c2c45d506085d300b72a6d4b10e3dce104228080a2cf095fc38333afe237e2be   21 months ago   133MB

#显示结果信息说明:

REPOSITORY      #镜像所属的仓库名称
TAG             #镜像版本号(标识符),默认为latest
IMAGE ID        #镜像唯一ID标识,如果ID相同,说明是同一个镜像有多个名称
CREATED         #镜像在仓库中被创建时间
VIRTUAL SIZE    #镜像的大小

5.导出和导入镜像

(1)导出镜像

#利用docker save命令可以将从本地镜像导出为一个打包 tar文件,然后复制到其它主机进行导入使用

格式:docker save 镜像名 -o tar文件路径

选项:-o:将输出字符串写入文件

用法:

#查看本地镜像列表
[root@docker1 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        latest    605c77e624dd   12 months ago   141MB
nginx        1.18.0    c2c45d506085   21 months ago   133MB
[root@docker1 ~]# docker save nginx:1.18.0 -o /data/nginx.tar

#打包镜像为tar文件
[root@docker1 ~]# docker save nginx:1.18.0 -o /data/nginx.tar

#查看tar文件
[root@docker1 ~]# ls /data/nginx.tar 
/data/nginx.tar

#上传至另一台主机上
[root@docker1 ~]# scp /data/nginx.tar 10.0.0.100:/data
The authenticity of host '10.0.0.100 (10.0.0.100)' can't be established.
ECDSA key fingerprint is SHA256:ZyQrEY2+tXpQpgIXx8tA8ZSzHLXmhChf2aQrbEtX+Ls.
ECDSA key fingerprint is MD5:1b:f1:e3:71:ff:8c:66:c8:39:63:e0:47:82:df:15:cc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.100' (ECDSA) to the list of known hosts.
[email protected]'s password: 
nginx.tar                                                                                  100%  131MB  78.4MB/s   00:01

#打包所有镜像的方法:

#打包所有镜像至不同文件
[root@centos7-temp ~]# docker images | awk 'NR!=1{print $1,$2}' | while read repo tag\
> ;do docker save $repo:$tag -o /opt/$repo-$tag.tar ;done

#打包所有镜像至同一个文件
方法1: 使用image ID导出镜像,在导入后的镜像没有REPOSITORY和TAG,显示为<none>
[root@centos7-temp ~]#docker save `docker images -qa` -o all.tar
[root@centos7-temp ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              605c77e624dd        12 months ago       141MB
<none>              <none>              3218b38490ce        12 months ago       516MB

方法2:将所有镜像导入到一个文件中,此方法导入后可以看REPOSITORY和TAG
[root@centos7-temp ~]#docker save `docker images | awk 'NR!=1{print $1":"$2}'` -o all.tar
[root@centos7-temp ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              605c77e624dd        12 months ago       141MB
mysql               latest              3218b38490ce        12 months ago       516MB

方法3:将所有镜像导入到一个文件中,此方法导入后可以看REPOSITORY和TAG
[root@centos7-temp ~]#docker image save `docker image ls --format "{{.Repository}}:{{.Tag}}"` -o all.tar
[root@centos7-temp ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              605c77e624dd        12 months ago       141MB
mysql               latest              3218b38490ce        12 months ago       516MB

(2)导入镜像

#主机ip:10.0.0.100

#利用docker load命令可以将镜像导出的打包或压缩文件再导入

格式:docker load -i tar文件路径

选项:-i:输入字符串从tar存档文件读取

用法:

#查看本地镜像列表
[root@centos7-temp ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

#导入镜像
[root@centos7-temp ~]# docker load -i /data/nginx.tar 
7e718b9c0c8c: Loading layer [==================================================>]  72.52MB/72.52MB
f3fdf88f1cb7: Loading layer [==================================================>]   64.6MB/64.6MB
6fcbf7acaafd: Loading layer [==================================================>]  3.072kB/3.072kB
4fe7d87c8e14: Loading layer [==================================================>]  4.096kB/4.096kB
4fa6704c8474: Loading layer [==================================================>]  3.584kB/3.584kB
Loaded image: nginx:1.18.0

#再次查看本地镜像列表
[root@centos7-temp ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               1.18.0              c2c45d506085        21 months ago       133MB

(3)一次导出多个镜像,然后导入

#查看本地镜像列表
[root@centos7-temp ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        latest    605c77e624dd   12 months ago   141MB
nginx        1.18.0    c2c45d506085   21 months ago   133MB

#打包两个不同版本的nginx镜像
[root@centos7-temp ~]# docker save nginx > /all.tar

#查看tar文件
[root@centos7-temp ~]# ll -h /all.tar 
-rw-r--r-- 1 root root 270M 1月   1 16:54 /all.tar

#删除原来的两个镜像
[root@centos7-temp ~]# docker rmi -f `docker images -q`
Untagged: nginx:latest
Untagged: nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
Untagged: nginx:1.18.0
Untagged: nginx@sha256:e90ac5331fe095cea01b121a3627174b2e33e06e83720e9a934c7b8ccc9c55a0
Deleted: sha256:c2c45d506085d300b72a6d4b10e3dce104228080a2cf095fc38333afe237e2be
Deleted: sha256:43d6c481a041dbcc1d8ea9c565b1b692bcb28da3414683c316703c669c012ebc
Deleted: sha256:defebc732c194dd0b5b39e20c4d014896ce120207f5dfdb41ed6696b0e8224d6
Deleted: sha256:4ea0f2550407442f808812429981c0b62d8dd6a531db8a412640293a1faf8f3c
Deleted: sha256:778ca58cf39b8fa0776ade88562750a035a24ec5afb7dc4ab2aa892b2c09769d
Deleted: sha256:7e718b9c0c8c2e6420fe9c4d1d551088e314fe923dce4b2caf75891d82fb227d

#再次查看本地镜像列表
[root@centos7-temp ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

#导入镜像
[root@centos7-temp ~]# docker load -i /all.tar
7e718b9c0c8c: Loading layer [==================================================>]  72.52MB/72.52MB
f3fdf88f1cb7: Loading layer [==================================================>]   64.6MB/64.6MB
6fcbf7acaafd: Loading layer [==================================================>]  3.072kB/3.072kB
4fe7d87c8e14: Loading layer [==================================================>]  4.096kB/4.096kB
4fa6704c8474: Loading layer [==================================================>]  3.584kB/3.584kB
Loaded image: nginx:1.18.0
2edcec3590a4: Loading layer [==================================================>]  83.86MB/83.86MB
e379e8aedd4d: Loading layer [==================================================>]     62MB/62MB
b8d6e692a25e: Loading layer [==================================================>]  3.072kB/3.072kB
f1db227348d0: Loading layer [==================================================>]  4.096kB/4.096kB
32ce5f6a5106: Loading layer [==================================================>]  3.584kB/3.584kB
d874fd2bc83b: Loading layer [==================================================>]  7.168kB/7.168kB
Loaded image: nginx:latest

#再次查看本地镜像列表
[root@centos7-temp ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        latest    605c77e624dd   12 months ago   141MB
nginx        1.18.0    c2c45d506085   21 months ago   133MB

6.删除镜像

格式:docker rmi [选项] [镜像名]

选项:-f:强制删除

用法:

#删除指定的镜像
[root@centos7-temp ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              605c77e624dd        12 months ago       141MB
mysql               latest              3218b38490ce        12 months ago       516MB
[root@centos7-temp ~]# docker rmi -f nginx
Untagged: nginx:latest
Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
[root@centos7-temp ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              3218b38490ce        12 months ago       516MB

#删除多个镜像
[root@centos7-temp ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              605c77e624dd        12 months ago       141MB
mysql               latest              3218b38490ce        12 months ago       516MB
[root@centos7-temp ~]# docker rmi nginx mysql
Untagged: nginx:latest
Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
Untagged: mysql:latest
Deleted: sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b
Deleted: sha256:aa81ca46575069829fe1b3c654d9e8feb43b4373932159fe2cad1ac13524a2f5
Deleted: sha256:0558823b9fbe967ea6d7174999be3cc9250b3423036370dc1a6888168cbd224d
Deleted: sha256:a46013db1d31231a0e1bac7eeda5ad4786dea0b1773927b45f92ea352a6d7ff9
Deleted: sha256:af161a47bb22852e9e3caf39f1dcd590b64bb8fae54315f9c2e7dc35b025e4e3
Deleted: sha256:feff1495e6982a7e91edc59b96ea74fd80e03674d92c7ec8a502b417268822ff
Deleted: sha256:8805862fcb6ef9deb32d4218e9e6377f35fb351a8be7abafdf1da358b2b287ba
Deleted: sha256:872d2f24c4c64a6795e86958fde075a273c35c82815f0a5025cce41edfef50c7
Deleted: sha256:6fdb3143b79e1be7181d32748dd9d4a845056dfe16ee4c827410e0edef5ad3da
Deleted: sha256:b0527c827c82a8f8f37f706fcb86c420819bb7d707a8de7b664b9ca491c96838
Deleted: sha256:75147f61f29796d6528486d8b1f9fb5d122709ea35620f8ffcea0e0ad2ab0cd0
Deleted: sha256:2938c71ddf01643685879bf182b626f0a53b1356138ef73c40496182e84548aa
Deleted: sha256:ad6b69b549193f81b039a1d478bc896f6e460c77c1849a4374ab95f9a3d2cea2
[root@centos7-temp ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

#删除所有的镜像
[root@centos7-temp ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
<none>       <none>    605c77e624dd   12 months ago   141MB
mysql        latest    3218b38490ce   12 months ago   516MB
[root@centos7-temp ~]# docker rmi -f `docker images -q`
Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
Untagged: mysql:latest
Deleted: sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b
Deleted: sha256:aa81ca46575069829fe1b3c654d9e8feb43b4373932159fe2cad1ac13524a2f5
Deleted: sha256:0558823b9fbe967ea6d7174999be3cc9250b3423036370dc1a6888168cbd224d
Deleted: sha256:a46013db1d31231a0e1bac7eeda5ad4786dea0b1773927b45f92ea352a6d7ff9
Deleted: sha256:af161a47bb22852e9e3caf39f1dcd590b64bb8fae54315f9c2e7dc35b025e4e3
Deleted: sha256:feff1495e6982a7e91edc59b96ea74fd80e03674d92c7ec8a502b417268822ff
Deleted: sha256:8805862fcb6ef9deb32d4218e9e6377f35fb351a8be7abafdf1da358b2b287ba
Deleted: sha256:872d2f24c4c64a6795e86958fde075a273c35c82815f0a5025cce41edfef50c7
Deleted: sha256:6fdb3143b79e1be7181d32748dd9d4a845056dfe16ee4c827410e0edef5ad3da
Deleted: sha256:b0527c827c82a8f8f37f706fcb86c420819bb7d707a8de7b664b9ca491c96838
Deleted: sha256:75147f61f29796d6528486d8b1f9fb5d122709ea35620f8ffcea0e0ad2ab0cd0
Deleted: sha256:2938c71ddf01643685879bf182b626f0a53b1356138ef73c40496182e84548aa
Deleted: sha256:ad6b69b549193f81b039a1d478bc896f6e460c77c1849a4374ab95f9a3d2cea2
[root@centos7-temp ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

7.镜像打标签

格式:docker tag 旧TAG 新TAG

用法:

  • 容器命令

1.启动容器

注意: 容器启动后,如果容器内没有前台运行的进程,将自动退出停止

格式:docker run [选项] [镜像名] [shell命令] [参数]

选项:

--name:指定容器名称

-it:生成一个交互式的终端

-h:指定容器的主机名

--rm:退出时自动删除容器

-d:后台运行容器

--restart:定义容器的重启策略

-P:随机端口映射

-p:指定端口映射

#启动容器的流程:

用法:

#运行第一个容器
[root@centos7-temp ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

#查看本地镜像列表,生成了hello-world镜像
[root@centos7-temp ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
nginx         1.18.0    605c77e624dd   12 months ago   141MB
nginx         latest    605c77e624dd   12 months ago   141MB
mysql         latest    3218b38490ce   12 months ago   516MB
hello-world   latest    feb5d9fea6a5   15 months ago   13.3kB

#查看容器运行进程
[root@centos7-temp ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED          STATUS                      PORTS     NAMES
08be02244e32   hello-world   "/hello"   56 seconds ago   Exited (0) 55 seconds ago             dazzling_banzai
#一次性运行容器中的命令
[root@centos7-temp ~]#docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS                     PORTS     NAMES

[root@centos7-temp ~]# docker run nginx echo "Hello "
Hello

[root@centos7-temp ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS                     PORTS     NAMES
96ccb61ea8cf   nginx         "/docker-entrypoint.…"   5 seconds ago   Exited (0) 4 seconds ago             hopeful_carver
#指定容器名称
[root@centos7-temp ~]# docker run --name a1 nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/01/04 09:35:50 [notice] 1#1: using the "epoll" event method
2023/01/04 09:35:50 [notice] 1#1: nginx/1.21.5
2023/01/04 09:35:50 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2023/01/04 09:35:50 [notice] 1#1: OS: Linux 3.10.0-1160.81.1.el7.x86_64
2023/01/04 09:35:50 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/01/04 09:35:50 [notice] 1#1: start worker processes
2023/01/04 09:35:50 [notice] 1#1: start worker process 31
2023/01/04 09:35:50 [notice] 1#1: start worker process 32
^C2023/01/04 09:37:22 [notice] 1#1: signal 2 (SIGINT) received, exiting
2023/01/04 09:37:22 [notice] 31#31: exiting
2023/01/04 09:37:22 [notice] 31#31: exit
2023/01/04 09:37:22 [notice] 32#32: exiting
2023/01/04 09:37:22 [notice] 32#32: exit
2023/01/04 09:37:22 [notice] 1#1: signal 17 (SIGCHLD) received from 31
2023/01/04 09:37:22 [notice] 1#1: worker process 31 exited with code 0
2023/01/04 09:37:22 [notice] 1#1: signal 29 (SIGIO) received
2023/01/04 09:37:22 [notice] 1#1: signal 17 (SIGCHLD) received from 32
2023/01/04 09:37:22 [notice] 1#1: worker process 32 exited with code 0
2023/01/04 09:37:22 [notice] 1#1: exit

[root@centos7-temp ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS                     PORTS     NAMES
20c5cc588587   nginx     "/docker-entrypoint.…"   About a minute ago   Exited (0) 9 seconds ago             a1
#运行交互式容器
[root@centos7-temp ~]# docker run -it nginx sh
# ls
bin   dev           docker-entrypoint.sh  home  lib64  mnt  proc  run   srv  tmp  var
boot  docker-entrypoint.d  etc             lib   media  opt  root  sbin  sys  usr
# 
#退出交互式容器,容器不在运行
# exit
[root@centos7-temp ~]# 

#查看运行的容器
[root@centos7-temp ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
#退出交互式容器,容器继续运行
同时按三个键,ctrl+p+q

#查看运行的容器
[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
d17aecdf3a46   nginx     "/docker-entrypoint.…"   19 seconds ago   Up 18 seconds   80/tcp    ecstatic_archimedes
#设置容器内的主机名
[root@centos7-temp ~]# docker run -it -h centos7 alpine
/ # hostname
centos7
/ # cat /etc/hosts
127.0.0.1    localhost
::1    localhost ip6-localhost ip6-loopback
fe00::0    ip6-localnet
ff00::0    ip6-mcastprefix
ff02::1    ip6-allnodes
ff02::2    ip6-allrouters
172.17.0.2    centos7
/ # 
#一次性运行容器,退出后立即删除,用于测试
[root@centos7-temp ~]# docker run --rm alpine cat /etc/issue
Welcome to Alpine Linux 3.17
Kernel \r on an \m (\l)

#查看运行的容器
[root@centos7-temp ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
#前台运行容器,会占用终端
[root@centos7-temp ~]# docker run nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/01/04 15:02:23 [notice] 1#1: using the "epoll" event method
2023/01/04 15:02:23 [notice] 1#1: nginx/1.21.5
2023/01/04 15:02:23 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2023/01/04 15:02:23 [notice] 1#1: OS: Linux 3.10.0-1160.81.1.el7.x86_64
2023/01/04 15:02:23 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/01/04 15:02:23 [notice] 1#1: start worker processes
2023/01/04 15:02:23 [notice] 1#1: start worker process 31
2023/01/04 15:02:23 [notice] 1#1: start worker process 32

#后台运行容器
[root@centos7-temp ~]# docker run -d nginx
5ede6fcb99bd6064be801beac288d2ce6de8d62e5f38dad4e2f756ee0de8dcd0

[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
5ede6fcb99bd   nginx     "/docker-entrypoint.…"   10 seconds ago   Up 10 seconds   80/tcp    modest_perlman
#设置容器开机自启动(默认不会自启动)
[root@centos7-temp ~]# docker run -d --name nginx --restart=always nginx
6aea8688df49ffd012e57a3c46bfa1e3f71dbeb3c2eca2de21a9f267e4d4c42a

[root@centos7-temp ~]# reboot

[root@centos7-temp ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                               NAMES
6aea8688df49   nginx     "/docker-entrypoint.…"   39 seconds ago   Up 18 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx
#随机端口映射
[root@centos7-temp ~]# docker run -P nginx

[root@centos7-temp ~]# ss -ntl
State       Recv-Q Send-Q                 Local Address:Port                                Peer Address:Port              
LISTEN      0      128                                *:32769                                          *:*                  
LISTEN      0      128                                *:22                                             *:*                  
LISTEN      0      100                        127.0.0.1:25                                             *:*                  
LISTEN      0      128                             [::]:32769                                       [::]:*                  
LISTEN      0      128                             [::]:22                                          [::]:*                  
LISTEN      0      100                            [::1]:25                                          [::]:*

#此命令可以查看容器端口映射关系
[root@centos7-temp ~]# docker port 1bf6734c7b04 
80/tcp -> 0.0.0.0:32769
80/tcp -> :::32769

#测试访问
[root@centos7-temp ~]# curl 127.0.0.1:32769 -I
HTTP/1.1 200 OK
Server: nginx/1.21.5
Date: Sun, 08 Jan 2023 15:30:08 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 28 Dec 2021 15:28:38 GMT
Connection: keep-alive
ETag: "61cb2d26-267"
Accept-Ranges: bytes

#退出前台运行,容器也会停止
[root@centos7-temp ~]# docker run -P nginx
2023/01/08 15:33:42 [notice] 1#1: exit

[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

#容器启动后,默认处于预定义的NAT网络中,所以外部网络的主机无法直接访问容器中网络服务docker run -P 可以将事先容器预定义的所有端口映射宿主机的网卡的随机端口,默认从32768开始
使用随机端口时,当停止容器后再启动可能会导致端口发生变化

#指定端口映射
[root@centos7-temp ~]# docker run -d -p 80:80 nginx
cd4108e6577e20e270510f3d087c0e6fa82ef918d2d342cc5bd5870f334c1519

[root@centos7-temp ~]# ss -ntl
State       Recv-Q Send-Q                 Local Address:Port                                Peer Address:Port              
LISTEN      0      128                                *:80                                             *:*                  
LISTEN      0      128                                *:22                                             *:*                  
LISTEN      0      100                        127.0.0.1:25                                             *:*                  
LISTEN      0      128                             [::]:80                                          [::]:*                  
LISTEN      0      128                             [::]:22                                          [::]:*                  
LISTEN      0      100                            [::1]:25                                          [::]:*

[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED             STATUS          PORTS                               NAMES
cd4108e6577e   nginx     "/docker-entrypoint.…"   2 minutes ago       Up 2 minutes    0.0.0.0:80->80/tcp, :::80->80/tcp   pedantic_clarke

#docker run -p 可以将容器的预定义的指定端口映射到宿主机的相应端口
注意: 多个容器映射到宿主机的端口不能冲突,但容器内使用的端口可以相同

2.列出容器

格式:docker ps [选项]

选项:

-a:列出所有,包括运行和停止的

-q:只显示容器ID

-s:查看容器大小

-l:查看最新创建的容器

-f:查看停止运行的容器

用法:

#查看运行的容器
[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS         PORTS     NAMES
6950646e97f8   nginx     "/docker-entrypoint.…"   27 minutes ago   Up 2 seconds   80/tcp    nginx

#查看所有容器
[root@centos7-temp ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                     PORTS     NAMES
e506a29205b6   mysql     "docker-entrypoint.s…"   3 seconds ago    Exited (1) 2 seconds ago             kind_ardinghelli
6950646e97f8   nginx     "/docker-entrypoint.…"   28 minutes ago   Up 42 seconds              80/tcp    nginx

#只显示容器ID
[root@centos7-temp ~]# docker ps -q
6950646e97f8

#查看容器大小
[root@centos7-temp ~]# docker ps -s
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS         PORTS     NAMES     SIZE
6950646e97f8   nginx     "/docker-entrypoint.…"   29 minutes ago   Up 2 minutes   80/tcp    nginx     1.09kB (virtual 141MB)

#查看最新创建的容器
[root@centos7-temp ~]# docker ps -l
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS                     PORTS     NAMES
e506a29205b6   mysql     "docker-entrypoint.s…"   2 minutes ago   Exited (1) 2 minutes ago             kind_ardinghelli

#查看停止运行的容器
[root@centos7-temp ~]# docker ps -f 'status=exited'
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS                     PORTS     NAMES
e506a29205b6   mysql     "docker-entrypoint.s…"   3 minutes ago   Exited (1) 3 minutes ago             kind_ardinghelli

3.查看容器内的进程

格式:docker top 容器名/容器ID

用法:

#容器名查看
[root@centos7-temp ~]# docker top nginx
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                2053                2033                0                   23:42               ?                   00:00:00            nginx: master process nginx -g daemon off;
101                 2098                2053                0                   23:42               ?                   00:00:00            nginx: worker process
101                 2099                2053                0                   23:42               ?                   00:00:00            nginx: worker process

#容器ID查看
[root@centos7-temp ~]# docker top 6950646e97f8
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                2053                2033                0                   23:42               ?                   00:00:00            nginx: master process nginx -g daemon off;
101                 2098                2053                0                   23:42               ?                   00:00:00            nginx: worker process
101                 2099                2053                0                   23:42               ?                   00:00:00            nginx: worker process

4.查看容器内的资源使用情况

格式:docker stats 容器名/容器ID

#查看指定容器
[root@centos7-temp ~]# docker stats nginx
CONTAINER ID   NAME      CPU %     MEM USAGE / LIMIT     MEM %     NET I/O     BLOCK I/O   PIDS
6950646e97f8   nginx     0.00%     1.957MiB / 1.934GiB   0.10%     740B / 0B   0B / 0B     3
[root@centos7-temp ~]# docker stats 6950646e97f8
CONTAINER ID   NAME      CPU %     MEM USAGE / LIMIT     MEM %     NET I/O     BLOCK I/O   PIDS
6950646e97f8   nginx     0.00%     1.957MiB / 1.934GiB   0.10%     740B / 0B   0B / 0B     3

#查看所有容器
[root@centos7-temp ~]# docker stats 
CONTAINER ID   NAME            CPU %     MEM USAGE / LIMIT     MEM %     NET I/O         BLOCK I/O        PIDS
4b13c503d086   elasticsearch   0.30%     1.135GiB / 1.934GiB   58.72%    1.22kB / 984B   251MB / 1.62MB   44
6950646e97f8   nginx           0.00%     1.957MiB / 1.934GiB   0.10%     740B / 0B       0B / 0B          3

5.查看容器的详细信息

格式:docker inspect 容器名/容器ID

[root@centos7-temp ~]# docker inspect nginx

6.删除容器

格式:docker rm [选项] 容器名/容器ID

-f:强制删除正在运行的容器

-a:删除所有,包括运行和停止的

用法:

#正在运行的容器需要加上-f选项强制删除
[root@centos7-temp ~]# docker rm 6950646e97f8 
Error response from daemon: You cannot remove a running container 6950646e97f88ce338d4294c1bd6d088b5e5245709f49604a137ba5951879607. Stop the container before attempting removal or force remove
[root@centos7-temp ~]# docker rm -f 6950646e97f8 
6950646e97f8
#删除所有容器
[root@centos7-temp ~]# docker ps -a
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS                      PORTS                                                                                  NAMES
4b13c503d086   elasticsearch:7.6.2   "/usr/local/bin/dock…"   20 minutes ago   Up 20 minutes               0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp   elasticsearch
e506a29205b6   mysql                 "docker-entrypoint.s…"   36 minutes ago   Exited (1) 36 minutes ago                                                                                          kind_ardinghelli

[root@centos7-temp ~]# docker rm -f `docker ps -a`

[root@centos7-temp ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
#删除所有正在运行的容器
[root@centos7-temp ~]#docker rm -f `docker ps -q -f status=running`

#删除所有停止运行的容器
[root@centos7-temp ~]#docker rm `docker ps -qf status=exited`
或
[root@centos7-temp ~]docker container prune -f

7.启动和停止容器

格式:

docker start 容器名/容器ID

docker stop 容器名/容器ID

docker restart 容器名/容器ID

docker pause 容器名/容器ID

docker unpause 容器名/容器ID

用法:

#启动容器
[root@centos7-temp ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@centos7-temp ~]# docker start 730a80fdf57f
730a80fdf57f
[root@centos7-temp ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND                  CREATED      STATUS         PORTS     NAMES
730a80fdf57f   nginx     "/docker-entrypoint.…"   3 days ago   Up 5 seconds   80/tcp    nginx1

#停止容器
[root@centos7-temp ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND                  CREATED      STATUS              PORTS     NAMES
730a80fdf57f   nginx     "/docker-entrypoint.…"   3 days ago   Up About a minute   80/tcp    nginx1
[root@centos7-temp ~]# docker stop 730a80fdf57f 
730a80fdf57f
[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

#重启容器
[root@centos7-temp ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS                          PORTS     NAMES
54218a657176   nginx     "/docker-entrypoint.…"   About a minute ago   Exited (0) About a minute ago             nginx1
7542f9a9e4ca   busybox   "sh"                     3 days ago           Exited (0) 3 days ago                     hardcore_taussig
7912c335a5e1   mysql     "docker-entrypoint.s…"   3 days ago           Exited (1) 3 days ago                     musing_bose
[root@centos7-temp ~]# docker restart nginx1
nginx1
[root@centos7-temp ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
54218a657176   nginx     "/docker-entrypoint.…"   2 minutes ago   Up 5 seconds   80/tcp    nginx1

#暂停容器中的所有进程
[root@centos7-temp ~]# docker pause nginx1
nginx1

#恢复容器中的所有进程
[root@centos7-temp ~]# docker unpause nginx1
nginx1

8.给正在运行的容器发信号

格式:docker kill 容器名/容器ID

用法:

#默认发送关闭信号
[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
54218a657176   nginx     "/docker-entrypoint.…"   15 minutes ago   Up 12 minutes   80/tcp    nginx1

[root@centos7-temp ~]# docker kill nginx1
nginx1

[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
#强制关闭所有运行中的容器
[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS         PORTS     NAMES
246349e5a3a8   busybox   "sh"                     7 seconds ago    Up 5 seconds             busybox1
54218a657176   nginx     "/docker-entrypoint.…"   23 minutes ago   Up 3 minutes   80/tcp    nginx1

[root@centos7-temp ~]# docker kill `docker ps -a -q`
246349e5a3a8
54218a657176
Error response from daemon: Cannot kill container: eb90aedd7724: Container eb90aedd772498b8ca5090708f5e80a2fc6d51fc6ccbc46b6eb0438a520d4788 is not running
Error response from daemon: Cannot kill container: b02457163726: Container b02457163726d73a1951ca6fe8a806ab7d309327321157f90924895419096702 is not running
Error response from daemon: Cannot kill container: 7d9d15cdebcc: Container 7d9d15cdebccfbf257d132c62ad3011fae5fb13c12507fa0832cb9a36b10b242 is not running
Error response from daemon: Cannot kill container: 73e5f7ffbd24: Container 73e5f7ffbd246b19cf714c26a8977665e329658540dd0d6bbfe350d69ee06598 is not running
Error response from daemon: Cannot kill container: 7912c335a5e1: Container 7912c335a5e16a3d236d916eed47ea8b57738e228080d21e4a69b3c7ddf8b5cc is not running

[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

9.进入正在运行的容器

(1)

#操作会在同一个容器的多个会话界面同步显示,所有使用此方式进入容器的操作都是同步显示的,且使用exit退出后容器自动关闭,不推荐使用,需要进入到有shell环境的容器

格式:docker attach 容器名/容器ID

用法:

#进入正在运行的centos容器
[root@centos7-temp ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
14e6aa0417f5   centos    "/bin/bash"   5 seconds ago   Up 5 seconds             c1

[root@centos7-temp ~]# docker attach c1

#同时在第二个终端attach到容器,执行命令,两个终端同步显示结果
[root@14e6aa0417f5 /]# cat /etc/redhat-release 
CentOS Linux release 8.4.2105

#exit同时退出
[root@14e6aa0417f5 /]# exit
exit
[root@centos7-temp ~]# 
#退出后,容器自动停止
[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS         PORTS     NAMES
14e6aa0417f5   centos    "/bin/bash"   19 minutes ago   Up 2 minutes             c1

[root@centos7-temp ~]# docker attach c1

[root@14e6aa0417f5 /]# exit
exit
[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

(2)

#在运行中的容器启动新进程,可以执行单次命令,以及进入容器测试环境使用此方式,使用exit退出,但容器还在运行,此为推荐方式

格式:docker exec -it 容器名/容器ID sh/bash

选项:-it:在容器中生成一个交互式的终端

用法:

#执行一次性命令
[root@centos7-temp ~]# docker exec c1 cat /etc/redhat-release 
CentOS Linux release 8.4.2105

#进入容器内执行命令,exit退出,但容器不停止
[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS              PORTS     NAMES
14e6aa0417f5   centos    "/bin/bash"   19 minutes ago   Up About a minute             c1

[root@centos7-temp ~]# docker exec -it c1 bash

[root@14e6aa0417f5 /]# cat /etc/redhat-release 
CentOS Linux release 8.4.2105

[root@14e6aa0417f5 /]# exit
exit

[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS         PORTS     NAMES
14e6aa0417f5   centos    "/bin/bash"   19 minutes ago   Up 2 minutes             c1
标签: docker 容器

本文转载自: https://blog.csdn.net/zhanghongshun624/article/details/128508601
版权归原作者 a rookie. 所有, 如有侵权,请联系我们删除。

“Docker常用命令总结”的评论:

还没有评论