全面易懂的Docker指令大全

映像檔指令參數總類

Docker的映像檔指令是第二多的,僅次於對於容器處理的指令,下面簡單分類。

映像檔指令有images,pull,load,save,push,rmi,tag,pull,build幾個。我們就來看看。

列出本機映像檔docker images

如果你使用任何映像檔建立過Container,或是手動下載過映像檔,就會存放在本機中。此時可以列出所有的映像檔:

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
joshhu/webdemo      latest              a3574f323972        3 days ago          243.2 MB
ubuntu              latest              d0955f21bf24        5 days ago          188.3 MB
nginx               latest              3f72b0ae3e59        5 days ago          93.43 MB
tutum/apache-php    latest              df6b22a47766        6 days ago          244.4 MB

可以看到列出的REPOSITORY就是user name/repo name,TAG就是tag name。當然還有其IMAGE ID以及下載時間和大小。docker images有幾個常用的參數,分別是:

  • -a:列出完整的映像檔層次資訊。每個映像檔是由不同層次組成的,我們會在稍後說明。
  • -q:只列出映像檔ID。這在做映像檔批次處理時很方便。
  • -tree:官方文件已經沒有這個參數,但還是可以用。列出映像檔不同層次之間的樹狀關係。
$ docker images -a
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
<none>              <none>              d11f03cd837d        3 days ago          243.2 MB
<none>              <none>              8abfc77aac61        3 days ago          243.2 MB
joshhu/webdemo      latest              a3574f323972        3 days ago          243.2 MB
<none>              <none>              d1dfd80f5ed9        3 days ago          243.2 MB
<none>              <none>              2d4173730925        3 days ago          243.2 MB
<none>              <none>              e8f612e3238f        3 days ago          243.2 MB
<none>              <none>              8be3dadcb43a        3 days ago          243.2 MB
<none>              <none>              45ad00454734        3 days ago          243.2 MB
<none>              <none>              1214be61bcaa        3 days ago          188.3 MB
ubuntu              latest              d0955f21bf24        5 days ago          188.3 MB
<none>              <none>              9fec74352904        5 days ago          188.3 MB
<none>              <none>              a1a958a24818        5 days ago          188.3 MB
<none>              <none>              f3c84ac3a053        5 days ago          188.1 MB
<none>              <none>              511136ea3c5a        21 months ago       0 B
$ docker images -q
a3574f323972
d0955f21bf24
$ docker images -tree
Warning: '-tree' is deprecated, it will be removed soon. See usage.
└─511136ea3c5a Virtual Size: 0 B
  └─f3c84ac3a053 Virtual Size: 188.1 MB
    └─a1a958a24818 Virtual Size: 188.3 MB
      └─9fec74352904 Virtual Size: 188.3 MB
        └─d0955f21bf24 Virtual Size: 188.3 MB Tags: ubuntu:latest
          └─1214be61bcaa Virtual Size: 188.3 MB
            └─45ad00454734 Virtual Size: 243.2 MB
              └─8be3dadcb43a Virtual Size: 243.2 MB
                └─2d4173730925 Virtual Size: 243.2 MB
                  └─e8f612e3238f Virtual Size: 243.2 MB
                    └─d11f03cd837d Virtual Size: 243.2 MB
                      └─8abfc77aac61 Virtual Size: 243.2 MB
                        └─d1dfd80f5ed9 Virtual Size: 243.2 MB
                          └─a3574f323972 Virtual Size: 243.2 MB Tags: joshhu/webdemo:latest

下載映像檔docker pull

從Docker Hub下載映像檔,沒有加任何Registry的位址時,就預設從官方的Registry下載(registry.hub.docker.com)。如:

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
joshhu/webdemo      latest              a3574f323972        3 days ago          243.2 MB
$ docker pull ubuntu:latest
511136ea3c5a: Already exists
f3c84ac3a053: Already exists
a1a958a24818: Already exists
9fec74352904: Already exists
d0955f21bf24: Already exists
ubuntu:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.

