【Docker】Docker学习③ - Docker镜像管理
- 三、Docker镜像管理
- 1. 搜索镜像
- 2. 下载镜像
- 3. 查看本地镜像
- 4. 镜像导出
- 5. 镜像导入
- 6. 删除镜像
- 7. 容器操作基础命令
- 7.1 从镜像启动一个容器
- 7.2 显示正在运行的容器
- 7.3 显示所有容器
- 7.4 删除运行中的容器
- 7.5 随机映射端口
- 7.6 指定端口映射
- 7.7 查看容器已经映射的端口
- 7.8 自定义容器名称
- 7.9 后台启动容器
- 7.10 创建并进入容器
- 7.11 单次运行
- 7.12 传递运行命令
- 7.13 容器的启动和关闭
- 7.14 进入到正在运行的容器
- 7.15 查看容器内部的hosts文件
- 7.16 批量关闭正在运行的容器
- 7.17 批量强制关闭正在运行中的容器
- 7.18 批量删除已退出容器
- 7.19 批量删除所有容器
- 7.20 指定容器DNS
三、Docker镜像管理
- Docker镜像含有启动容器所需的文件系统及所需要的内容,因此镜像主要用于创建并启动docker容器。
- Docker镜像里面是一层层文件系统,叫做Union FS(联合文件系统),联合文件系统可以将几层目录挂载到一起,形成一个虚拟文件系统,虚拟文件系统的目录结构就像普通linux的目录结构一样,docker通过这些文件再加上宿主机的内核提供了一个linux的虚拟环境,每一层文件系统我们叫做layer,连恶化文件系统可以对每一层文件系统设置三种权限,只读(readonly)、读写(readwrite)和写出(whiteout-able),但是docker镜像中每一层文件系统都是只读的,构建镜像的时候,从一个最基本的操作系统开始,每个构建的操作都相当于做一层的修改,增加了一层文件系统,一层层往上叠加,上层的修改会覆盖底层该位置的可见性,这也很容易理解,就像上层把底层遮住了一样,当使用镜像的时候,我们只会看到一个完全的整体,不知道里面有几层也不需要知道里面有几层,结构如下:
一个典型的linux文件系统由bootfs和rootfs两部分组成。- bootfs(boot file system)主要包含bootloader和kernel ,bootloader主要用于加载kernel,当kernel被加载到内存中后bootfs会被umount掉。
- rootfs(root file system)包含的就是典型Linux系统中的 /dev,/proc,/bin,/etc 等标准目录和文件。
- 对于docker镜像通常都比较小,官方提供的centos基础镜像在200MB左右,一些其他版本的镜像甚至只有几MB,docker镜像直接调用宿主机的内核,镜像中只提供rootfs,也就是只需要包括最基本的命令、工具和程序库就可以了,比如alpine镜像,在5MB左右。
1. 搜索镜像
在官方的docker仓库中搜索指定名称的docker镜像,也会有很多三方镜像。
docker search centos:7.2.1511 #带指定版本号
docker search centos #不带版本号默认latest
2. 下载镜像
从docker仓库将镜像下载到本地
docker pull 仓库服务器:端口/项目名称/镜像名称:tag(版本)号docker pull alpinedocker pull nginxdocker pull hello-worlddocker pull centos
3. 查看本地镜像
下载完成的镜像比下载的大,因为下载完成后会解压
docker imagesREPOSITORY #镜像所属的仓库名称TAG #镜像版本号(标识符),默认为latestIMAGE ID #镜像唯一ID标识CREATED #镜像创建的时间VIRTUAL SIZE #镜像的大小
4. 镜像导出
可以将镜像从本地导出为一个压缩文件,然后复制到其他服务器进行导入使用
导出方法1:
docker save centos -o /opt/centos.tar.gzll /opt/centos.tar.gz
导出方法2:
docker save centos > /opt/centos-1.tar.gzll /opt/centos-1.tar.gz查看镜像内容:tar xvf centos.tar.gzcat manifest.json #包含了镜像的相关配置,配置文件、分层[root@gbase8c_private opt]# cat manifest.json [{"Config":"5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6.json","RepoTags":["centos:latest"],"Layers":["079bc5e75545bf45253ab44ce73fbd51d96fa52ee799031e60b65a82e89df662/layer.tar"]}]#分层为了方便文件的使用,即相同的文件可以共用
5. 镜像导入
docker load < /opt/centos.tar.gz[root@gbase8c_1 opt]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE[root@gbase8c_1 opt]# docker load < /opt/centos.tar.gz74ddd0ec08fa: Loading layer [==================================================>] 238.6MB/238.6MBLoaded image: centos:latest
验证镜像:
docker images[root@gbase8c_1 opt]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEcentos latest 5d0da3dc9764 2 years ago 231MB
6. 删除镜像
docker rmi centos
日志:
[root@gbase8c_private opt]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEalpine latest 8ca4688f4f35 7 weeks ago 7.34MBnginx latest 605c77e624dd 23 months ago 141MBhello-world latest feb5d9fea6a5 2 years ago 13.3kBcentos latest 5d0da3dc9764 2 years ago 231MB[root@gbase8c_private opt]# docker rmi centosUntagged: centos:latestUntagged: centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6Deleted: sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59[root@gbase8c_private opt]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEalpine latest 8ca4688f4f35 7 weeks ago 7.34MBnginx latest 605c77e624dd 23 months ago 141MBhello-world latest feb5d9fea6a5 2 years ago 13.3kB
7. 容器操作基础命令
命令格式:
#docker run [选项] [镜像名] [shell命令] [参数]#docker run [参数选项] [镜像名称,必须在所有选项的后面] [/bin/echo/ 'hello world'] #单次执行,没有自定义容器名称#docker run centos /bin/echo 'hello world' #启动的容器在执行完shell命令就退出了
7.1 从镜像启动一个容器
会直接进入到容器,并随机生成容器ID和名称
docker run -it docker.io/centos bash
#退出容器不注销
ctrl+p+q
7.2 显示正在运行的容器
docker ps
7.3 显示所有容器
包括当前正在运行以及以及关闭的所有容器
docker ps -a
[root@gbase8c_private ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7d6158088cbd centos "bash" About a minute ago Up About a minute loving_mcnulty
e3e5fc354a71 hello-world "/hello" About a minute ago Exited (0) About a minute ago determined_mayer
7.4 删除运行中的容器
即使容器正在运行当中,也会被强制删除掉
docker rm -f id
[root@gbase8c_private ~]# docker rm -f 7d6158088cbd
7d6158088cbd
[root@gbase8c_private ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e3e5fc354a71 hello-world "/hello" 2 minutes ago Exited (0) 2 minutes ago determined_mayer
7.5 随机映射端口
docker pull nginx #下载nginx 镜像
docker run -P docker.io/nginx #前台启动并随机映射本地端口到容器的80
前台启动的会话窗口无法进行其他操作,除非退出,但是退出后容器也会退出
随机端口映射,其实是默认从32768开始
[root@gbase8c_private ~]# docker run -P docker.io/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.sh10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf10-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 up2023/11/21 11:43:14 [notice] 1#1: using the "epoll" event method2023/11/21 11:43:14 [notice] 1#1: nginx/1.21.52023/11/21 11:43:14 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 2023/11/21 11:43:14 [notice] 1#1: OS: Linux 3.10.0-1160.71.1.el7.x86_642023/11/21 11:43:14 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:10485762023/11/21 11:43:14 [notice] 1#1: start worker processes2023/11/21 11:43:14 [notice] 1#1: start worker process 292023/11/21 11:43:14 [notice] 1#1: start worker process 30[root@gbase8c_private ~]# ss -nlt | grep 32LISTEN 0 128 [::]:32768 [::]:* [root@gbase8c_private ~]# lsof -i:32768COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEdocker-pr 18312 root 4u IPv6 1918183 0t0 TCP *:filenet-tms (LISTEN)
7.6 指定端口映射
- 方式一:本地端口81映射到容器80端口
docker run -d -p 81:80 --name nginx-test-port1 nginx
- 方式二:本地IP:本地端口:容器端口
docker run -d -p 192.168.56.199:82:80 --name nginx-test-port2 docker.io/nginx
- 方式三:本地IP:本地随机端口:容器端口
docker run -d -p 192.168.56.199::80 --name nginx-test-port3 docker.io/nginx
- 方式四:本机IP:本地端口:容器端口/协议,默认为TCP协议
docker run -d -p 192.168.56.199:83:80/udp --name nginx-test-port4 docker.io/nginx
- 方式五:一次性映射多个端口+协议
docker run -d -p 86:80/tcp -p 443:443/tcp -p 54:54/udp --name nginx-test-port5 docker.io/nginx
- 查看运行的容器:
[root@gbase8c_private ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESe98563df7763 nginx "/docker-entrypoint.…" 31 seconds ago Up 30 seconds 0.0.0.0:54->54/udp, 0.0.0.0:443->443/tcp, 0.0.0.0:86->80/tcp nginx-test-port5d906beba9990 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 80/tcp, 192.168.56.199:83->80/udp nginx-test-port43a11ac02e895 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 192.168.56.199:32768->80/tcp nginx-test-port38af8fc6162d8 nginx "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 192.168.56.199:82->80/tcp nginx-test-port26f9517d4c2d6 nginx "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 0.0.0.0:81->80/tcp nginx-test-port1
- 查看Nginx容器访问日志:
docker logs nginx-test-port3docker logs -f nginx-test-port3
7.7 查看容器已经映射的端口
docker port nginx-test-port5
日志:
[root@gbase8c_private ~]# docker port nginx-test-port5443/tcp -> 0.0.0.0:44354/udp -> 0.0.0.0:5480/tcp -> 0.0.0.0:86
7.8 自定义容器名称
docker run -it --name nginx-test nginx
7.9 后台启动容器
docker run -d -P --name nginx-test1 docker.io/nginxdocker run -d --name centos-test1 docker.io/centos
[root@gbase8c_private ~]# docker run -d --name centos-test1 docker.io/centose5ff3cdb2754c37f706e29dd4be5a37e755e46889cada528562def7798899af2
7.10 创建并进入容器
创建容器后直接进入,执行exit退出后容器关闭
docker run -t -i --name test-centos2 docker.io/centos /bin/bash
日志:
[root@gbase8c_private ~]# docker run -t -i --name test-centos2 docker.io/centos /bin/bash[root@5cb9c84e7d17 /]# ps -auxUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDroot 1 1.2 0.0 12036 2108 pts/0 Ss 01:55 0:00 /bin/bashroot 13 0.0 0.0 47572 2060 pts/0 R+ 01:55 0:00 ps -aux
7.11 单次运行
容器退出后自动删除
docker run -it --rm --name nginx-delete-test docker.io/nginx日志在4.7.13
7.12 传递运行命令
容器需要有一个前台运行的进程才能保持容器的运行,通过传递运行参数是一种方式,另外一可以在构建镜像的时候指定容器启动时运行的前台命令。
docker run -d centos /usr/bin/tail -f ‘/etc/hosts’
日志:
[root@gbase8c_private ~]# docker run -d centos /usr/bin/tail -f '/etc/hosts'74c0f99166085629ed7c488769505a7c35c9a98f96962594593ff0cc8960485d[root@gbase8c_private ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES74c0f9916608 centos "/usr/bin/tail -f /e…" 3 seconds ago Up 3 seconds competent_jepsenbd16c6917a24 nginx "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 80/tcp nginx-delete-test1
7.13 容器的启动和关闭
docker stop 容器ID
docker start 容器ID
日志:
[root@gbase8c_private ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES74c0f9916608 centos "/usr/bin/tail -f /e…" 3 seconds ago Up 3 seconds competent_jepsenbd16c6917a24 nginx "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 80/tcp nginx-delete-test1c844e569456f nginx "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 80/tcp nginx-delete-teste98563df7763 nginx "/docker-entrypoint.…" 18 minutes ago Up 18 minutes 0.0.0.0:54->54/udp, 0.0.0.0:443->443/tcp, 0.0.0.0:86->80/tcp nginx-test-port5d906beba9990 nginx "/docker-entrypoint.…" 20 minutes ago Up 20 minutes 80/tcp, 192.168.56.199:83->80/udp nginx-test-port43a11ac02e895 nginx "/docker-entrypoint.…" 20 minutes ago Up 20 minutes 192.168.56.199:32768->80/tcp nginx-test-port38af8fc6162d8 nginx "/docker-entrypoint.…" 20 minutes ago Up 20 minutes 192.168.56.199:82->80/tcp nginx-test-port26f9517d4c2d6 nginx "/docker-entrypoint.…" 21 minutes ago Up 21 minutes 0.0.0.0:81->80/tcp nginx-test-port1[root@gbase8c_private ~]# docker stop bd16c6917a24bd16c6917a24[root@gbase8c_private ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES74c0f9916608 centos "/usr/bin/tail -f /e…" About a minute ago Up About a minute competent_jepsenc844e569456f nginx "/docker-entrypoint.…" 5 minutes ago Up 5 minutes 80/tcp nginx-delete-teste98563df7763 nginx "/docker-entrypoint.…" 19 minutes ago Up 19 minutes 0.0.0.0:54->54/udp, 0.0.0.0:443->443/tcp, 0.0.0.0:86->80/tcp nginx-test-port5d906beba9990 nginx "/docker-entrypoint.…" 21 minutes ago Up 21 minutes 80/tcp, 192.168.56.199:83->80/udp nginx-test-port43a11ac02e895 nginx "/docker-entrypoint.…" 21 minutes ago Up 21 minutes 192.168.56.199:32768->80/tcp nginx-test-port38af8fc6162d8 nginx "/docker-entrypoint.…" 22 minutes ago Up 22 minutes 192.168.56.199:82->80/tcp nginx-test-port26f9517d4c2d6 nginx "/docker-entrypoint.…" 22 minutes ago Up 22 minutes 0.0.0.0:81->80/tcp nginx-test-port1[root@gbase8c_private ~]# docker start bd16c6917a24Error response from daemon: No such container: bd16c6917a24Error: failed to start containers: bd16c6917a24[root@gbase8c_private ~]# docker stop 74c0f991660874c0f9916608[root@gbase8c_private ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESc844e569456f nginx "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 80/tcp nginx-delete-teste98563df7763 nginx "/docker-entrypoint.…" 19 minutes ago Up 19 minutes 0.0.0.0:54->54/udp, 0.0.0.0:443->443/tcp, 0.0.0.0:86->80/tcp nginx-test-port5d906beba9990 nginx "/docker-entrypoint.…" 22 minutes ago Up 22 minutes 80/tcp, 192.168.56.199:83->80/udp nginx-test-port43a11ac02e895 nginx "/docker-entrypoint.…" 22 minutes ago Up 22 minutes 192.168.56.199:32768->80/tcp nginx-test-port38af8fc6162d8 nginx "/docker-entrypoint.…" 22 minutes ago Up 22 minutes 192.168.56.199:82->80/tcp nginx-test-port26f9517d4c2d6 nginx "/docker-entrypoint.…" 23 minutes ago Up 23 minutes 0.0.0.0:81->80/tcp nginx-test-port1[root@gbase8c_private ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES74c0f9916608 centos "/usr/bin/tail -f /e…" 2 minutes ago Exited (137) 11 seconds ago competent_jepsenc844e569456f nginx "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 80/tcp nginx-delete-teste5ff3cdb2754 centos "/bin/bash" 7 minutes ago Exited (0) 7 minutes ago centos-test15cb9c84e7d17 centos "/bin/bash" 11 minutes ago Exited (130) 7 minutes ago test-centos2264f9bd66c3f nginx "/docker-entrypoint.…" 15 minutes ago Exited (0) 15 minutes ago nginx-teste98563df7763 nginx "/docker-entrypoint.…" 20 minutes ago Up 20 minutes 0.0.0.0:54->54/udp, 0.0.0.0:443->443/tcp, 0.0.0.0:86->80/tcp nginx-test-port5d906beba9990 nginx "/docker-entrypoint.…" 22 minutes ago Up 22 minutes 80/tcp, 192.168.56.199:83->80/udp nginx-test-port43a11ac02e895 nginx "/docker-entrypoint.…" 22 minutes ago Up 22 minutes 192.168.56.199:32768->80/tcp nginx-test-port38af8fc6162d8 nginx "/docker-entrypoint.…" 22 minutes ago Up 22 minutes 192.168.56.199:82->80/tcp nginx-test-port26f9517d4c2d6 nginx "/docker-entrypoint.…" 23 minutes ago Up 23 minutes 0.0.0.0:81->80/tcp nginx-test-port15f5d8410b29a nginx "/docker-entrypoint.…" 14 hours ago Exited (0) 14 hours ago musing_wozniake3e5fc354a71 hello-world "/hello" 15 hours ago Exited (0) 15 hours ago determined_mayer[root@gbase8c_private ~]# docker start 74c0f991660874c0f9916608[root@gbase8c_private ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES74c0f9916608 centos "/usr/bin/tail -f /e…" 2 minutes ago Up 5 seconds competent_jepsenc844e569456f nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 80/tcp nginx-delete-teste98563df7763 nginx "/docker-entrypoint.…" 21 minutes ago Up 20 minutes 0.0.0.0:54->54/udp, 0.0.0.0:443->443/tcp, 0.0.0.0:86->80/tcp nginx-test-port5d906beba9990 nginx "/docker-entrypoint.…" 23 minutes ago Up 23 minutes 80/tcp, 192.168.56.199:83->80/udp nginx-test-port43a11ac02e895 nginx "/docker-entrypoint.…" 23 minutes ago Up 23 minutes 192.168.56.199:32768->80/tcp nginx-test-port38af8fc6162d8 nginx "/docker-entrypoint.…" 23 minutes ago Up 23 minutes 192.168.56.199:82->80/tcp nginx-test-port26f9517d4c2d6 nginx "/docker-entrypoint.…" 24 minutes ago Up 24 minutes 0.0.0.0:81->80/tcp nginx-test-port1
7.14 进入到正在运行的容器
- 7.14.1 使用attach命令
使用方式为docker attact 容器名,attach类似于vnc,操作会在各个容器界面显示,所有使用此方式进入容器的操作都是同步显示的且exit后容器将被关闭,且使用exit退出后容器关闭,不推荐使用,需要进入到有shell环境的容器,比如centos为例:
docker run -it centos centos bash
- 7.14.2 使用exec命令
执行单次命令进入容器,不是很推荐此方式,虽然exit退出容器还在运行。
docker exec -it centos-test3 /bin/bash
日志:
[root@gbase8c_private ~]# docker run --name centos-test3 -itd docker.io/centos /bin/bash46d17824678e24c8642e9e782e21c556b041889192733869c9dba2f50c120347[root@gbase8c_private ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES46d17824678e centos "/bin/bash" 5 seconds ago Up 4 seconds centos-test3[root@gbase8c_private ~]# docker exec -it centos-test3 /bin/bash[root@46d17824678e /]# lsbin etc lib lost+found mnt proc run srv tmp vardev home lib64 media opt root sbin sys usr[root@46d17824678e /]# exitexit[root@gbase8c_private ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES46d17824678e centos "/bin/bash" 49 seconds ago Up 49 seconds centos-test3
- 7.14.3 使用nsenter命令
推荐使用此方式,nsenter命令需要通过PID进入到容器内部,不过可以使用docker inspect获取到容器的PID
yum install util-linux #安装nsenter命令docker inspect -f "{{.NetworkSettings.IPAddress}}" 46d17824678e docker inspect -f "{{.State.Pid}}" mydocker #获取到某个docker容器的PID,可以通过PID进入到容器内docker inspect -f "{{.State.Pid}}" centos-test3nsenter -t PID -m -u -i -n -p
日志:
[root@gbase8c_private ~]# docker inspect -f "{{.NetworkSettings.IPAddress}}" 46d17824678e 172.17.0.2[root@gbase8c_private ~]# docker inspect -f "{{.State.Pid}}" centos-test38645[root@gbase8c_private ~]# nsenter -t 8645 -m -u -i -n -p[root@46d17824678e /]# lsbin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
- 7.14.4 脚本方式
测试脚本是否可以正常进入到容器且退出后仍然正常运行
chmod a+x docker-in.sh./docker-in.sh centos-test3
日志:
[root@gbase8c_private docker]# cat docker-in.sh#!/bin/bashdocker_in(){NAME_ID=$1PID=$(docker inspect -f "{{.State.Pid}}" ${NAME_ID})nsenter -t ${PID} -m -u -i -n -p}docker_in $1[root@gbase8c_private docker]# ./docker-in.sh centos-test3[root@46d17824678e /]# pwd/[root@46d17824678e /]# exitlogout[root@gbase8c_private docker]# ./docker-in.sh centos-test3[root@46d17824678e /]# exitlogout
7.15 查看容器内部的hosts文件
默认会将实例的ID添加到自己的hosts文件
日志:
[root@gbase8c_private docker]# docker run -it --name test-centos3 docker.io/centos /bin/bash[root@cccb2108056e /]# cat /etc/hosts127.0.0.1 localhost::1 localhost ip6-localhost ip6-loopbackfe00::0 ip6-localnetff00::0 ip6-mcastprefixff02::1 ip6-allnodesff02::2 ip6-allrouters172.17.0.3 cccb2108056e #ping 容器ID[root@cccb2108056e /]# ping cccb2108056ePING cccb2108056e (172.17.0.3) 56(84) bytes of data.64 bytes from cccb2108056e (172.17.0.3): icmp_seq=1 ttl=64 time=0.029 ms64 bytes from cccb2108056e (172.17.0.3): icmp_seq=2 ttl=64 time=0.037 ms64 bytes from cccb2108056e (172.17.0.3): icmp_seq=3 ttl=64 time=0.034 ms^C--- cccb2108056e ping statistics ---3 packets transmitted, 3 received, 0% packet loss, time 2007msrtt min/avg/max/mdev = 0.029/0.033/0.037/0.005 ms
7.16 批量关闭正在运行的容器
docker stop $(docker ps -a -q) #正常关闭所有运行中的容器
日志:
[root@gbase8c_private docker]# docker stop $(docker ps -a -q)cccb2108056e46d17824678e
7.17 批量强制关闭正在运行中的容器
docker kill $(docker ps -a -q)
日志:
[root@gbase8c_private docker]# docker kill $(docker ps -a -q)Error response from daemon: Cannot kill container: cccb2108056e: Container cccb2108056e5fea630892d67139ad6ee2bfa666f9d0eae738ff8c5d34984d29 is not running[root@gbase8c_private docker]# docker start cccb2108056ecccb2108056e[root@gbase8c_private docker]# docker kill $(docker ps -a -q)cccb2108056e
7.18 批量删除已退出容器
docker rm -f docker ps -aq -f status=exited
日志:
[root@gbase8c_private docker]# docker rm -f `docker ps -aq -f status=exited`cccb2108056e
7.19 批量删除所有容器
docker rm -f $(docker ps -a -q)
日志:
[root@gbase8c_private docker]# docker run -itd --name centos-test4 docker.io/centos /bin/bash 64bf39002c67ff9aded56780315a4a8c4639a3189d891cdd35092e1c203d4f6d[root@gbase8c_private docker]# docker rm -f $(docker ps -a -q)64bf39002c67261a6c2dd260
7.20 指定容器DNS
Dns服务,默认采用宿主机的dns地址
一是将dns地址配置在宿主机
二是将参数配置在docker启动脚本里面 -dns=1.1.1.1
docker run -it --rm --dns 223.6.6.6 centos bash
cat /etc/resolv.conf
日志:
[root@gbase8c_private docker]# docker run -it --rm --dns 223.6.6.6 centos bash[root@c01a69476a06 /]# cat /etc/resolv.confnameserver 223.6.6.6