1. 程式人生 > >Docke 1.12 基礎篇:48條命令(5)

Docke 1.12 基礎篇:48條命令(5)

本文針對Docker1.12的48條命令,列出了container相關的18條命令,上一篇文章我們學習了容器生狀態管理的幾條命令,這這一篇中我們來看一下剩下的9條命令都能做什麼。

container相關的命令

項番 命令 詳解
No.1 attach Attach to a running container
No.9 exec Run a command in a running container
No.3 commit Create a new image from a container’s changes
No.4 cp Copy files/folders between a container and the local filesystem
No.7 diff Inspect changes on a container’s filesystem
No.10 export Export a container’s filesystem as a tar archive
No.13 import Import the contents from a tarball to create a filesystem image
No.29 rename Rename a container
No.31 rm Remove one or more containers

事前準備

先啟動一個container準備用來霍霍。

[root@liumiaocn ~]# docker run --name=nginx -d -p80:80 nginx
46be04227f8ae236ec47acf0dcb20e65389cb88e6d724439d6305813ce6b7f4e
[root@liumiaocn ~]# docker ps
CONTAINER ID        IMAGE               COMMAND
CREATED STATUS PORTS NAMES 46be04227f8a nginx "nginx -g 'daemon off" 2 seconds ago Up 1 seconds 0.0.0.0:80->80/tcp, 443/tcp nginx [root@liumiaocn ~]#

exec

通過exec可以連線到一個正在執行的container執行一條命令,比如

[root@liumiaocn ~]# docker exec nginx hostname
46be04227f8a
[root@liumiaocn ~]#

在實際中下面的方法用的更多,類似於連線到一個container中,然後可以執行各種操作

[root@liumiaocn ~]# docker exec -it nginx /bin/bash
root@46be04227f8a:/# hostname
46be04227f8a
root@46be04227f8a:/# pwd
/
root@46be04227f8a:/# exit
exit
[root@liumiaocn ~]#

退出後也不會有副作用