Status: Image is up to date for ubuntu:latest
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
joshhu/webdemo      latest              a3574f323972        3 days ago          243.2 MB
ubuntu              latest              d0955f21bf24        5 days ago          188.3 MB

要將某一個倉庫的所有映像檔都下載回來,可使用-a參數。但這樣要小心,因為有可能會太大,下載需要很長時間。

$ docker pull -a joshhu/webdemo
c34b4129cfd8: Pulling image (u12) from joshhu/webdemo, endpoint: https://registry-1.docker.io/v1/
c34b4129cfd8: Download complete
a3574f323972: Download complete
511136ea3c5a: Download complete
f3c84ac3a053: Download complete
a1a958a24818: Download complete
9fec74352904: Download complete
.....
9654debb69ae: Download complete
Status: Downloaded newer image for joshhu/webdemo
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
joshhu/webdemo      u12                 c34b4129cfd8        35 minutes ago      288 MB
joshhu/webdemo      latest              a3574f323972        3 days ago          243.2 MB
$

將映像檔存入/匯出電腦檔案格式docker save/load

Docker的映像檔雖然名為檔案,但其格式十分複雜(本書後面章節會有說明)。你也不知道他存在哪,也不知道什麼格式,如果想要和其它人交換時 ,不想上傳到Docker Hub,不想自己架設私有Docker Registry,就可以用這兩個參數存成tarball格式及匯出。存入時別忘了加-o參數,要不然只會在顯示不會真的壓縮。

$ docker save -o webdemou12.tar joshhu/webdemo:u12
$ ls -al
total 294020
drwxrwxr-x  4 joshhu joshhu      4096 Mar 26 00:37 .
drwxr-xr-x 27 joshhu joshhu      4096 Mar 25 20:03 ..
drwxr-xr-x  3 joshhu joshhu      4096 Mar 22 08:25 test
drwxr-xr-x  3 joshhu joshhu      4096 Mar 25 23:52 u12
-rw-r--r--  1 root   root   301058560 Mar 26 00:37 webdemou12.tar
$

tarball還原回映像檔格式,則用load。可輸入

$ docker load --input webdemou12.tar

下面是範例:

$ docker images -a
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
$ ls -al webdemou12.tar
-rw-r--r-- 1 chtti chtti 301058560 Mar 26 00:41 webdemou12.tar
$ docker load --input webdemou12.tar
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
joshhu/webdemo      u12                 c34b4129cfd8        47 minutes ago      288 MB

刪除映像檔docker rmi

這個指令刪除本機中存放的映像檔。但如果有容器還在使用這個映像檔,則無法刪除。如果硬要刪除,可以下-f參數強迫刪除。

前面提到映像檔是以層次的方式來存放,因此一個映像檔會有多個層次。你可以下--no-prune=true這個參數,只殺掉有tag name的映像檔。

