容器技術之Docker常用命令說明

1874發表於2020-05-30

  前面我們聊了docker的基本概念、架構、映象、網路、資料卷,回顧請參考https://www.cnblogs.com/qiuhom-1874/category/1766327.html;今天這篇部落格主要是對前面部落格中的常用命令做一個總結補充說明;

  在前面的部落格中我們說過docker是基於LXC之上封裝來實現容器的,其核心就是利用核心的資源名稱空間和Cgroup實現資源的管控和隔離;這裡要補充一點的是docker容器把資源抽象成物件;作為客戶端,docker這個命令其實就是在對這些抽象出來的資源物件做增刪查改的操作;docker的API是RESTful風格的API,所以它支援類似像http協議裡的GET、POST、PUT、DELETE等操作;RESTful風格的API有這樣的特點;1、每一個URI代表1種資源;2、支援類似http裡的GET、POST、PUT、DELETE;GET用來獲取資源,POST用來新建資源(也可以用於更新資源),PUT用來更新資源,DELETE用來刪除資源;3、通過操作資源的表現形式來操作資源;4、資源的表現形式是XML或者HTML;5、客戶端與服務端之間的互動在請求之間是無狀態的,從客戶端到服務端的每個請求都必須包含理解請求所必需的資訊。這也是docker客戶端和服務端使用http或https協議通訊的原因吧!!

  瞭解了上面docker的API風格,我們接下來再說說docker的物件;docker的物件大概有這樣幾個,1、映象(image),2、容器(container),3、網路(network),4、卷(volumes),5、外掛(plugins);除上5種還有就是其他物件;其中最為核心的就image,container,network;對於docker來說執行一容器的最最基礎的是需要一映象,其次就是網路,對於容器來講,我們執行容器的目的就是為了提供服務,而絕大部分服務都是基於網路的,所以真正產生價值的是把服務通過網路提供給客戶;所以網路是必不可少的資源;

  映象相關操作

  映象搜尋

[root@node1 ~]# docker search nginx
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                              Official build of Nginx.                        13255               [OK]                
jwilder/nginx-proxy                Automated Nginx reverse proxy for docker con…   1812                                    [OK]
richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable of…   775                                     [OK]
linuxserver/nginx                  An Nginx container, brought to you by LinuxS…   113                                     
bitnami/nginx                      Bitnami nginx Docker Image                      83                                      [OK]
tiangolo/nginx-rtmp                Docker image with Nginx using the nginx-rtmp…   74                                      [OK]
alfg/nginx-rtmp                    NGINX, nginx-rtmp-module and FFmpeg from sou…   64                                      [OK]
jc21/nginx-proxy-manager           Docker container for managing Nginx proxy ho…   61                                      
nginxdemos/hello                   NGINX webserver that serves a simple page co…   50                                      [OK]
jlesage/nginx-proxy-manager        Docker container for Nginx Proxy Manager        44                                      [OK]
nginx/nginx-ingress                NGINX Ingress Controller for Kubernetes         32                                      
privatebin/nginx-fpm-alpine        PrivateBin running on an Nginx, php-fpm & Al…   25                                      [OK]
schmunk42/nginx-redirect           A very simple container to redirect HTTP tra…   18                                      [OK]
nginxinc/nginx-unprivileged        Unprivileged NGINX Dockerfiles                  16                                      
centos/nginx-112-centos7           Platform for running nginx 1.12 or building …   13                                      
blacklabelops/nginx                Dockerized Nginx Reverse Proxy Server.          13                                      [OK]
centos/nginx-18-centos7            Platform for running nginx 1.8 or building n…   13                                      
raulr/nginx-wordpress              Nginx front-end for the official wordpress:f…   12                                      [OK]
nginx/nginx-prometheus-exporter    NGINX Prometheus Exporter                       12                                      
mailu/nginx                        Mailu nginx frontend                            7                                       [OK]
sophos/nginx-vts-exporter          Simple server that scrapes Nginx vts stats a…   7                                       [OK]
bitnami/nginx-ingress-controller   Bitnami Docker Image for NGINX Ingress Contr…   5                                       [OK]
ansibleplaybookbundle/nginx-apb    An APB to deploy NGINX                          1                                       [OK]
wodby/nginx                        Generic nginx                                   1                                       [OK]
centos/nginx-110-centos7           Platform for running nginx 1.10 or building …   0                                       
[root@node1 ~]#

  提示:docker search 命令是從dockerhub中檢索對應映象的命令;該命令有個-s選項,是用於指定映象的星級;通過使用-s選項可以篩選出我們指定星級以上的映象;