[root@liumiaocn ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
46be04227f8a        nginx               "nginx -g 'daemon off"   2 minutes ago       Up About a minute   0.0.0.0:80->80/tcp, 443/tcp   nginx
[root@liumiaocn ~]#

attach

attach到一個正在執行的container上,可以看到現在container的狀況,按ctrl+C退出會伴隨著container的退出,

[root@liumiaocn ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
46be04227f8a        nginx               "nginx -g 'daemon off"   2 minutes ago       Up 2 minutes        0.0.0.0:80->80/tcp, 443/tcp   nginx
[root@liumiaocn ~]# docker attach nginx
^C[root@liumiaocn ~]#
[root@liumiaocn ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@liumiaocn ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
46be04227f8a        nginx               "nginx -g 'daemon off"   3 minutes ago       Exited (0) 7 seconds ago                       nginx
[root@liumiaocn ~]#

cp

docker cp用於將本地檔案copy到某容器內
在host上建立一個helloworld.txt的檔案

[root@liumiaocn ~]# echo "Hello world" >/tmp/helloworld.txt

啟動一個busybox的container,確認該container的/tmp下什麼也沒有

[root@liumiaocn ~]# docker run --name busybox -it busybox /bin/sh
/ # cd /tmp
/tmp # ls -l
total 0
/tmp #

docker cp

[root@liumiaocn ~]# docker cp /tmp/helloworld.txt busybox:/tmp
[root@liumiaocn ~]#

確認container中的結果

/tmp # ls -l /tmp
total 4
-rw-r--r--    1 root     root            12 Sep 24 06:41 helloworld.txt
/tmp # cat /tmp/helloworld.txt
Hello world
/tmp #
[root@liumiaocn ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
44f08d7f4732        busybox             "/bin/sh"           6 minutes ago       Up 6 minutes                            busybox
[root@liumiaocn ~]#

diff

docker是分層的,由映象檔案建立成容器,比如我們在docker cp中建立的container(44f08d7f4732),它有什麼變化呢,至少我們把helloworld.txt給copy到該container
的/tmp下了,我們怎樣才能看到這個差別呢

[root@liumiaocn ~]# docker diff busybox
C /root
A /root/.ash_history
C /tmp
A /tmp/helloworld.txt
[root@liumiaocn ~]#

commit

但是當我們在這個時候同時在一個新的終端啟動一個busybox的container的時候,這個新的container的/tmp下什麼也沒有.

[root@liumiaocn ~]# docker run -it busybox /bin/sh
/ # ls /tmp
/ # exit
[root@liumiaocn ~]#

為了證明這不是幻覺,使用ps -a來看一下剛才這個container是的確存在過的,只是我們退出了而已

[root@liumiaocn ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
63d08b54575c        busybox             "/bin/sh"           20 seconds ago      Exited (0) 14 seconds ago                       angry_northcutt
44f08d7f4732        busybox             "/bin/sh"           10 minutes ago      Up 10 minutes                                   busybox
[root@liumiaocn ~]#

使用commit就可以解決這個問題了,把當前的變更儲存到映象中,在/tmp下顯示一個檔案不是什麼好的例子,但是如果在一個Ubuntu的Base上
安裝完JDK做一次commit,在安裝完JDK之後的版本上再安裝Tomcat再做一次commit,是不是你就可以得到了三個版本的映象了,當然更多的做法是通過
dockerfile來實現的,但是commit倒也簡單有效。

[root@liumiaocn ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
44f08d7f4732        busybox             "/bin/sh"           15 minutes ago      Up 15 minutes                           busybox
[root@liumiaocn ~]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
busybox                   latest              2b8fd9751c4c        3 months ago        1.093 MB
74th/redmine-all-in-one   latest              cbd38a5bb23d        7 months ago        1.515 GB
[root@liumiaocn ~]# docker commit 44f08d7f4732 busybox:v2
sha256:185ccb97f18acf478b9a3a63964fcab96d2f2cc753e6ec16341013bbc0daa342
[root@liumiaocn ~]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
busybox                   v2                  185ccb97f18a        20 seconds ago      1.093 MB
busybox                   latest              2b8fd9751c4c        3 months ago        1.093 MB
74th/redmine-all-in-one   latest              cbd38a5bb23d        7 months ago        1.515 GB
[root@liumiaocn ~]#

使用這個新生成的映象busybox:v2啟動一下確認/tmp下確實存在剛才的變化內容。

[root@liumiaocn ~]# docker run -it busybox:v2 /bin/sh
/ # cd /tmp
/tmp # ls -l
total 4
-rw-r--r--    1 root     root            12 Sep 24 06:41 helloworld.txt
/tmp # cat helloworld.txt
Hello world
/tmp # exit
[root@liumiaocn ~]#

使用commit生成的新的映象可以看出history的變化內容

[root@liumiaocn ~]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
busybox                   v2                  185ccb97f18a        13 minutes ago      1.093 MB
busybox                   latest              2b8fd9751c4c        3 months ago        1.093 MB
74th/redmine-all-in-one   latest              cbd38a5bb23d        7 months ago        1.515 GB
[root@liumiaocn ~]# docker history busybox:v2
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
185ccb97f18a        13 minutes ago      /bin/sh                                         73 B
2b8fd9751c4c        3 months ago        /bin/sh -c #(nop) CMD ["sh"]                    0 B
<missing>           3 months ago        /bin/sh -c #(nop) ADD file:9ca60502d646bdd815   1.093 MB
[root@liumiaocn ~]#

export

上面使用commit將正在執行的container做成了新的映象檔案,export則是可以用來將正在執行的container生成tar檔案
啟動一個container

[root@liumiaocn ~]# docker run -it busybox:v2 /bin/sh
/ #

確認container然後將其export出來

[root@liumiaocn ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ea51341f60a5        busybox:v2          "/bin/sh"           19 seconds ago      Up 18 seconds                           goofy_roentgen
[root@liumiaocn ~]# docker export ea51341f60a5 >busyboxv3.tar
[root@liumiaocn ~]# ll busyboxv3.tar
-rw-r--r--. 1 root root 1299968 Sep 24 15:18 busyboxv3.tar
[root@liumiaocn ~]#

rename

用於將container的名稱進行修改

[root@liumiaocn ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
ea51341f60a5        busybox:v2          "/bin/sh"           About a minute ago   Up About a minute                       goofy_roentgen
[root@liumiaocn ~]# docker rename goofy_roentgen busybox
[root@liumiaocn ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ea51341f60a5        busybox:v2          "/bin/sh"           2 minutes ago       Up 2 minutes                            busybox
[root@liumiaocn ~]#

rm

刪除container,如果刪除正在執行中的container,要麼stop之後再rm,要麼加上-f選項

[root@liumiaocn ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ea51341f60a5        busybox:v2          "/bin/sh"           3 minutes ago       Up 3 minutes                            busybox
[root@liumiaocn ~]# docker rm -f busybox
busybox
[root@liumiaocn ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@liumiaocn ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@liumiaocn ~]#

import

從tar檔案生成docker管理的映象檔案,與save/load的區別在於,這個會丟history的內容

[root@liumiaocn ~]# ll busyboxv3.tar
-rw-r--r--. 1 root root 1299968 Sep 24 15:18 busyboxv3.tar
[root@liumiaocn ~]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
busybox                   v2                  185ccb97f18a        25 minutes ago      1.093 MB
busybox                   latest              2b8fd9751c4c        3 months ago        1.093 MB
74th/redmine-all-in-one   latest              cbd38a5bb23d        7 months ago        1.515 GB
[root@liumiaocn ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@liumiaocn ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@liumiaocn ~]# docker import busyboxv3.tar busybox:v3
sha256:8227b6ef55ac3ffc6f4f2a0bf3ed31f96e32cd1275eeaa9f92cfd35eda9de0e1
[root@liumiaocn ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@liumiaocn ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@liumiaocn ~]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
busybox                   v3                  8227b6ef55ac        9 seconds ago       1.093 MB
busybox                   v2                  185ccb97f18a        26 minutes ago      1.093 MB
busybox                   latest              2b8fd9751c4c        3 months ago        1.093 MB
74th/redmine-all-in-one   latest              cbd38a5bb23d        7 months ago        1.515 GB
[root@liumiaocn ~]# docker history busybox:v3
IMAGE               CREATED             CREATED BY          SIZE                COMMENT
8227b6ef55ac        19 seconds ago                          1.093 MB            Imported from -
[root@liumiaocn ~]#