以一個標準的映像檔來說,只會殺掉最上面一層,因為建立時的其它中間層次並不會有tag name,這樣做的好處是可以留下許多映像檔共用的母層次。這也是Docker映像檔指令中,唯一能處理到層次這個等級的參數了

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
joshhu/webdemo      u12                 c34b4129cfd8        2 hours ago         288 MB
$ docker images -a
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
joshhu/webdemo      u12                 c34b4129cfd8        2 hours ago         288 MB
<none>              <none>              878616cacd7b        2 hours ago         288 MB
<none>              <none>              95325bba7e4e        2 hours ago         288 MB
<none>              <none>              f12848fa258d        2 hours ago         288 MB
<none>              <none>              9654debb69ae        2 hours ago         288 MB
<none>              <none>              dec7d1913547        2 hours ago         288 MB
<none>              <none>              05958e5f3b74        2 hours ago         288 MB
<none>              <none>              fe798fd9c738        2 hours ago         288 MB
<none>              <none>              afc5c5484606        2 hours ago         131.9 MB
<none>              <none>              9c5e4be642b7        5 days ago          131.9 MB
<none>              <none>              72dffce15bf2        5 days ago          131.9 MB
<none>              <none>              c41f0fc9131d        5 days ago          131.9 MB
<none>              <none>              ea806576238a        5 days ago          131.7 MB
<none>              <none>              511136ea3c5a        21 months ago       0 B
$ docker rmi --no-prune=true joshhu/webdemo:u12
Untagged: joshhu/webdemo:u12
Deleted: c34b4129cfd8bbf8a9b6a820cc7ae5e96dc10c58dda5916d8849d64b9c43db7b
$ docker images -a
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
<none>              <none>              878616cacd7b        2 hours ago         288 MB
<none>              <none>              9654debb69ae        2 hours ago         288 MB
<none>              <none>              f12848fa258d        2 hours ago         288 MB
<none>              <none>              95325bba7e4e        2 hours ago         288 MB
<none>              <none>              05958e5f3b74        2 hours ago         288 MB
<none>              <none>              dec7d1913547        2 hours ago         288 MB
<none>              <none>              fe798fd9c738        2 hours ago         288 MB
<none>              <none>              afc5c5484606        2 hours ago         131.9 MB
<none>              <none>              9c5e4be642b7        5 days ago          131.9 MB
<none>              <none>              72dffce15bf2        5 days ago          131.9 MB
<none>              <none>              c41f0fc9131d        5 days ago          131.9 MB
<none>              <none>              ea806576238a        5 days ago          131.7 MB
<none>              <none>              511136ea3c5a        21 months ago       0 B

一次刪掉所有映像檔

可以配合Linux的批次指令來一次清乾淨所有的映像檔,輸入

docker rmi -f $(docker images -aq)

執行如下:

docker rmi -f $(docker images -aq)
Deleted: 9654debb69ae7a8ef27571e088e8779c89523c3654dd4d5f5ffa7862f097d012
Deleted: 95325bba7e4e26487d71b6e95d1574a235ebe993dd0708eade700f7f78b74e58
Deleted: f12848fa258de0af708f30eb135ad3a4ffcfb4391e219d266014116aa6fbfd27
Deleted: 878616cacd7b522581e5a41209293bef644381625405b19d6882c070314e1b49
Deleted: 05958e5f3b74cf5aaa788b79711e75f71b3e9b01a8da80a0fcbe294cb2c34d3c
Deleted: dec7d19135473395d025767bb00d0b2a8fdbff81426a1cf64b528e48f4b5b738
Deleted: fe798fd9c738d610261e5a87e9f8503fb04fe2c8c8c928399c15fe12d52d4aec
Deleted: afc5c5484606ebde6fd96908c958020d57bfdfc4e3dcc81d1d9551572916cbcf
Deleted: 9c5e4be642b799060baa07b826968128ff66d2ced86f6098f6d9f5845f2d35dd
Deleted: 72dffce15bf2df5afebfbe91515463a1629dc6a9233ede1346e80cd97da606ac
Deleted: c41f0fc9131d093dea7decaf641db6b455336a592409d5209832c6eebb749405
Deleted: ea806576238a90ec7ea482876f39d63427cbe1cf980a54488fbad1fbae8f9eef
Deleted: 511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158
FATA[0000] Error: failed to remove one or more images
$ docker images -a
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE

替本機映像檔加標籤名稱docker tag

前面提過,一個映像檔可以有很多不同的tag name。為了方便,我們常常會給同一個映像檔不同的tag name

