执行docker commit前的备忘录

环境 –

在VirtualBox上安装CentOS 6.5。

安装Docker

# yum install docker-io
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.tsukuba.wide.ad.jp
 * extras: ftp.tsukuba.wide.ad.jp
 * updates: centos.tt.co.kr
Setting up Install Process
No package docker-io available.
Error: Nothing to do

按照文件的要求安装EPEL并进行再次尝试。

# rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
Retrieving http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
warning: /var/tmp/rpm-tmp.SXgnRV: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing...                ########################################### [100%]
   1:epel-release           ########################################### [100%]

# yum install docker-io

# docker -v
Docker version 1.3.2, build 39fa2fa/1.3.2

# service docker start
Starting cgconfig service:                                 [  OK  ]
Starting docker:                                       [  OK  ]

用root用户创建容器。

这个地方很容易理解。结果来看,在后续的操作中,我觉得还是应该允许一般用户进行以下的操作。

# docker run centos /bin/echo "hello world"
Unable to find image 'centos' locally
centos:latest: The image you are pulling has been verified
511136ea3c5a: Pull complete 
5b12ef8fd570: Pull complete 
34943839435d: Pull complete 
Status: Downloaded newer image for centos:latest
hello world

# docker run centos /bin/cat /etc/redhat-release 
CentOS Linux release 7.0.1406 (Core) 

我动了。

尝试从Docker Hub上拉取镜像。

# docker pull centos:centos6

# docker run centos:centos6 /bin/cat /etc/redhat-release 
CentOS release 6.6 (Final)

启动容器

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              centos6             25c5298b1a36        2 weeks ago         215.8 MB
centos              latest              34943839435d        2 weeks ago         224 MB

# docker run --name="intro" -t  -i -d 25c5 /bin/bash
722f3a7dad6897d9749ac93b979ae8d8c420220af8728bc326cc76f32ae857aa

# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
722f3a7dad68        centos:centos6      "/bin/bash"         14 seconds ago      Up 13 seconds                           intro       

# docker attach 722f3a7dad68
[root@722f3a7dad68 /]# 

保存修改

进行修改。例如尝试添加用户。

[root@722f3a7dad68 /]# useradd myuser
[root@722f3a7dad68 /]# tail -1 /etc/passwd
myuser:x:500:500::/home/myuser:/bin/bash

按下Ctrl+P和Ctrl+Q进行分离。不是退出(exit)。之后提交(commit)。

# docker commit -m "useradd" 722f3a7dad68 mycontainer 
4d15b0eac3b0f2e41b607fef77b6bccf507f709cf8d4453d6e8c44e20e4a877a

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
mycontainer         latest              4d15b0eac3b0        47 seconds ago      215.9 MB
centos              centos6             25c5298b1a36        2 weeks ago         215.8 MB
centos              latest              34943839435d        2 weeks ago         224 MB

我的容器的新映像4d15b0eac3b0已经创建完毕,或许名字(mycontainer)取得太随意了。

如果不保存更改

再次附加并添加另一个用户。

[root@722f3a7dad68 /]# useradd myuser2
[root@722f3a7dad68 /]# tail -2  /etc/passwd
myuser:x:500:500::/home/myuser:/bin/bash
myuser2:x:501:501::/home/myuser2:/bin/bash

从保存的镜像中启动容器,然后分离

# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
722f3a7dad68        centos:centos6      "/bin/bash"         50 minutes ago      Up 50 minutes                           intro  

# docker run --name="intro2" -t  -i -d 4d /bin/bash
4942e5a4d043209032b0c2e76d6cbadfbc3d63f5b6bfaa2e4cf5dda6108d8997

# docker ps
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS              PORTS               NAMES
4942e5a4d043        mycontainer:latest   "/bin/bash"         13 seconds ago      Up 12 seconds                           intro2              
722f3a7dad68        centos:centos6       "/bin/bash"         52 minutes ago      Up 51 minutes                           intro    

