入狱丶
未曾开言我先笑场, 笑场完了听我诉一诉衷肠。

 

1、安装docker

1.1、安装环境

[root@docker ~]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 
[root@docker ~]# uname -r
3.10.0-327.el7.x86_64
安装环境

1.2、安装docker

yum install docker -y
安装docker

1.3、查看安装结果

[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
查看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 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.
查看docker状态

 2.3、关闭docker

systemctl stop docker
关闭docker

2.4、查看本地网络

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
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.                        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]
查看仓库镜像

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        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
导出镜像

 

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        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
查看已有镜像

 在列出的信息中,可以看到几个字段的信息

  • 来自于那个仓库(REPOSITORY)
  • 镜像标记(TAG)
  • 它的ID号(唯一)
  • 创建时间
  • 镜像大小

3.1.6删除镜像

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")
删除未打标签的本地镜像

 

 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"   16 seconds ago      Exited (0) 11 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         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在后台运行的标准操作包括:

  • 检查本地是否存在 指定的镜像,不存在就从仓库下载。
  • 利用镜像创建并启动一个镜像
  • 分配一个文件系统,并在只读的镜像层外面挂载一层可读写层。
  • 从宿主主机配置的网桥接口中桥接一个虚拟接口到容器中去
  • 从地址池配置一个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"              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
启动mycentos
注:有的人想在容器起来的时候在执行其他的东西。这是不推荐的因为docker的理念,不可变基础设施,不要修改容器(前面有讲解),再重新创建一个就可以了,已改动就可变了。

 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
10656
[root@docker ~]# nsenter -t 10656 -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         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
nsenter进入容器查看进程

 其中/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
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         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
执行过程

 

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         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
不进入容器操作
进入容器方法
docker exec -it [容器名称|id] /bin/bash
例:使用docker exec 进入容器
[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 /]# 
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"         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

查看所有运行容器的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"       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
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"   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
测试

 


 

目前先分享到这里,因本人报名赵班长的架构师课程开班。容器和创建镜像在以后的文档中会再有更加详细的介绍。

 

posted on 2016-12-05 10:53  入狱丶  阅读(3825)  评论(0编辑  收藏  举报