1. 程式人生 > >Docke 1 12 基礎篇 48條命令 3

Docke 1 12 基礎篇 48條命令 3

roc 修改 mar 零基礎 text 段子 net 直觀 repos

本文針對Docker1.12的48條命令,找出了Image相關的9條,劃分未必精確,大家也不會糾結。接下來看看這9條命令都是怎麽用的。

鏡像相關的docker命令

項番命令詳解
No.2 build Build an image from a Dockerfile
No.17 load Load an image from a tar archive or STDIN
No.18 login Log in to a Docker registry.
No.19 logout Log out from a Docker registry.
No.27 pull Pull an image or a repository from a registry
No.28 push Push an image or a repository to a registry
No.32 rmi Remove one or more images
No.34 save Save one or more images to a tar archive (streamed to STDOUT by default)
No.42 tag Tag an image into a repository

build

build是使用Dockerfile方式創建image時所必需的命令,比如,

[root@liumiaocn ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE [root@liumiaocn ~]# cat Dockerfile FROM busybox MAINTAINER liumiaocn@outlook.com [root@liumiaocn ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

使用這個2行的Dockerfile創建出一個busybox的鏡像

[root@liumiaocn ~]# docker build -t busybox .
Sending build context to Docker daemon 86.19
MB Step 1 : FROM busybox Trying to pull repository docker.io/library/busybox ... latest: Pulling from docker.io/library/busybox 8ddc19f16526: Pull complete Digest: sha256:a59906e33509d14c036c8678d687bd4eec81ed7c4b8ce907b888c607f6a1e0e6 Status: Downloaded newer image for docker.io/busybox:latest ---> 2b8fd9751c4c Step 2 : MAINTAINER [email protected] ---> Running in 3289de364e7f ---> 51cb1d751e30 Removing intermediate container 3289de364e7f Successfully built 51cb1d751e30 [root@liumiaocn ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

因為事前沒有busybox的鏡像,所以在build的時候會自動下載,build之後再次確認信息如下

[root@liumiaocn ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              51cb1d751e30        8 seconds ago       1.093 MB
docker.io/busybox   latest              2b8fd9751c4c        3 months ago        1.093 MB
[root@liumiaocn ~]#
  • 1
  • 2
  • 3
  • 4
  • 5

IMAGE ID為51cb1d751e30的是剛剛build的鏡像,從CREATED的時間可以看出,以及REPOSITORY,最關鍵的是build最後會提示出該IMAGEID“Successfully built 51cb1d751e30”。build的時候有很多option可以使用,基礎教程教程只負責入門,詳細使用方法自行研究,其他命令也是如此。

tag

有很多用法,主要用來建立TAG和REPOSITORY之間的關聯。從直觀地感受上來說是用來修改docker images出來的REPOSITORY和tag的信息的。比如:

[root@liumiaocn ~]# docker tag docker.io/busybox:latest hello:world
[root@liumiaocn ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              51cb1d751e30        8 minutes ago       1.093 MB
docker.io/busybox   latest              2b8fd9751c4c        3 months ago        1.093 MB
hello               world               2b8fd9751c4c        3 months ago        1.093 MB
[root@liumiaocn ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

請實際執行的時候REPOSITORY和TAG使用實際意義,例子只是找個樂子而已。但是請註意到修改前後的IMAGE ID都是同一個,說明實際docker在處理這個操作的時候應該只是做了一個alias類似的動作。

rmi

rmi即為remove image的意思,等我們以後研究的東西全部都用漢語拼音的縮寫,scjx,累死他們不會漢語的。

[root@liumiaocn ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/busybox   latest              2b8fd9751c4c        3 months ago        1.093 MB
hello               world               2b8fd9751c4c        3 months ago        1.093 MB
[root@liumiaocn ~]# docker rmi hello:world
Untagged: hello:world
[root@liumiaocn ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/busybox   latest              2b8fd9751c4c        3 months ago        1.093 MB
[root@liumiaocn ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

刪除tag的內容其實只是untag一下,如果實際刪除還會有實際的刪除信息提示出來

[root@liumiaocn ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/busybox   latest              2b8fd9751c4c        3 months ago        1.093 MB
[root@liumiaocn ~]#
[root@liumiaocn ~]# docker rmi docker.io/busybox
Untagged: docker.io/busybox:latest
Deleted: sha256:2b8fd9751c4c0f5dd266fcae00707e67a2545ef34f9a29354585f93dac906749
Deleted: sha256:8ac8bfaff55af948c796026ee867448c5b5b5d9dd3549f4006d9759b25d4a893
[root@liumiaocn ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

pull

pull命令用於從dockerhub或者自建的repository上pull下相關的image,一般使用search先確認,比如我們pull以下剛才下載的busybox
docker build的時候其根據依賴關系,如果本地沒有該鏡像的話,就會自動地到dockerhub上去拖。

[root@liumiaocn ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[root@liumiaocn ~]# docker pull busybox
Using default tag: latest
Trying to pull repository docker.io/library/busybox ...
latest: Pulling from docker.io/library/busybox
8ddc19f16526: Pull complete
Digest: sha256:a59906e33509d14c036c8678d687bd4eec81ed7c4b8ce907b888c607f6a1e0e6
Status: Downloaded newer image for docker.io/busybox:latest
[root@liumiaocn ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/busybox   latest              2b8fd9751c4c        3 months ago        1.093 MB
[root@liumiaocn ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

save

將鏡像文件保存為一個archive文件

[root@liumiaocn ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/busybox   latest              2b8fd9751c4c        3 months ago        1.093 MB
[root@liumiaocn ~]# docker save busybox >busybox.tar
[root@liumiaocn ~]# ll busybox.tar
-rw-r--r--. 1 root root 1302016 Sep 23 20:18 busybox.tar
[root@liumiaocn ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

load

load是save的反向操作,將save的文件load成為docker管理的鏡像。

[root@liumiaocn ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[root@liumiaocn ~]# docker load < busybox.tar
[root@liumiaocn ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/busybox   latest              2b8fd9751c4c        3 months ago        1.093 MB
[root@liumiaocn ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

再確認一下history,還都在

[root@liumiaocn ~]# docker history 2b8fd9751c4c
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
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 ~]#
  • 1
  • 2
  • 3
  • 4
  • 5

login

login操作是了演示後面的logout和push命令,用戶和repository之間建立的關聯通過login/logout/pull/push來進行。
既可以和dockerhub進行連攜,也可以在企業自己創立的私庫進行操作,比如如下是用liumiaocn的個人賬戶登陸到dockerhub

[root@liumiaocn ~]# docker login
Username: liumiaocn
Password:
Email: liumiaocn@outlook.com
WARNING: login credentials saved in /root/.docker/config.json
Login Succeeded
[root@liumiaocn ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

註意此處會把login相關的用戶信息放到/root/.docker/config.json中,而與之成對的logout命令則是通過這個文件判斷是否在login的狀態的

logout

logout則是login的反向操作

[root@liumiaocn ~]# docker logout
Remove login credentials for https://index.docker.io/v1/
[root@liumiaocn ~]#
  • 1
  • 2
  • 3

在logout的基礎上再次logout的話,會提示如下

[root@liumiaocn ~]# docker logout
Not logged in to https://index.docker.io/v1/
[root@liumiaocn ~]#
  • 1
  • 2
  • 3

確認之後發現,config.json的auth信息已經被清空

[root@liumiaocn ~]# cat /root/.docker/config.json
{
        "auths": {}
}[root@liumiaocn ~]#
  • 1
  • 2
  • 3
  • 4

push

push命令用於將本地鏡像推送到遠端的respository中,如果事前沒有login的話,會push失敗。

[root@liumiaocn ~]# docker images
REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
docker.io/liumiaocn/pause-amd64   latest              a7403e3eec99        2 weeks ago         350.2 kB
[root@liumiaocn ~]# docker push docker.io/liumiaocn/pause-amd64
The push refers to a repository [docker.io/liumiaocn/pause-amd64]
5f70bf18a086: Layer already exists
38157ef1625c: Layer already exists
unauthorized: authentication required
[root@liumiaocn ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

login之後,成功push。

[root@liumiaocn ~]# docker login
Username: liumiaocn
Password:
Email: liumiaocn@outlook.com
WARNING: login credentials saved in /root/.docker/config.json
Login Succeeded
[root@liumiaocn ~]# docker push docker.io/liumiaocn/pause-amd64
The push refers to a repository [docker.io/liumiaocn/pause-amd64]
5f70bf18a086: Layer already exists
38157ef1625c: Layer already exists
latest: digest: sha256:c629c9e027a27d9a398e0a2d7179e3d8034a40a5e0c804d65bdaff34372818c0 size: 711
[root@liumiaocn ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

再分享一下我老師大神的人工智能教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智能的隊伍中來!https://blog.csdn.net/jiangjunshow

Docke 1 12 基礎篇 48條命令 3