# docker attach 49
[root@4942e5a4d043 /]# tail -2 /etc/passwd
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
myuser:x:500:500::/home/myuser:/bin/bash

从已保存的更改点重新开始。(保存后没有添加第二个用户)

因为弄乱了,所以要整理集装箱。

# docker ps
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS              PORTS               NAMES
4942e5a4d043        mycontainer:latest   "/bin/bash"         5 minutes ago       Up 5 minutes                            intro2              
722f3a7dad68        centos:centos6       "/bin/bash"         56 minutes ago      Up 56 minutes                           intro        

# docker stop 4942e5a4d043
4942e5a4d043

# docker stop 722f3a7dad68
722f3a7dad68

# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

由于停止仍然残留,因此执行rm命令。

# docker ps -a
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS                      PORTS               NAMES
4942e5a4d043        mycontainer:latest   "/bin/bash"         14 minutes ago      Exited (-1) 8 minutes ago                       intro2              
722f3a7dad68        centos:centos6       "/bin/bash"         About an hour ago   Exited (-1) 8 minutes ago                       intro       

# docker rm 4942e5a4d043
4942e5a4d043

# docker rm 722f3a7dad68
722f3a7dad68

# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

整理图像

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
mycontainer         latest              4d15b0eac3b0        50 minutes ago      215.9 MB
centos              centos6             25c5298b1a36        2 weeks ago         215.8 MB
centos              latest              34943839435d        2 weeks ago         224 MB

# docker rmi 4d15b0eac3b0
Untagged: mycontainer:latest
Deleted: 4d15b0eac3b0f2e41b607fef77b6bccf507f709cf8d4453d6e8c44e20e4a877a

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              centos6             25c5298b1a36        2 weeks ago         215.8 MB
centos              latest              34943839435d        2 weeks ago         224 MB

让普通用户试试看

按照文件的要求,将普通用户添加到docker组中。

$ sudo usermod -a -G docker satzz


$ sudo vi /etc/sysconfig/docker
# /etc/sysconfig/docker
#
# Other arguments to pass to the docker daemon process
# These will be parsed by the sysv initscript and appended
# to the arguments list passed to docker -d

#other_args=
other_args="-H 127.0.0.1:4243" # add


$ export DOCKER_HOST=tcp://127.0.0.1:4243

$ docker pull centos:centos6
centos:centos6: The image you are pulling has been verified
511136ea3c5a: Already exists 
5b12ef8fd570: Already exists 
25c5298b1a36: Already exists 
Status: Image is up to date for centos:centos6

有现有的图像可用,DOCKER_HOST应该写在.bashrc等文件中。

$ docker run --name="intro" -t  -i -d 25c5 /bin/bash
0c890bd6d900ae4cc609fb85b7ea36ba91b93bd96bd8c6628f8aa717b4dcdd2c

$ docker attach 0c890bd6d900ae4cc609fb85b7ea36ba91b93bd96bd8c6628f8aa717b4dcdd2c
[root@0c890bd6d900 /]# 

我能够运行并附加。

如果选择exit而不是detach会发生什么?

$ docker run --name="intro" -t  -i -d 25c5 /bin/bash
3b2d8ffaaac414f3a5e96fddb2ff7303be8c6db5a615cca2f81b9cabde2d7410

$ docker attach 3b2d8ffaaac414f3a5e96fddb2ff7303be8c6db5a615cca2f81b9cabde2d7410

[root@3b2d8ffaaac4 /]# exit
exit

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
3b2d8ffaaac4        centos:centos6      "/bin/bash"         15 seconds ago      Exited (0) 3 seconds ago                       intro

$ docker attach 3b2d8ffaaac4
2014/12/18 18:49:59 You cannot attach to a stopped container, start it first

STATUS从UP变为Exited。由于它没有运行,无法进行附加操作,但只要启动它,就可以进行附加操作。

$ docker start 3b2d8ffaaac4
3b2d8ffaaac4

$ docker attach 3b2d8ffaaac4
[root@3b2d8ffaaac4 /]#