舉例來說,筆者在本機有一個joshhu/webdemo倉庫,其中有兩個映像檔,一個是Ubuntu 12+Apache/php,一個是Ubuntu14+Apache/php。由於不同Ubuntu版本安裝的Apache/php版本也不同,因此都放在webdemo這個repo下,必須使用tag name來區分。原來的Ubuntu12已經有一個tag nameu12了,因此我要將另一個沒有tag name的加上標籤名稱(在建立時,系統自動將這個映像檔的tag name指定成latest)。 原來沒的joshhu/webdemo的TAG是latest

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
joshhu/webdemo      u12                 c34b4129cfd8        12 hours ago        288 MB
joshhu/webdemo      latest              a3574f323972        3 days ago          243.2 MB
ubuntu              12.04               9c5e4be642b7        5 days ago          131.9 MB
ubuntu              12.04.5             9c5e4be642b7        5 days ago          131.9 MB
ubuntu              precise             9c5e4be642b7        5 days ago          131.9 MB
ubuntu

加上TAG之後

$ docker tag joshhu/webdemo:latest joshhu/webdemo:u14
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
joshhu/webdemo      u12                 c34b4129cfd8        12 hours ago        288 MB
joshhu/webdemo      latest              a3574f323972        3 days ago          243.2 MB
joshhu/webdemo      u14                 a3574f323972        3 days ago          243.2 MB
ubuntu              12.04               9c5e4be642b7        5 days ago          131.9 MB
ubuntu              12.04.5             9c5e4be642b7        5 days ago          131.9 MB
ubuntu              precise             9c5e4be642b7        5 days ago          131.9 MB
ubuntu              precise-20150320    9c5e4be642b7        5 days ago          131.9 MB

可以看出其中latestu14兩個TAG是指到同一個映像檔ID。

從上圖也可以看到,來自官方的映像檔tag name通常用來標記來自同一個repo的不同映像檔。例如 ubuntu 倉庫中有多個映像檔,用 TAG 來區分發行版本,如圖中, 9c5e4be642b7映像檔擁有四個TAG,是同一份映像檔。

自建映像檔docker build/hisotry

docker build指令可以從現成的映像檔為基礎,自行建立全新的映像檔,而docker history則會列出製作的每一步過程。我們會在自建映像檔章節中討論完整實作。

上傳映像檔docker push

如果你自建立映像檔,可以上傳到官方/私有/非官方公開的Docker Registry上。就使用push這個指令。使用方法很簡單,如果你已經使用docker login,那就可以直接用標準的映像檔名稱上傳,如果你還沒有login,會提示你輸入帳號、密碼及電子郵件。

docker push <username>/<repo name>:<Tag name>

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
joshhu/webdemo      u12                 c34b4129cfd8        12 hours ago        288 MB
joshhu/webdemo      latest              a3574f323972        3 days ago          243.2 MB
joshhu/webdemo      u14                 a3574f323972        3 days ago          243.2 MB
ubuntu              12.04               9c5e4be642b7        5 days ago          131.9 MB
ubuntu              12.04.5             9c5e4be642b7        5 days ago          131.9 MB
ubuntu              precise             9c5e4be642b7        5 days ago          131.9 MB
ubuntu              precise-20150320    9c5e4be642b7        5 days ago          131.9 MB
$
$ docker push joshhu/webdemo:u12
The push refers to a repository [joshhu/webdemo] (len: 1)
Sending image list
Pushing repository joshhu/webdemo (1 tags)
511136ea3c5a: Image already pushed, skipping
ea806576238a: Image already pushed, skipping
c41f0fc9131d: Image already pushed, skipping
72dffce15bf2: Image already pushed, skipping
9c5e4be642b7: Image already pushed, skipping
afc5c5484606: Image already pushed, skipping
fe798fd9c738: Image already pushed, skipping
dec7d1913547: Image already pushed, skipping
05958e5f3b74: Image already pushed, skipping
878616cacd7b: Image already pushed, skipping
f12848fa258d: Image already pushed, skipping
95325bba7e4e: Image already pushed, skipping
9654debb69ae: Image already pushed, skipping
c34b4129cfd8: Image already pushed, skipping
Pushing tag for rev [c34b4129cfd8] on {https://cdn-registry-1.docker.io/v1/repositories/joshhu/webdemo/tags/u12}
$