[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
目前先分享到這裏,因本人報名趙班長的架構師課程開班。容器和建立鏡像在之後的文檔中會再有更加詳細的介紹。