[root@docker ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) [root@docker ~]# uname -r 3.10.0-327.el7.x86_64
yum install docker -y
[root@docker ~]# rpm -qa docker docker-1.10.3-46.el7.centos.14.x86_64 [root@docker ~]# docker --version Docker version 1.10.3, build cb079f6-unsupported
咱们能够经过官方的yum源安装docker1.12,或在docker官网下载最新版本。
php
Docker官网:http://www.docker.com/ python
注:使用docker目前使用的越新越好,由于docker一直都在不足,每次更新都更加的完善。linux
[root@docker ~]# systemctl start docker
[root@docker ~]# systemctl status docker ● docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled) Active: active (running) since Thu 2016-12-01 20:17:44 CST; 13s ago Docs: http://docs.docker.com Main PID: 7595 (docker-current) CGroup: /system.slice/docker.service └─7595 /usr/bin/docker-current daemon --exec-opt native.cgroupdriver=systemd --selinux-enabled --log-driver=journald Dec 01 20:17:41 docker docker-current[7595]: time="2016-12-01T20:17:41.379210415+08:00" level=warning msg="Docker could not enable SELinux on the host system" Dec 01 20:17:41 docker docker-current[7595]: time="2016-12-01T20:17:41.528222902+08:00" level=info msg="Graph migration to content-addressability took 0.01 seconds" Dec 01 20:17:42 docker docker-current[7595]: time="2016-12-01T20:17:42.301945116+08:00" level=info msg="Firewalld running: true" Dec 01 20:17:43 docker docker-current[7595]: time="2016-12-01T20:17:43.809557691+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.1...IP address" Dec 01 20:17:44 docker docker-current[7595]: time="2016-12-01T20:17:44.395349153+08:00" level=info msg="Loading containers: start." Dec 01 20:17:44 docker docker-current[7595]: time="2016-12-01T20:17:44.395640410+08:00" level=info msg="Loading containers: done." Dec 01 20:17:44 docker docker-current[7595]: time="2016-12-01T20:17:44.395690801+08:00" level=info msg="Daemon has completed initialization" Dec 01 20:17:44 docker docker-current[7595]: time="2016-12-01T20:17:44.395765160+08:00" level=info msg="Docker daemon" commit=cb079f6-unsupported execdriver=native-0...sion=1.10.3 Dec 01 20:17:44 docker systemd[1]: Started Docker Application Container Engine. Dec 01 20:17:44 docker docker-current[7595]: time="2016-12-01T20:17:44.511453473+08:00" level=info msg="API listen on /var/run/docker.sock" Hint: Some lines were ellipsized, use -l to show in full.
systemctl stop docker
ifconfig docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0 ether 02:42:61:c2:8e:54 txqueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.0.129 netmask 255.255.255.0 broadcast 10.0.0.255 ether 00:0c:29:1c:0e:08 txqueuelen 1000 (Ethernet) RX packets 182195 bytes 17035479 (16.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 14495 bytes 1810618 (1.7 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 0 (Local Loopback) RX packets 4 bytes 200 (200.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 4 bytes 200 (200.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
docker启动后,会生成一个docker:0的网卡(虚拟机也会生成)。nginx
下面我将以docker的三大组件,分别讲解命令。web
docker 运行容器前须要本地存在对应的镜像,若是镜像本地不存在,docker会从镜像仓库下载(默认从Docker Hub仓库下载)。redis
docker search [镜像名称]docker
例:查看dockerhub中nginx的镜像。centos
[root@docker ~]# docker search nginx INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/nginx Official build of Nginx. 4755 [OK] docker.io docker.io/jwilder/nginx-proxy Automated Nginx reverse proxy for docker c... 880 [OK] docker.io docker.io/richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable ... 315 [OK] docker.io docker.io/million12/nginx-php Nginx + PHP-FPM 5.5, 5.6, 7.0 (NG), CentOS... 76 [OK] docker.io docker.io/webdevops/php-nginx Nginx with PHP-FPM 63 [OK] docker.io docker.io/maxexcloo/nginx-php Framework container with nginx and PHP-FPM... 58 [OK] docker.io docker.io/bitnami/nginx Bitnami nginx Docker Image 20 [OK] docker.io docker.io/evild/alpine-nginx Minimalistic Docker image with Nginx 8 [OK] docker.io docker.io/gists/nginx Nginx on Alpine 8 [OK] docker.io docker.io/maxexcloo/nginx Framework container with nginx installed. 7 [OK] docker.io docker.io/webdevops/nginx Nginx container 7 [OK] docker.io docker.io/1science/nginx Nginx Docker images that include Consul Te... 4 [OK] docker.io docker.io/drupaldocker/nginx NGINX for Drupal 3 [OK] docker.io docker.io/ixbox/nginx Nginx on Alpine Linux. 3 [OK] docker.io docker.io/blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 2 [OK] docker.io docker.io/dock0/nginx Arch container running nginx 2 [OK] docker.io docker.io/frekele/nginx docker run --rm --name nginx -p 80:80 -p 4... 2 [OK] docker.io docker.io/servivum/nginx Nginx Docker Image with Useful Tools 2 [OK] docker.io docker.io/xataz/nginx Light nginx image 2 [OK] docker.io docker.io/yfix/nginx Yfix own build of the nginx-extras package 2 [OK] docker.io docker.io/radial/nginx Spoke container for Nginx, a high performa... 1 [OK] docker.io docker.io/tozd/nginx Dockerized nginx. 1 [OK] docker.io docker.io/c4tech/nginx Several nginx images for web applications. 0 [OK] docker.io docker.io/unblibraries/nginx Baseline non-PHP nginx container 0 [OK] docker.io docker.io/watsco/nginx nginx:1.11-alpine 0 [OK]
docker pull [镜像名称]bash
例:下载docker.io/centos/redis服务器
docker pull docker.io/centos/redis Using default tag: latest Trying to pull repository docker.io/centos/redis ... latest: Pulling from docker.io/centos/redis 1d702521dae2: Downloading [=> ] 118.7 kB/5.587 MB 0de01b15a36f: Download complete 66479f7f733c: Download complete c7e83aba718a: Download complete dda623482619: Download complete dda623482619: Pulling fs layer
docker save -o [镜像包名] [镜像名称]
例:导出centos基础镜像为centos.tar
[root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE oldboy/nginx1 latest c436de4b837a 5 days ago 401.1 MB <none> <none> 6ab373e100c9 5 days ago 181.4 MB oldboy/nginx v1 99695fe97fb5 5 days ago 181.4 MB docker.io/nginx latest 05a60462f8ba 3 weeks ago 181.4 MB docker.io/centos latest 0584b3d2cf6d 4 weeks ago 196.5 MB [root@docker ~]# docker save -o centos.tar docker.io/centos [root@docker ~]# ll -h centos.tar -rw-r--r-- 1 root root 195M Dec 1 20:54 centos.tar
docker load --input [镜像包名]
docker load <[镜像包名]
docker images
[root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE oldboy/nginx1 latest c436de4b837a 5 days ago 401.1 MB oldboy/nginx v1 99695fe97fb5 5 days ago 181.4 MB docker.io/nginx latest 05a60462f8ba 3 weeks ago 181.4 MB docker.io/centos latest 0584b3d2cf6d 4 weeks ago 196.5 MB
在列出的信息中,能够看到几个字段的信息
docker rni [镜像id]
例:删除一个镜像
[root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE oldboy/nginx1 latest c436de4b837a 5 days ago 401.1 MB <none> <none> 6ab373e100c9 5 days ago 181.4 MB oldboy/nginx v1 99695fe97fb5 5 days ago 181.4 MB docker.io/nginx latest 05a60462f8ba 3 weeks ago 181.4 MB docker.io/centos latest 0584b3d2cf6d 4 weeks ago 196.5 MB daemon: conflict: unable to delete 0584b3d2cf6d (cannot be forced) - image has dependent child images [root@docker ~]# docker rmi 6ab373e100c9 [root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE oldboy/nginx1 latest c436de4b837a 5 days ago 401.1 MB oldboy/nginx v1 99695fe97fb5 5 days ago 181.4 MB docker.io/nginx latest 05a60462f8ba 3 weeks ago 181.4 MB docker.io/centos latest 0584b3d2cf6d 4 weeks ago 196.5 MB
注:不能删除已有容器的镜像
例:删除一个已有容器的镜像
[root@docker ~]# docker rmi 0584b3d2cf6d Failed to remove image (0584b3d2cf6d): Error response from daemon: conflict: unable to delete 0584b3d2cf6d (cannot be forced) - image has dependent child images
例:移除本地全部未打过标签的本地镜像
docker images能够列出本地全部的镜像,其中可能包含不少中间状态的来打过标签的镜像,大量占据着磁盘空间。
使用下面命令能够清理全部未打过标签的本地镜像
docker rmi $(docker images -q -f "danglingture")
建立镜像为较为复杂点,在以后的文档中会详细介绍。
容器是独立运行的一个或一组应用,以及他们的运行环境。对应的虚拟机能够理解为模拟运行的一套操做系统(提供了运行环境和其余其系统环境)和跑在上面的应用。
启动容器的方式
容器过轻量了,不少时候用户都是随时删除和新建立容器。
docker run [选项、参数] 镜像名称 [执行的命令]
注:若是不指定镜像名称会自动生成名称
例:输出一个hello world ,以后终止容器
[root@docker ~]# docker run docker.io/centos /bin/echo 'hello world' hello world 这跟直接在本地执行一个hello world 没有任何区别。 [root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 443da0721b41 docker.io/centos "/bin/echo 'hello wor" 16 seconds ago Exited (0) 11 seconds ago gigantic_sinoussi 由于没有指定容器名称,因此他本身自动生成了gigantic_sinoussi
例:启动一个bash,容许用户交互,指定容器名称为mycentos
[root@docker ~]# docker run -i -t --name mycentos docker.io/centos /bin/bash [root@c761cee8863f /]# -name:指定容器名称 -t:让docker分配一个伪终端 -i:让容器的标准输入打开(input)
接上例:在交互模户,模式下。用户能够经过所建立的终端来输入命令
[root@c761cee8863f /]# pwd / [root@c761cee8863f /]# ls anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
继续接上例:在可交互模式查看进程,本地信息
[root@c761cee8863f /]# ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 11752 1876 ? Ss 15:44 0:00 /bin/bash root 17 0.0 0.0 47424 1660 ? R+ 16:02 0:00 ps aux [root@c761cee8863f /]# free -m total used free shared buff/cache available Mem: 1993 158 1121 8 713 1669 Swap: 475 0 475 [root@c761cee8863f /]# exit [root@docker ~]# free -m total used free shared buff/cache available Mem: 1993 147 1140 8 706 1681 Swap: 475 0 475 容器不是一个虚拟机,他只是一个进程,pid为1的进程结束了,这个容器就挂了,他就是为了隔离这个进程而存在的。 docker的隔离性并非很好,在物理机能够看到服务器的硬件信息,docker自己不能解决,可是能够用一些黑科技解决,固然若是本身用的话也能够不用解决这个问题。
docker run来建立容器时,docker在后台运行的标准操做包括:
docker start [容器名称|容器id]
例:启动mycentos
[root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c761cee8863f docker.io/centos "/bin/bash" 35 minutes ago Exited (0) 18 minutes ago mycentos ccfc03f90613 docker.io/centos "/bin/echo /bin/bash" 36 minutes ago Exited (0) 36 minutes ago elated_brown 443da0721b41 docker.io/centos "/bin/echo 'hello wor" 44 minutes ago Exited (0) 44 minutes ago gigantic_sinoussi [root@docker ~]# docker start mycentos mycentos [root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c761cee8863f docker.io/centos "/bin/bash" 36 minutes ago Up 3 seconds mycentos
更多的时候须要让Docker 在后台运行而不是直接执行命令的结果输出在当前宿主机下,此时,能够经过添加 -d 参数来实现。
例:若是不使用-d参数运行容器
[root@docker ~]# docker run centos echo hello world
hello world
容器会把输出结果打印到宿主机上
例:使用-d参数
[root@docker ~]# docker run -d centos echo hello world f6f8844f843b6479ab2d9faf52aef55a8c9516effa1dc7574299989025f24a41 [root@docker ~]# docker logs f6f8844f843b6479ab2d9faf52aef55a8c9516effa1dc7574299989025f24a41 hello world
这时候容器会在后台运行并不会把输出结果(STDOUT)打印到宿主机上面(输出结果能够用docker logs查看)
注:容器是否会长久运行,是和docker run指定的
docker attach [镜像名称]
这个登陆至关于单用户模式,只能有一我的登陆。通常生产不推荐使用此法。
例:一方操做另外一方会自动操做。
生产场景是不使用docker attach
的,须要咱们使用nsenter
这个工具,这个工具包含在util-linux
软件包里面。nsenter能够访问另外一个进程的名字空间。nsenter要正常工做须要有root权限。
[root@docker ~]# yum install util-linux -y
通常状况下最小化安装里都已经安装完毕。
咱们经过nsenter
就能够进入容器
,可是nsenter是经过容器第一个进程的pid
进入容器里,因此咱们须要知道容器的pid。咱们能够经过docker inspect
来获取到pid
利用docker inspect 得到PID号
[root@docker ~]# docker start mycentos mycentos [root@docker ~]# docker inspect -f "{{ .State.Pid }}" mycentos 10656 [root@docker ~]# nsenter -t 10656 -m -u -i -n -p [root@c761cee8863f /]#
那个参数选项是简写全称的就是:
nsenter --traget $PID --mount --uts --ipc --net --pid
nsenter参数:
–mount参数是进去到mount namespace中
–uts参数是进入到uts namespace中
–ipc参数是进入到System V IPC namaspace中
–net参数是进入到network namespace中
–pid参数是进入到pid namespace中
–user参数是进入到user namespace中
[root@c761cee8863f /]# ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 11776 1664 ? Ss+ 22:06 0:00 /bin/bash root 14 0.0 0.0 13376 1988 ? S 22:12 0:00 -bash root 27 0.0 0.0 49024 1808 ? R+ 22:21 0:00 ps aux
其中/bin/bash 是咱们开启容器运行的第一个进程
-bash 是nsenter 启动时生成的进程,这样咱们退出后,容器不会退出,仍然会运行。
咱们不能每次都要执行上面的命令,找到PID 在进入再次咱们须要写一个脚本
cat cat docker_in.sh #!/bin/bash # Use nsenter to access docker docker_in(){ NAME_ID=$1 PID=$(docker inspect -f "{{ .State.Pid }}" $NAME_ID) nsenter -t $PID -m -u -i -n -p } docker_in $1
执行结果
[root@docker ~]# chmod +x docker_in.sh [root@docker ~]# ./docker_in.sh mycentos [root@c761cee8863f /]# ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 11776 1664 ? Ss+ 22:06 0:00 /bin/bash root 28 0.0 0.0 13376 1984 ? S 22:28 0:00 -bash root 41 0.0 0.0 49024 1812 ? R+ 22:28 0:00 ps aux
docker exec 的本意不是为了进入容器,仅仅是为了在外面执行一个容器的操做。
docker exec [容器名称|id] 命令
例:在容器mycentos外执行命令whoami等
[root@docker ~]# docker exec mycentos whoami root [root@docker ~]# docker exec mycentos ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 11776 1664 ? Ss+ 22:06 0:00 /bin/bash root 46 0.0 0.0 47424 1660 ? Rs 22:34 0:00 ps aux
[root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c761cee8863f docker.io/centos "/bin/bash" 6 hours ago Up 30 minutes mycentos [root@docker ~]# docker exec -it mycentos /bin/bash [root@c761cee8863f /]# exit [root@docker ~]# docker exec -it c761cee8863f /bin/bash [root@c761cee8863f /]#
docker ps
例:查看正在运行的容器
[root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c761cee8863f docker.io/centos "/bin/bash" 6 hours ago Up 32 minutes mycentos
例:查看全部容器
[root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES edfdd26e20da centos "echo hello world" 3 hours ago Exited (0) 3 hours ago kickass_heisenberg f6f8844f843b centos "echo hello world" 3 hours ago Exited (0) 3 hours ago reverent_yalow cd9cf2398630 centos "while true ;echo hel" 3 hours ago Created high_mclean cc93e8ced322 centos "echo hello world" 3 hours ago Exited (0) 3 hours ago stupefied_dubinsky 475cd09a2e6e centos "/bin/sh -c 'while tr" 3 hours ago Exited (1) 3 hours ago romantic_easley d580c6389d1e centos "/bin/bash -c 'while " 3 hours ago Exited (1) 3 hours ago fervent_williams b54ed375b3f1 centos "/bin/sh -c 'while tr" 3 hours ago Exited (1) 3 hours ago goofy_davinci a9864174cfb0 centos "while true; do echo " 3 hours ago Created stoic_goodall eb62a497f026 centos "/bin/sh -c 'while tr" 3 hours ago Exited (1) 3 hours ago pedantic_ritchie 3e68d0c13bfd centos "/bin/sh -c 'while tr" 3 hours ago Exited (1) 3 hours ago kickass_jepsen 4d6ae0f25206 centos "/bin/sh 'while true;" 3 hours ago Exited (127) 3 hours ago pensive_chandrasekhar a8770406b896 centos "/bin/sh -c 'while tr" 3 hours ago Exited (1) 3 hours ago cranky_pasteur 7ecb2ca7d612 centos "while true;do echo h" 4 hours ago Created gigantic_lalande 6ddae878765f centos "/bin/sh -c 'while tr" 4 hours ago Exited (1) 4 hours ago serene_euler c761cee8863f docker.io/centos "/bin/bash" 6 hours ago Up 33 minutes mycentos ccfc03f90613 docker.io/centos "/bin/echo /bin/bash" 6 hours ago Exited (0) 6 hours ago elated_brown 443da0721b41 docker.io/centos "/bin/echo 'hello wor" 7 hours ago Exited (0) 7 hours ago gigantic_sinoussi
例:只查全部镜像的id
[root@docker ~]# docker ps -a -q
edfdd26e20da
f6f8844f843b
cd9cf2398630
cc93e8ced322
475cd09a2e6e
d580c6389d1e
b54ed375b3f1
a9864174cfb0
eb62a497f026
3e68d0c13bfd
4d6ae0f25206
a8770406b896
7ecb2ca7d612
6ddae878765f
c761cee8863f
ccfc03f90613
443da0721b41
查看全部运行容器的id只要去掉-a参数便可。
可使用docker rm 来删除一个出于终止状态的容器。
例:删除 reverent_yalow
[root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f6f8844f843b centos "echo hello world" 4 hours ago Exited (0) 4 hours ago reverent_yalow cd9cf2398630 centos "while true ;echo hel" 4 hours ago Created high_mclean cc93e8ced322 centos "echo hello world" 4 hours ago Exited (0) 4 hours ago stupefied_dubinsky 475cd09a2e6e centos "/bin/sh -c 'while tr" 4 hours ago Exited (1) 4 hours ago romantic_easley d580c6389d1e centos "/bin/bash -c 'while " 4 hours ago Exited (1) 4 hours ago fervent_williams b54ed375b3f1 centos "/bin/sh -c 'while tr" 4 hours ago Exited (1) 4 hours ago goofy_davinci a9864174cfb0 centos "while true; do echo " 4 hours ago Created stoic_goodall eb62a497f026 centos "/bin/sh -c 'while tr" 4 hours ago Exited (1) 4 hours ago pedantic_ritchie 3e68d0c13bfd centos "/bin/sh -c 'while tr" 4 hours ago Exited (1) 4 hours ago kickass_jepsen 4d6ae0f25206 centos "/bin/sh 'while true;" 4 hours ago Exited (127) 4 hours ago pensive_chandrasekhar a8770406b896 centos "/bin/sh -c 'while tr" 4 hours ago Exited (1) 4 hours ago cranky_pasteur 7ecb2ca7d612 centos "while true;do echo h" 5 hours ago Created gigantic_lalande 6ddae878765f centos "/bin/sh -c 'while tr" 5 hours ago Exited (1) 5 hours ago serene_euler c761cee8863f docker.io/centos "/bin/bash" 7 hours ago Up 49 minutes mycentos ccfc03f90613 docker.io/centos "/bin/echo /bin/bash" 7 hours ago Exited (0) 7 hours ago elated_brown 443da0721b41 docker.io/centos "/bin/echo 'hello wor" 7 hours ago Exited (0) 7 hours ago gigantic_sinoussi [root@docker ~]# docker rm reverent_yalow reverent_yalow [root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cd9cf2398630 centos "while true ;echo hel" 4 hours ago Created high_mclean cc93e8ced322 centos "echo hello world" 4 hours ago Exited (0) 4 hours ago stupefied_dubinsky 475cd09a2e6e centos "/bin/sh -c 'while tr" 4 hours ago Exited (1) 4 hours ago romantic_easley d580c6389d1e centos "/bin/bash -c 'while " 4 hours ago Exited (1) 4 hours ago fervent_williams b54ed375b3f1 centos "/bin/sh -c 'while tr" 4 hours ago Exited (1) 4 hours ago goofy_davinci a9864174cfb0 centos "while true; do echo " 4 hours ago Created stoic_goodall eb62a497f026 centos "/bin/sh -c 'while tr" 4 hours ago Exited (1) 4 hours ago pedantic_ritchie 3e68d0c13bfd centos "/bin/sh -c 'while tr" 4 hours ago Exited (1) 4 hours ago kickass_jepsen 4d6ae0f25206 centos "/bin/sh 'while true;" 4 hours ago Exited (127) 4 hours ago pensive_chandrasekhar a8770406b896 centos "/bin/sh -c 'while tr" 4 hours ago Exited (1) 4 hours ago cranky_pasteur 7ecb2ca7d612 centos "while true;do echo h" 5 hours ago Created gigantic_lalande 6ddae878765f centos "/bin/sh -c 'while tr" 5 hours ago Exited (1) 5 hours ago serene_euler c761cee8863f docker.io/centos "/bin/bash" 7 hours ago Up 49 minutes mycentos ccfc03f90613 docker.io/centos "/bin/echo /bin/bash" 7 hours ago Exited (0) 7 hours ago elated_brown 443da0721b41 docker.io/centos "/bin/echo 'hello wor" 7 hours ago Exited (0) 7 hours ago gigantic_sinoussi
若是删除一个正在运行中的容器,能够添加-f 参数。Docker会发送SIGKLL信号给容器。
清理全部出于终止状态的容器,能够配合刚才咱们展现的docker ps -a -q。用docker rm $(docker ps -a -q)
例:删除全部终止状态的容器
[root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cd9cf2398630 centos "while true ;echo hel" 4 hours ago Created high_mclean cc93e8ced322 centos "echo hello world" 4 hours ago Exited (0) 4 hours ago stupefied_dubinsky 475cd09a2e6e centos "/bin/sh -c 'while tr" 4 hours ago Exited (1) 4 hours ago romantic_easley d580c6389d1e centos "/bin/bash -c 'while " 4 hours ago Exited (1) 4 hours ago fervent_williams b54ed375b3f1 centos "/bin/sh -c 'while tr" 4 hours ago Exited (1) 4 hours ago goofy_davinci a9864174cfb0 centos "while true; do echo " 4 hours ago Created stoic_goodall eb62a497f026 centos "/bin/sh -c 'while tr" 4 hours ago Exited (1) 4 hours ago pedantic_ritchie 3e68d0c13bfd centos "/bin/sh -c 'while tr" 4 hours ago Exited (1) 4 hours ago kickass_jepsen 4d6ae0f25206 centos "/bin/sh 'while true;" 4 hours ago Exited (127) 4 hours ago pensive_chandrasekhar a8770406b896 centos "/bin/sh -c 'while tr" 4 hours ago Exited (1) 4 hours ago cranky_pasteur 7ecb2ca7d612 centos "while true;do echo h" 5 hours ago Created gigantic_lalande 6ddae878765f centos "/bin/sh -c 'while tr" 5 hours ago Exited (1) 5 hours ago serene_euler c761cee8863f docker.io/centos "/bin/bash" 7 hours ago Up About an hour mycentos ccfc03f90613 docker.io/centos "/bin/echo /bin/bash" 7 hours ago Exited (0) 7 hours ago elated_brown 443da0721b41 docker.io/centos "/bin/echo 'hello wor" 7 hours ago Exited (0) 7 hours ago gigantic_sinoussi [root@docker ~]# docker rm $(docker ps -a -q) cd9cf2398630 cc93e8ced322 475cd09a2e6e d580c6389d1e b54ed375b3f1 a9864174cfb0 eb62a497f026 3e68d0c13bfd 4d6ae0f25206 a8770406b896 7ecb2ca7d612 6ddae878765f ccfc03f90613 443da0721b41 Failed to remove container (c761cee8863f): Error response from daemon: Conflict, You cannot remove a running container. Stop the container before attempting removal or use -f [root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c761cee8863f docker.io/centos "/bin/bash" 7 hours ago Up About an hour mycentos
这里还有一个测试时好用的参数
docker run --rm [选项] [镜像名称] [命令]
使用 --rm参数在咱们使用完这个容器,关闭就会自动删除,省时省力,固然若是还须要在测试,就从新启动一个容器,也符合docker不可变基础设施理念。
例:建立一个测试容器,退出后自动给删除
[root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c761cee8863f docker.io/centos "/bin/bash" 7 hours ago Up About an hour mycentos [root@docker ~]# docker run --rm -it --name ceshi centos /bin/bash [root@e392d10516b0 /]# exit [root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c761cee8863f docker.io/centos "/bin/bash" 7 hours ago Up About an hour mycentos
目前先分享到这里,因本人报名赵班长的架构师课程开班。容器和建立镜像在之后的文档中会再有更加详细的介绍。