[root@node1 ~]# docker search -s 5000 nginx
Flag --stars has been deprecated, use --filter=stars=3 instead
NAME                DESCRIPTION                STARS               OFFICIAL            AUTOMATED
nginx               Official build of Nginx.   13255               [OK]                
[root@node1 ~]# docker search -s 500 nginx 
Flag --stars has been deprecated, use --filter=stars=3 instead
NAME                      DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                     Official build of Nginx.                        13255               [OK]                
jwilder/nginx-proxy       Automated Nginx reverse proxy for docker con…   1812                                    [OK]
richarvey/nginx-php-fpm   Container running Nginx + PHP-FPM capable of…   775                                     [OK]
[root@node1 ~]# 

  映象下載

[root@node1 ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
afb6ec6fdc1c: Pull complete 
b90c53a0b692: Pull complete 
11fa52a0fdc0: Pull complete 
Digest: sha256:30dfa439718a17baafefadf16c5e7c9d0a1cde97b4fd84f63b69e13513be7097
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@node1 ~]# docker image pull centos:7
7: Pulling from library/centos
524b0c1e57f8: Pull complete 
Digest: sha256:e9ce0b76f29f942502facd849f3e468232492b259b9d9f076f71b392293f1582
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
[root@node1 ~]#

  提示:docker pull和docker image pull兩個命令都是從dockerhub倉庫中下載指定映象;這裡需要提示一下,docker的映象是映象名+版本組成的;如果我們只指定了映象名稱沒有指定版本,預設是latest版本(最新版);

  檢視映象列表

