1、安装docker
1.1、安装环境
[root@docker ~]# cat /etc/redhat-release
CentOS Linux release 7.2. (Core)
[root@docker ~]# uname -r
3.10.-.el7.x86_64
安装环境
1.2、安装docker
yum install docker -y
安装docker
1.3、查看安装结果
[root@docker ~]# rpm -qa docker
docker-1.10.-.el7.centos..x86_64
[root@docker ~]# docker --version
Docker version 1.10., build cb079f6-unsupported
查看docker版本
我们可以通过官方的yum源安装docker1.12,或在docker官网下载最新版本。
Docker官网:http://www.docker.com/
注:使用docker目前使用的越新越好,因为docker一直都在不足,每次更新都更加的完善。
2、启动docker
2.1、启动docker镜像
[root@docker ~]# systemctl start docker
启动docker
2.2、查看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 -- :: CST; 13s ago
Docs: http://docs.docker.com
Main PID: (docker-current)
CGroup: /system.slice/docker.service
└─ /usr/bin/docker-current daemon --exec-opt native.cgroupdriver=systemd --selinux-enabled --log-driver=journald Dec :: docker docker-current[]: time="2016-12-01T20:17:41.379210415+08:00" level=warning msg="Docker could not enable SELinux on the host system"
Dec :: docker docker-current[]: time="2016-12-01T20:17:41.528222902+08:00" level=info msg="Graph migration to content-addressability took 0.01 seconds"
Dec :: docker docker-current[]: time="2016-12-01T20:17:42.301945116+08:00" level=info msg="Firewalld running: true"
Dec :: docker docker-current[]: 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 :: docker docker-current[]: time="2016-12-01T20:17:44.395349153+08:00" level=info msg="Loading containers: start."
Dec :: docker docker-current[]: time="2016-12-01T20:17:44.395640410+08:00" level=info msg="Loading containers: done."
Dec :: docker docker-current[]: time="2016-12-01T20:17:44.395690801+08:00" level=info msg="Daemon has completed initialization"
Dec :: docker docker-current[]: time="2016-12-01T20:17:44.395765160+08:00" level=info msg="Docker daemon" commit=cb079f6-unsupported execdriver=native-...sion=1.10.
Dec :: docker systemd[]: Started Docker Application Container Engine.
Dec :: docker docker-current[]: 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.
查看docker状态
2.3、关闭docker
systemctl stop docker
关闭docker
2.4、查看本地网络
ifconfig
docker0: flags=<UP,BROADCAST,MULTICAST> mtu
inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
ether :::c2:8e: txqueuelen (Ethernet)
RX packets bytes (0.0 B)
RX errors dropped overruns frame
TX packets bytes (0.0 B)
TX errors dropped overruns carrier collisions eno16777736: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu
inet 10.0.0.129 netmask 255.255.255.0 broadcast 10.0.0.255
ether :0c::1c:0e: txqueuelen (Ethernet)
RX packets bytes (16.2 MiB)
RX errors dropped overruns frame
TX packets bytes (1.7 MiB)
TX errors dropped overruns carrier collisions lo: flags=<UP,LOOPBACK,RUNNING> mtu
inet 127.0.0.1 netmask 255.0.0.0
inet6 :: prefixlen scopeid 0x10<host>
loop txqueuelen (Local Loopback)
RX packets bytes (200.0 B)
RX errors dropped overruns frame
TX packets bytes (200.0 B)
TX errors dropped overruns carrier collisions
ifconfig
docker启动后,会生成一个docker:0的网卡(虚拟机也会生成)。
3、基础命令讲解
下面我将以docker的三大组件,分别讲解命令。
- 镜像
- 容器
- 仓库
3.1、镜像
docker 运行容器前需要本地存在对应的镜像,如果镜像本地不存在,docker会从镜像仓库下载(默认从Docker Hub仓库下载)。
3.1.1、搜索
docker search [镜像名称]
例:查看dockerhub中nginx的镜像。
[root@docker ~]# docker search nginx
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/nginx Official build of Nginx. [OK]
docker.io docker.io/jwilder/nginx-proxy Automated Nginx reverse proxy for docker c... [OK]
docker.io docker.io/richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable ... [OK]
docker.io docker.io/million12/nginx-php Nginx + PHP-FPM 5.5, 5.6, 7.0 (NG), CentOS... [OK]
docker.io docker.io/webdevops/php-nginx Nginx with PHP-FPM [OK]
docker.io docker.io/maxexcloo/nginx-php Framework container with nginx and PHP-FPM... [OK]
docker.io docker.io/bitnami/nginx Bitnami nginx Docker Image [OK]
docker.io docker.io/evild/alpine-nginx Minimalistic Docker image with Nginx [OK]
docker.io docker.io/gists/nginx Nginx on Alpine [OK]
docker.io docker.io/maxexcloo/nginx Framework container with nginx installed. [OK]
docker.io docker.io/webdevops/nginx Nginx container [OK]
docker.io docker.io/1science/nginx Nginx Docker images that include Consul Te... [OK]
docker.io docker.io/drupaldocker/nginx NGINX for Drupal [OK]
docker.io docker.io/ixbox/nginx Nginx on Alpine Linux. [OK]
docker.io docker.io/blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. [OK]
docker.io docker.io/dock0/nginx Arch container running nginx [OK]
docker.io docker.io/frekele/nginx docker run --rm --name nginx -p : -p ... [OK]
docker.io docker.io/servivum/nginx Nginx Docker Image with Useful Tools [OK]
docker.io docker.io/xataz/nginx Light nginx image [OK]
docker.io docker.io/yfix/nginx Yfix own build of the nginx-extras package [OK]
docker.io docker.io/radial/nginx Spoke container for Nginx, a high performa... [OK]
docker.io docker.io/tozd/nginx Dockerized nginx. [OK]
docker.io docker.io/c4tech/nginx Several nginx images for web applications. [OK]
docker.io docker.io/unblibraries/nginx Baseline non-PHP nginx container [OK]
docker.io docker.io/watsco/nginx nginx:1.11-alpine [OK]
查看仓库镜像
3.1.2、下载
docker pull [镜像名称]
例:下载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
下载镜像
3.1.3 导出镜像
docker save -o [镜像包名] [镜像名称]
例:导出centos基础镜像为centos.tar
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
oldboy/nginx1 latest c436de4b837a days ago 401.1 MB
<none> <none> 6ab373e100c9 days ago 181.4 MB
oldboy/nginx v1 99695fe97fb5 days ago 181.4 MB
docker.io/nginx latest 05a60462f8ba weeks ago 181.4 MB
docker.io/centos latest 0584b3d2cf6d weeks ago 196.5 MB
[root@docker ~]# docker save -o centos.tar docker.io/centos
[root@docker ~]# ll -h centos.tar
-rw-r--r-- root root 195M Dec : centos.tar
导出镜像
3.1.4、导入镜像
docker load --input [镜像包名]
docker load <[镜像包名]
3.1.5 查看已有镜像
docker images
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
oldboy/nginx1 latest c436de4b837a days ago 401.1 MB
oldboy/nginx v1 99695fe97fb5 days ago 181.4 MB
docker.io/nginx latest 05a60462f8ba weeks ago 181.4 MB
docker.io/centos latest 0584b3d2cf6d weeks ago 196.5 MB
查看已有镜像
在列出的信息中,可以看到几个字段的信息
- 来自于那个仓库(REPOSITORY)
- 镜像标记(TAG)
- 它的ID号(唯一)
- 创建时间
- 镜像大小
3.1.6删除镜像
docker rni [镜像id]
例:删除一个镜像
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
oldboy/nginx1 latest c436de4b837a days ago 401.1 MB
<none> <none> 6ab373e100c9 days ago 181.4 MB
oldboy/nginx v1 99695fe97fb5 days ago 181.4 MB
docker.io/nginx latest 05a60462f8ba weeks ago 181.4 MB
docker.io/centos latest 0584b3d2cf6d 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 days ago 401.1 MB
oldboy/nginx v1 99695fe97fb5 days ago 181.4 MB
docker.io/nginx latest 05a60462f8ba weeks ago 181.4 MB
docker.io/centos latest 0584b3d2cf6d 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")
删除未打标签的本地镜像
3.1.7创建镜像
创建镜像为较为复杂点,在之后的文档中会详细介绍。
3.2容器
容器是独立运行的一个或一组应用,以及他们的运行环境。对应的虚拟机可以理解为模拟运行的一套操作系统(提供了运行环境和其他其系统环境)和跑在上面的应用。
3.2.1、启动容器
启动容器的方式
- 基于镜像新建一个容器并启动。
- 将终止状态的容器重新启动。
容器太轻量了,很多时候用户都是随时删除和新创建容器。
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" seconds ago Exited () seconds ago gigantic_sinoussi 因为没有指定容器名称,所以他自己自动生成了gigantic_sinoussi
hello world
例:启动一个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 0.0 0.0 ? Ss : : /bin/bash
root 0.0 0.0 ? R+ : : ps aux
[root@c761cee8863f /]# free -m
total used free shared buff/cache available
Mem:
Swap:
[root@c761cee8863f /]# exit
[root@docker ~]# free -m
total used free shared buff/cache available
Mem:
Swap: 容器不是一个虚拟机,他只是一个进程,pid为1的进程结束了,这个容器就挂了,他就是为了隔离这个进程而存在的。 docker的隔离性并不是很好,在物理机可以看到服务器的硬件信息,docker本身不能解决,但是可以用一些黑科技解决,当然如果自己用的话也可以不用解决这个问题。
交互模式下的进程及内存
docker run来创建容器时,docker在后台运行的标准操作包括:
- 检查本地是否存在 指定的镜像,不存在就从仓库下载。
- 利用镜像创建并启动一个镜像
- 分配一个文件系统,并在只读的镜像层外面挂载一层可读写层。
- 从宿主主机配置的网桥接口中桥接一个虚拟接口到容器中去
- 从地址池配置一个ip地址给容器
- 执行用户指定的应用程序
- 执行完毕后容器被终止
3.2.2、启动已经终止的容器
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" minutes ago Exited () minutes ago mycentos
ccfc03f90613 docker.io/centos "/bin/echo /bin/bash" minutes ago Exited () minutes ago elated_brown
443da0721b41 docker.io/centos "/bin/echo 'hello wor" minutes ago Exited () 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" minutes ago Up seconds mycentos
启动mycentos
3.2.3、后台运行
更多的时候需要让Docker 在后台运行而不是直接执行命令的结果输出在当前宿主机下,此时,可以通过添加 -d 参数来实现。
例:如果不使用-d参数运行容器
[root@docker ~]# docker run centos echo hello world
hello world
不使用-d参数
容器会把输出结果打印到宿主机上
例:使用-d参数
[root@docker ~]# docker run -d centos echo hello world
f6f8844f843b6479ab2d9faf52aef55a8c9516effa1dc7574299989025f24a41
[root@docker ~]# docker logs f6f8844f843b6479ab2d9faf52aef55a8c9516effa1dc7574299989025f24a41
hello world
使用-d参数
这时候容器会在后台运行并不会把输出结果(STDOUT)打印到宿主机上面(输出结果可以用docker logs查看)
注:容器是否会长久运行,是和docker run指定的
3.2.3、进入镜像
3.2.3.1、attach
docker attach [镜像名称]
这个登录相当于单用户模式,只能有一个人登录。一般生产不推荐使用此法。
例:一方操作另一方会自动操作。
3.2.3.2、 nsenter
生产场景是不使用docker attach
的,需要我们使用nsenter
这个工具,这个工具包含在util-linux
软件包里面。nsenter可以访问另一个进程的名字空间。nsenter要正常工作需要有root权限。
[root@docker ~]# yum install util-linux -y
util
一般情况下最小化安装里都已经安装完毕。
我们通过nsenter
就可以进入容器
,但是nsenter是通过容器第一个进程的pid
进入容器里,所以我们需要知道容器的pid。我们可以通过docker inspect
来获取到pid
利用docker inspect 获得PID号
[root@docker ~]# docker start mycentos
mycentos
[root@docker ~]# docker inspect -f "{{ .State.Pid }}" mycentos [root@docker ~]# nsenter -t -m -u -i -n -p
[root@c761cee8863f /]#
nsenter进入容器
那个参数选项是简写全称的就是:
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 0.0 0.0 ? Ss+ : : /bin/bash
root 0.0 0.0 ? S : : -bash
root 0.0 0.0 ? R+ : : ps aux
nsenter进入容器查看进程
其中/bin/bash 是我们开启容器运行的第一个进程
-bash 是nsenter 启动时生成的进程,这样我们退出后,容器不会退出,仍然会运行。
我们不能每次都要执行上面的命令,找到PID 在进入再次我们需要写一个脚本
cat cat docker_in.sh
#!/bin/bash
# Use nsenter to access docker
docker_in(){
NAME_ID=$
PID=$(docker inspect -f "{{ .State.Pid }}" $NAME_ID)
nsenter -t $PID -m -u -i -n -p
}
docker_in $
cat docker_in.sh
执行结果
[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 0.0 0.0 ? Ss+ : : /bin/bash
root 0.0 0.0 ? S : : -bash
root 0.0 0.0 ? R+ : : ps aux
执行过程
3.2.3.3、docker exec
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 0.0 0.0 ? Ss+ : : /bin/bash
root 0.0 0.0 ? Rs : : ps aux
不进入容器操作
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c761cee8863f docker.io/centos "/bin/bash" hours ago Up minutes mycentos
[root@docker ~]# docker exec -it mycentos /bin/bash
[root@c761cee8863f /]# exit
[root@docker ~]# docker exec -it c761cee8863f /bin/bash
[root@c761cee8863f /]#
exec 进入容器
和 nsenter 实现原理不一样 实现结果一样的
3.2.4、查看容器列表
docker ps
例:查看正在运行的容器
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c761cee8863f docker.io/centos "/bin/bash" hours ago Up minutes mycentos
正在运行容器
例:查看所有容器
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
edfdd26e20da centos "echo hello world" hours ago Exited () hours ago kickass_heisenberg
f6f8844f843b centos "echo hello world" hours ago Exited () hours ago reverent_yalow
cd9cf2398630 centos "while true ;echo hel" hours ago Created high_mclean
cc93e8ced322 centos "echo hello world" hours ago Exited () hours ago stupefied_dubinsky
475cd09a2e6e centos "/bin/sh -c 'while tr" hours ago Exited () hours ago romantic_easley
d580c6389d1e centos "/bin/bash -c 'while " hours ago Exited () hours ago fervent_williams
b54ed375b3f1 centos "/bin/sh -c 'while tr" hours ago Exited () hours ago goofy_davinci
a9864174cfb0 centos "while true; do echo " hours ago Created stoic_goodall
eb62a497f026 centos "/bin/sh -c 'while tr" hours ago Exited () hours ago pedantic_ritchie
3e68d0c13bfd centos "/bin/sh -c 'while tr" hours ago Exited () hours ago kickass_jepsen
4d6ae0f25206 centos "/bin/sh 'while true;" hours ago Exited () hours ago pensive_chandrasekhar
a8770406b896 centos "/bin/sh -c 'while tr" hours ago Exited () hours ago cranky_pasteur
7ecb2ca7d612 centos "while true;do echo h" hours ago Created gigantic_lalande
6ddae878765f centos "/bin/sh -c 'while tr" hours ago Exited () hours ago serene_euler
c761cee8863f docker.io/centos "/bin/bash" hours ago Up minutes mycentos
ccfc03f90613 docker.io/centos "/bin/echo /bin/bash" hours ago Exited () hours ago elated_brown
443da0721b41 docker.io/centos "/bin/echo 'hello wor" hours ago Exited () 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
查看所有运行容器的id只要去掉-a参数即可。
3.2.5、容器删除
可以使用docker rm 来删除一个出于终止状态的容器。
例:删除 reverent_yalow
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f6f8844f843b centos "echo hello world" hours ago Exited () hours ago reverent_yalow
cd9cf2398630 centos "while true ;echo hel" hours ago Created high_mclean
cc93e8ced322 centos "echo hello world" hours ago Exited () hours ago stupefied_dubinsky
475cd09a2e6e centos "/bin/sh -c 'while tr" hours ago Exited () hours ago romantic_easley
d580c6389d1e centos "/bin/bash -c 'while " hours ago Exited () hours ago fervent_williams
b54ed375b3f1 centos "/bin/sh -c 'while tr" hours ago Exited () hours ago goofy_davinci
a9864174cfb0 centos "while true; do echo " hours ago Created stoic_goodall
eb62a497f026 centos "/bin/sh -c 'while tr" hours ago Exited () hours ago pedantic_ritchie
3e68d0c13bfd centos "/bin/sh -c 'while tr" hours ago Exited () hours ago kickass_jepsen
4d6ae0f25206 centos "/bin/sh 'while true;" hours ago Exited () hours ago pensive_chandrasekhar
a8770406b896 centos "/bin/sh -c 'while tr" hours ago Exited () hours ago cranky_pasteur
7ecb2ca7d612 centos "while true;do echo h" hours ago Created gigantic_lalande
6ddae878765f centos "/bin/sh -c 'while tr" hours ago Exited () hours ago serene_euler
c761cee8863f docker.io/centos "/bin/bash" hours ago Up minutes mycentos
ccfc03f90613 docker.io/centos "/bin/echo /bin/bash" hours ago Exited () hours ago elated_brown
443da0721b41 docker.io/centos "/bin/echo 'hello wor" hours ago Exited () 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" hours ago Created high_mclean
cc93e8ced322 centos "echo hello world" hours ago Exited () hours ago stupefied_dubinsky
475cd09a2e6e centos "/bin/sh -c 'while tr" hours ago Exited () hours ago romantic_easley
d580c6389d1e centos "/bin/bash -c 'while " hours ago Exited () hours ago fervent_williams
b54ed375b3f1 centos "/bin/sh -c 'while tr" hours ago Exited () hours ago goofy_davinci
a9864174cfb0 centos "while true; do echo " hours ago Created stoic_goodall
eb62a497f026 centos "/bin/sh -c 'while tr" hours ago Exited () hours ago pedantic_ritchie
3e68d0c13bfd centos "/bin/sh -c 'while tr" hours ago Exited () hours ago kickass_jepsen
4d6ae0f25206 centos "/bin/sh 'while true;" hours ago Exited () hours ago pensive_chandrasekhar
a8770406b896 centos "/bin/sh -c 'while tr" hours ago Exited () hours ago cranky_pasteur
7ecb2ca7d612 centos "while true;do echo h" hours ago Created gigantic_lalande
6ddae878765f centos "/bin/sh -c 'while tr" hours ago Exited () hours ago serene_euler
c761cee8863f docker.io/centos "/bin/bash" hours ago Up minutes mycentos
ccfc03f90613 docker.io/centos "/bin/echo /bin/bash" hours ago Exited () hours ago elated_brown
443da0721b41 docker.io/centos "/bin/echo 'hello wor" hours ago Exited () hours ago gigantic_sinoussi
reverent_yalow
如果删除一个正在运行中的容器,可以添加-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" hours ago Created high_mclean
cc93e8ced322 centos "echo hello world" hours ago Exited () hours ago stupefied_dubinsky
475cd09a2e6e centos "/bin/sh -c 'while tr" hours ago Exited () hours ago romantic_easley
d580c6389d1e centos "/bin/bash -c 'while " hours ago Exited () hours ago fervent_williams
b54ed375b3f1 centos "/bin/sh -c 'while tr" hours ago Exited () hours ago goofy_davinci
a9864174cfb0 centos "while true; do echo " hours ago Created stoic_goodall
eb62a497f026 centos "/bin/sh -c 'while tr" hours ago Exited () hours ago pedantic_ritchie
3e68d0c13bfd centos "/bin/sh -c 'while tr" hours ago Exited () hours ago kickass_jepsen
4d6ae0f25206 centos "/bin/sh 'while true;" hours ago Exited () hours ago pensive_chandrasekhar
a8770406b896 centos "/bin/sh -c 'while tr" hours ago Exited () hours ago cranky_pasteur
7ecb2ca7d612 centos "while true;do echo h" hours ago Created gigantic_lalande
6ddae878765f centos "/bin/sh -c 'while tr" hours ago Exited () hours ago serene_euler
c761cee8863f docker.io/centos "/bin/bash" hours ago Up About an hour mycentos
ccfc03f90613 docker.io/centos "/bin/echo /bin/bash" hours ago Exited () hours ago elated_brown
443da0721b41 docker.io/centos "/bin/echo 'hello wor" hours ago Exited () 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" 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" 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" hours ago Up About an hour mycentos
测试
目前先分享到这里,因本人报名赵班长的架构师课程开班。容器和创建镜像在以后的文档中会再有更加详细的介绍。