[root@node1 ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              9beeba249f3e        13 days ago         127MB
centos              7                   b5b4d78bc90c        3 weeks ago         203MB
[root@node1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              9beeba249f3e        13 days ago         127MB
centos              7                   b5b4d78bc90c        3 weeks ago         203MB
[root@node1 ~]#

  提示:docker image ls 等同docker images 這兩個命令都是用來檢視本地倉庫所有映象列表;

  除了上面的兩種操作外,還有其他操作,我們可以通過docker image --help 來檢視對映象的命令幫助;如下

[root@node1 ~]# docker image --help

Usage:  docker image COMMAND

Manage images

Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Run 'docker image COMMAND --help' for more information on a command.
[root@node1 ~]# 

  提示:build子命令是基於dockerfile來構建映象;history:顯示映象的歷史製作過程;import:從tarball匯入內容以建立檔案系統映象;inspect:顯示映象的詳細資訊;load:指定本地一個tar包格式的檔案從標準輸入匯入映象;prune:移除未使用的映象;push:把指定映象推送到倉庫;rm:刪除一個或多個映象;save:匯出一個或多個映象到本地指定tar檔案中;預設是從標準輸出匯出;tag:對指定映象新建一個標籤;

  檢視映象歷史

[root@node1 ~]# docker image history   nginx              
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
9beeba249f3e        13 days ago         /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B                  
<missing>           13 days ago         /bin/sh -c #(nop)  STOPSIGNAL SIGTERM           0B                  
<missing>           13 days ago         /bin/sh -c #(nop)  EXPOSE 80                    0B                  
<missing>           13 days ago         /bin/sh -c ln -sf /dev/stdout /var/log/nginx…   22B                 
<missing>           13 days ago         /bin/sh -c set -x     && addgroup --system -…   57.6MB              
<missing>           13 days ago         /bin/sh -c #(nop)  ENV PKG_RELEASE=1~buster     0B                  
<missing>           13 days ago         /bin/sh -c #(nop)  ENV NJS_VERSION=0.3.9        0B                  
<missing>           13 days ago         /bin/sh -c #(nop)  ENV NGINX_VERSION=1.17.10    0B                  
<missing>           13 days ago         /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B                  
<missing>           2 weeks ago         /bin/sh -c #(nop)  CMD ["bash"]                 0B                  
<missing>           2 weeks ago         /bin/sh -c #(nop) ADD file:7780c81c33e6cc5b6…   69.2MB              
[root@node1 ~]# 

  提示:以上預設是顯示部分命令,如果要顯示命令全部可以使用 --no-trunc選項;

  檢視映象詳細資訊

[root@node1 ~]# docker image inspect nginx
[
    {
        "Id": "sha256:9beeba249f3ee158d3e495a6ac25c5667ae2de8a43ac2a8bfd2bf687a58c06c9",
        "RepoTags": [
            "nginx:latest"
        ],
        "RepoDigests": [
            "nginx@sha256:30dfa439718a17baafefadf16c5e7c9d0a1cde97b4fd84f63b69e13513be7097"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2020-05-15T20:15:52.366876108Z",
        "Container": "ed0a25109c2acf3dcb0b4926cad00692e01717b8417d36e50928aa6f03a711ca",
        "ContainerConfig": {
            "Hostname": "ed0a25109c2a",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.17.10",
                "NJS_VERSION=0.3.9",
                "PKG_RELEASE=1~buster"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"nginx\" \"-g\" \"daemon off;\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:23edbb1066877bcc22193518edb32ee3a50a18d52787c4f45f9a8bca95d329eb",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
            },
            "StopSignal": "SIGTERM"
        },
        "DockerVersion": "18.09.7",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.17.10",
                "NJS_VERSION=0.3.9",
                "PKG_RELEASE=1~buster"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:23edbb1066877bcc22193518edb32ee3a50a18d52787c4f45f9a8bca95d329eb",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
            },
            "StopSignal": "SIGTERM"
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 126773960,
        "VirtualSize": 126773960,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/4728ce1723abfab65d8065858c616360f45e258d53e7573be8e0f7bbe338be5d/diff:/var/lib/docker/overlay2/e998de0cd64ffe2b0410fb4595cbc3358b1b28626c8d356c6c6ac0ef97234a31/diff",
                "MergedDir": "/var/lib/docker/overlay2/d9c40ef0bb9226e6a84b720b9c6ce18ed46b4f61caab567b5ee13a801dbc7ef5/merged",
                "UpperDir": "/var/lib/docker/overlay2/d9c40ef0bb9226e6a84b720b9c6ce18ed46b4f61caab567b5ee13a801dbc7ef5/diff",
                "WorkDir": "/var/lib/docker/overlay2/d9c40ef0bb9226e6a84b720b9c6ce18ed46b4f61caab567b5ee13a801dbc7ef5/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:ffc9b21953f4cd7956cdf532a5db04ff0a2daa7475ad796f1bad58cfbaf77a07",
                "sha256:2f4accd375d93db49e5a47c9bebe4e0dd3cef35f765f5cd36840a986435affc9",
                "sha256:6c7de695ede33d90077f01d60ec29e6a51552a3e350757018ff1b1ecd6cee0bf"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]
[root@node1 ~]# 

  提示:docker image inspect 是顯示指定映象的詳細資訊;其中-f選項可以只顯示指定欄位;起著過濾功能

[root@node1 ~]# docker image inspect -f {{.RootFS}} nginx        
{layers [sha256:ffc9b21953f4cd7956cdf532a5db04ff0a2daa7475ad796f1bad58cfbaf77a07 sha256:2f4accd375d93db49e5a47c9bebe4e0dd3cef35f765f5cd36840a986435affc9 sha256:6c7de695ede33d90077f01d60ec29e6a51552a3e350757018ff1b1ecd6cee0bf] }
[root@node1 ~]# docker image inspect -f {{.RootFS.Type}} nginx 
layers
[root@node1 ~]# docker image inspect -f {{.RootFS.Layers}} nginx    
[sha256:ffc9b21953f4cd7956cdf532a5db04ff0a2daa7475ad796f1bad58cfbaf77a07 sha256:2f4accd375d93db49e5a47c9bebe4e0dd3cef35f765f5cd36840a986435affc9 sha256:6c7de695ede33d90077f01d60ec29e6a51552a3e350757018ff1b1ecd6cee0bf]
[root@node1 ~]# 

  提示:-f指定欄位必須從“.”開始且需要用雙大括號把欄位括起來;如果一級欄位是下還有二級欄位,可以使用“.”加二級欄位名稱來表示顯示二級欄位的值;以此類推;

  刪除映象

[root@node1 ~]# docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
d9cbbca60e5f: Pull complete 
Digest: sha256:836945da1f3afe2cfff376d379852bbb82e0237cb2925d53a13f53d6e8a8c48c
Status: Downloaded newer image for busybox:latest
docker.io/library/busybox:latest
[root@node1 ~]# docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
cbdbe7a5bc2a: Pull complete 
Digest: sha256:9a839e63dad54c3a6d1834e29692c8492d93f90c59c978c1ed79109ea4fb9a54
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest
[root@node1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              9beeba249f3e        13 days ago         127MB
busybox             latest              78096d0a5478        2 weeks ago         1.22MB
centos              7                   b5b4d78bc90c        3 weeks ago         203MB
alpine              latest              f70734b6a266        5 weeks ago         5.61MB
[root@node1 ~]# docker image rm centos:7
Untagged: centos:7
Untagged: centos@sha256:e9ce0b76f29f942502facd849f3e468232492b259b9d9f076f71b392293f1582
Deleted: sha256:b5b4d78bc90ccd15806443fb881e35b5ddba924e2f475c1071a38a3094c3081d
Deleted: sha256:edf3aa290fb3c255a84fe836109093fbfeef65c08544f655fad8d6afb53868ba
[root@node1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              9beeba249f3e        13 days ago         127MB
busybox             latest              78096d0a5478        2 weeks ago         1.22MB
alpine              latest              f70734b6a266        5 weeks ago         5.61MB
[root@node1 ~]#

  提示:刪除映象的前提是該映象沒有執行成容器;docker image rm 等同docker rmi命令;如果需要強行刪除已經執行為容器的映象,可以使用-f選項來指定;

[root@node1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              f70734b6a266        5 weeks ago         5.61MB
[root@node1 ~]# docker run --name a1 -d alpine
d0abe1efe7138dfb409574a785e77b4941a50534a23740d6f7d2f1af36d05589
[root@node1 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
d0abe1efe713        alpine              "/bin/sh"           4 seconds ago       Exited (0) 2 seconds ago                       a1
[root@node1 ~]# docker image rm alpine
Error response from daemon: conflict: unable to remove repository reference "alpine" (must force) - container d0abe1efe713 is using its referenced image f70734b6a266
[root@node1 ~]# docker rmi alpine
Error response from daemon: conflict: unable to remove repository reference "alpine" (must force) - container d0abe1efe713 is using its referenced image f70734b6a266
[root@node1 ~]# docker image rm -f alpine
Untagged: alpine:latest
Untagged: alpine@sha256:9a839e63dad54c3a6d1834e29692c8492d93f90c59c978c1ed79109ea4fb9a54
Deleted: sha256:f70734b6a266dcb5f44c383274821207885b549b75c8e119404917a61335981a
[root@node1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[root@node1 ~]# 

  提示:只要執行為容器,不管容器是否是執行狀態,刪除映象都需要先刪除容器在刪除映象,否則就需要用-f來指定強制刪除映象;通常不建議這麼幹;

  給映象打標籤

[root@node1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              f70734b6a266        5 weeks ago         5.61MB
[root@node1 ~]# docker image tag alpine:latest alpine:v1
[root@node1 ~]# docker image tag alpine:latest alpine:v2
[root@node1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              f70734b6a266        5 weeks ago         5.61MB
alpine              v1                  f70734b6a266        5 weeks ago         5.61MB
alpine              v2                  f70734b6a266        5 weeks ago         5.61MB
[root@node1 ~]# docker tag alpine alpine:v3 
[root@node1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              f70734b6a266        5 weeks ago         5.61MB
alpine              v1                  f70734b6a266        5 weeks ago         5.61MB
alpine              v2                  f70734b6a266        5 weeks ago         5.61MB
alpine              v3                  f70734b6a266        5 weeks ago         5.61MB
[root@node1 ~]# 

  提示:docker image tag 和docker tag 等同;都是用於給映象打標籤;打標籤的意思類似給映象取別名的意思;

  移除未使用的映象

[root@node1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              9beeba249f3e        13 days ago         127MB
alpine              latest              f70734b6a266        5 weeks ago         5.61MB
alpine              v1                  f70734b6a266        5 weeks ago         5.61MB
alpine              v2                  f70734b6a266        5 weeks ago         5.61MB
alpine              v3                  f70734b6a266        5 weeks ago         5.61MB
[root@node1 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
d0abe1efe713        alpine              "/bin/sh"           11 minutes ago      Exited (0) 11 minutes ago                       a1
[root@node1 ~]# docker image prune -a
WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: alpine:latest
untagged: alpine:v1
untagged: alpine:v2
untagged: nginx:latest
untagged: nginx@sha256:30dfa439718a17baafefadf16c5e7c9d0a1cde97b4fd84f63b69e13513be7097
deleted: sha256:9beeba249f3ee158d3e495a6ac25c5667ae2de8a43ac2a8bfd2bf687a58c06c9
deleted: sha256:8fb6373b4cca3383756d7fd7843dd92f95827e5f2913609e09a9621dcddb3752
deleted: sha256:8b09841626797a03a9fe5e73aa38aeacf9ff0ce85a3004236ff35234eec3b35c
deleted: sha256:ffc9b21953f4cd7956cdf532a5db04ff0a2daa7475ad796f1bad58cfbaf77a07

Total reclaimed space: 126.8MB
[root@node1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              v3                  f70734b6a266        5 weeks ago         5.61MB
[root@node1 ~]# 

  提示:-a表示移除全部未使用的映象;如果不想互動式確認可以使用-f選項,-f表示強制刪除,不詢問;

  把指定映象推送到倉庫

[root@node1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
linux1874/myimg     latest              e408b1c6e04f        6 days ago          1.22MB
linux1874/myimg     v0.1                e408b1c6e04f        6 days ago          1.22MB
alpine              v3                  f70734b6a266        5 weeks ago         5.61MB
[root@node1 ~]# docker image push linux1874/myimg
The push refers to repository [docker.io/linux1874/myimg]
Get https://registry-1.docker.io/v2/: net/http: request canceled (Client.Timeout exceeded while awaiting headers)
[root@node1 ~]# docker image push linux1874/myimg
The push refers to repository [docker.io/linux1874/myimg]
4d567d38fed1: Layer already exists 
1079c30efc82: Layer already exists 
latest: digest: sha256:6c2f6b7a0df5ca0a46cd46d858e9fd564169471e6715c0155027ac77672508f6 size: 734
4d567d38fed1: Layer already exists 
1079c30efc82: Layer already exists 
v0.1: digest: sha256:6c2f6b7a0df5ca0a46cd46d858e9fd564169471e6715c0155027ac77672508f6 size: 734
[root@node1 ~]# 

  提示:如果是往自己私有倉庫裡推送映象,需要先登入,然後在推映象;這裡需要注意一點映象的命名需要同倉庫一樣;如果是頂級倉庫則沒有“/”分割;如果推送映象沒有指定版本,預設會把本地指定映象的所有版本的推送到倉庫;

  映象匯出

[root@node1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
linux1874/myimg     latest              e408b1c6e04f        6 days ago          1.22MB
linux1874/myimg     v0.1                e408b1c6e04f        6 days ago          1.22MB
alpine              v3                  f70734b6a266        5 weeks ago         5.61MB
[root@node1 ~]# docker save alpine > alpine.tar.gz
[root@node1 ~]# ls
alpine.tar.gz
[root@node1 ~]# 

  提示:docker save預設是把映象輸出到標準輸出,所以我們匯出時需要用重定向的方式儲存到某個檔案中,通常這個檔案是tar格式的檔案;

  映象匯入

[root@node1 ~]# scp alpine.tar.gz 192.168.0.22:/root
The authenticity of host '192.168.0.22 (192.168.0.22)' can't be established.
ECDSA key fingerprint is SHA256:EG9nua4JJuUeofheXlgQeL9hX5H53JynOqf2vf53mII.
ECDSA key fingerprint is MD5:57:83:e6:46:2c:4b:bb:33:13:56:17:f7:fd:76:71:cc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.22' (ECDSA) to the list of known hosts.
alpine.tar.gz                                                   100% 5750KB  31.9MB/s   00:00    
[root@node1 ~]# 
[root@docker_node1 ~]# ip a s ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:22:36:7f brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.22/24 brd 192.168.0.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe22:367f/64 scope link 
       valid_lft forever preferred_lft forever
[root@docker_node1 ~]# ls
alpine.tar.gz  
[root@docker_node1 ~]# docker images
WARNING: Error loading config file: /root/.docker/config.json: EOF
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
linux1874/myimg     v0.1                e408b1c6e04f        6 days ago          1.22MB
wordpress           latest              c3fa1c8546fb        4 weeks ago         540MB
mysql               5.7                 f965319e89de        4 weeks ago         448MB
httpd               2.4.37-alpine       dfd436f9a5d8        17 months ago       91.8MB
[root@docker_node1 ~]# docker load -i alpine.tar.gz 
WARNING: Error loading config file: /root/.docker/config.json: EOF
3e207b409db3: Loading layer  5.879MB/5.879MB
Loaded image: alpine:v3
[root@docker_node1 ~]# docker images
WARNING: Error loading config file: /root/.docker/config.json: EOF
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
linux1874/myimg     v0.1                e408b1c6e04f        6 days ago          1.22MB
wordpress           latest              c3fa1c8546fb        4 weeks ago         540MB
mysql               5.7                 f965319e89de        4 weeks ago         448MB
alpine              v3                  f70734b6a266        5 weeks ago         5.61MB
httpd               2.4.37-alpine       dfd436f9a5d8        17 months ago       91.8MB
[root@docker_node1 ~]#

  有關容器的操作

[root@node1 ~]# docker container --help
WARNING: Error loading config file: /root/.docker/config.json: EOF

Usage:  docker container COMMAND

Manage containers

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  inspect     Display detailed information on one or more containers
  kill        Kill one or more running containers
  logs        Fetch the logs of a container
  ls          List containers
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  prune       Remove all stopped containers
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  run         Run a command in a new container
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker container COMMAND --help' for more information on a command.
[root@node1 ~]# 

  提示:docker container 操作大概有這麼多;其中run ,exec,kill,port,logs,inspect用的比較多;其餘命令其實我們看子命令就大概知道什麼意思;如果需要檢視某個子命令的具體用法和選項,我們可以加上子命令 然後用--help來檢視幫助即可;如下

[root@node1 ~]# docker container logs --help
WARNING: Error loading config file: /root/.docker/config.json: EOF

Usage:  docker container logs [OPTIONS] CONTAINER

Fetch the logs of a container

Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative
                       (e.g. 42m for 42 minutes)
      --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps
      --until string   Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative
                       (e.g. 42m for 42 minutes)
[root@node1 ~]# 

  提示:以上就是檢視logs指明的用法和選項說明;

  建立容器

[root@node1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
linux1874/myimg     latest              e408b1c6e04f        6 days ago          1.22MB
linux1874/myimg     v0.1                e408b1c6e04f        6 days ago          1.22MB
alpine              v3                  f70734b6a266        5 weeks ago         5.61MB
[root@node1 ~]# docker container create alpine
Unable to find image 'alpine:latest' locally
latest: Pulling from library/alpine
Digest: sha256:9a839e63dad54c3a6d1834e29692c8492d93f90c59c978c1ed79109ea4fb9a54
Status: Downloaded newer image for alpine:latest
13bcdb5067a31389acd75b8218f73c9d759590b81e178084de3c8110af330d0b
[root@node1 ~]# docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@node1 ~]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
13bcdb5067a3        alpine              "/bin/sh"           17 seconds ago      Created                                         tender_grothendieck
d0abe1efe713        alpine              "/bin/sh"           49 minutes ago      Exited (0) 49 minutes ago                       a1
[root@node1 ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@node1 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
13bcdb5067a3        alpine              "/bin/sh"           3 minutes ago       Created                                         tender_grothendieck
d0abe1efe713        alpine              "/bin/sh"           53 minutes ago      Exited (0) 53 minutes ago                       a1
[root@node1 ~]# 

  提示:用create命令建立容器,容器不會自動執行;檢視容器用ls命令,其中ls不加選項表示檢視執行狀態的容器列表,-a表示檢視所有狀態容器列表;docker container ls 就等同docker ps 都用於檢視執行態容器列表; 

  建立並執行容器

[root@node1 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@node1 ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
linux1874/myimg     latest              e408b1c6e04f        6 days ago          1.22MB
linux1874/myimg     v0.1                e408b1c6e04f        6 days ago          1.22MB
alpine              latest              f70734b6a266        5 weeks ago         5.61MB
alpine              v3                  f70734b6a266        5 weeks ago         5.61MB
[root@node1 ~]# docker run --name a1 -d linux1874/myimg
6be4be4e54690bc10fb07b12cc00c3173636ed95bd456f04ed2cca6cb8cc93e9
[root@node1 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
6be4be4e5469        linux1874/myimg     "/bin/sh -c '/bin/ht…"   4 seconds ago       Up 2 seconds                            a1
[root@node1 ~]# 

  提示:docker run 等同docker container run;其中--name表示指定容器的的名稱;-d表示後臺執行,不佔據現有終端;用run命令表示執行一容器,如果本地沒有映象,它預設會從dockerhub上去拖映象,然後直接建立並執行容器;如果有映象,則直接建立並執行為容器;

  停止、啟動、重啟容器

[root@node1 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
6be4be4e5469        linux1874/myimg     "/bin/sh -c '/bin/ht…"   4 minutes ago       Up 4 minutes                            a1
[root@node1 ~]# docker stop a1
a1
[root@node1 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
6be4be4e5469        linux1874/myimg     "/bin/sh -c '/bin/ht…"   4 minutes ago       Exited (137) 5 seconds ago                       a1
[root@node1 ~]# docker start a1
a1
[root@node1 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
6be4be4e5469        linux1874/myimg     "/bin/sh -c '/bin/ht…"   4 minutes ago       Up 3 seconds                            a1
[root@node1 ~]# docker restart a1
a1
[root@node1 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
6be4be4e5469        linux1874/myimg     "/bin/sh -c '/bin/ht…"   5 minutes ago       Up 6 seconds                            a1
[root@node1 ~]#

  提示:以上命令都支援後面跟多個容器,表示同時操作多個容器;docker start;docker stop ;docker restart;這三個命令等同docker contaier start,docker container stop ,docker container restart;停止容器除了正常用stop停止外,還可以使用kill命令停止容器;兩者不同的是一個是給docker程式傳送15號訊號,正常停止容器,一個是傳送9號訊號,強殺容器;如下

  提示:--rm選項表示啟動一次性容器,容器停止後,自動刪除容器;

  檢視容器日誌

[root@node1 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@node1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
linux1874/myimg     latest              e408b1c6e04f        6 days ago          1.22MB
linux1874/myimg     v0.1                e408b1c6e04f        6 days ago          1.22MB
nginx               stable-alpine       ab94f84cc474        5 weeks ago         21.3MB
alpine              latest              f70734b6a266        5 weeks ago         5.61MB
alpine              v3                  f70734b6a266        5 weeks ago         5.61MB
[root@node1 ~]# docker run --name n1 -d nginx:stable-alpine
a859fda7c6021a1e5398f744826d5a62257eb1d272b622ea32dac39f83009fc9
[root@node1 ~]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS               NAMES
a859fda7c602        nginx:stable-alpine   "nginx -g 'daemon of…"   3 seconds ago       Up 2 seconds        80/tcp              n1
[root@node1 ~]# docker container inspect -f {{.NetworkSettings.Networks.bridge.IPAddress}} n1
172.17.0.2
[root@node1 ~]# curl http://172.17.0.2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@node1 ~]# elinks -dump http://172.17.0.2
                               Welcome to nginx!

   If you see this page, the nginx web server is successfully installed and
   working. Further configuration is required.

   For online documentation and support please refer to [1]nginx.org.
   Commercial support is available at [2]nginx.com.

   Thank you for using nginx.

References

   Visible links
   1. http://nginx.org/
   2. http://nginx.com/
[root@node1 ~]# docker logs n1
172.17.0.1 - - [29/May/2020:16:20:15 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
172.17.0.1 - - [29/May/2020:16:20:27 +0000] "GET / HTTP/1.1" 200 612 "-" "ELinks/0.12pre6 (textmode; Linux; -)" "-"
[root@node1 ~]# docker container logs n1
172.17.0.1 - - [29/May/2020:16:20:15 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
172.17.0.1 - - [29/May/2020:16:20:27 +0000] "GET / HTTP/1.1" 200 612 "-" "ELinks/0.12pre6 (textmode; Linux; -)" "-"
[root@node1 ~]# 

  提示:docker container logs等同docker logs命令,用於檢視指定容器的日誌資訊;跟蹤某個容器的日誌資訊可以使用-f選項;該選項同tail -f選項一樣;

  刪除容器

[root@node1 ~]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS               NAMES
d6bd7c0a29bb        nginx:stable-alpine   "nginx -g 'daemon of…"   3 seconds ago       Up 3 seconds        80/tcp              n1
[root@node1 ~]# docker container rm n1
Error response from daemon: You cannot remove a running container d6bd7c0a29bb4ea4cb60266727bd534fa086c8a70831dfea00791034bb2a84c4. Stop the container before attempting removal or force remove
[root@node1 ~]# docker container rm -f n1
n1
[root@node1 ~]# 

  提示:刪除容器必須得是停止狀態的容器才可正常刪除;如果刪除正在執行的容器需要用到-f選項;docker rm 等同docker container rm 用於刪除容器;

  附加到現執行的容器終端

  提示:容器執行服務和傳統系統上執行服務端方式有點不同,傳統執行服務都是把服務執行到後臺,而容器執行服務通常是把服務執行為前臺;所以我們進入到容器內部終端是可以直接看到nginx訪問日誌的輸出的;docker attach命令等同docker container attach;

  通過上面對映象和容器的一些基本操作,總結為一點docker客戶端命令就是對docker的資源物件做增刪查改操作,每個資源物件後面都有一些子命令支援對該資源的操作;我們可以使用docker +資源物件+--help 來檢視該資源物件支援那些操作,每個子命令的用法可以使用docker + 資源物件 + 子命令 + --help來檢視具體資源物件子命令的操作命令用法和選項的說明;有關網路和卷的操作我這裡就不多去演示;有興趣的朋友可以試試docker + 資源物件 + --help去了解每個資源物件的用法吧;

相關文章