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

来源:https://www.cnblogs.com/qiuhom-1874/archive/2020/05/30/12990424.html
-Advertisement-
Play Games

docker容器把資源抽象成對象;作為客戶端,docker這個命令其實就是在對這些抽象出來的資源對象做增刪查改的操作;docker的API是RESTful風格的API,所以它支持類似像http協議里的GET、POST、PUT、DELETE等操作;RESTful風格的API有這樣的特點;1、每一個U... ...


  前面我們聊了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 <[email protected]>"
            },
            "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 <[email protected]>"
            },
            "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去瞭解每個資源對象的用法吧;


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • Java這門語言的發展是很有意思的,它不像Python, Ruby 等完全是開源社區驅動,也不像C#,VB.NET主要由微軟操刀。它是一個以Oracle(之前是Sun)為主,各大巨頭一起參與,一起制定標準的一門語言。 想對Java添加一點特性, 得走JCP流程,巨頭們要審查,看看對自己是否有利,然後 ...
  • Blazor編譯後的文件是靜態文件,所以我們只需要一個支持靜態頁面的web server即可。 根據不同項目,會用不同的容器編排,本文已無網關的情況下為例,一步一步展示如何打包進docker 需求 HTTPS 既然無網關,直接面向互聯網,所以HTTPS顯得尤為重要 HTTP/2 TLS3.0 既然都 ...
  • C#編碼轉換主要使用了Encoding.Convert方法,它需要原編碼字元串的位元組數組作為參數,返回目標編碼的位元組數組。Encoding對象可以從字元串獲取位元組數組,又能夠從位元組數組還原字元串,因此可以將其組合用來轉碼。 ...
  • k8s 和 Docker容器技術,當前非常流行的技術. 讓人日狗的是, 這套技術棧對CN的donet 程式員不怎麼友好。娓娓道來,1. 好多鏡像都是需要梯子才能訪問; 2. window程式員天生對命令行操作陌生。3. 好多資料都是linux 等等..... 下麵我們來一起安裝部署下。 一, 安裝環 ...
  • 0. 前言 在前一篇中我們講到了Dapper的應用,但是給我們的感覺Dapper不像個ORM更像一個IDbConnection的擴展。是的,沒錯。在實際開發中我們經常用Dapper作為對EF Core的補充。當然了Dapper並不僅僅只有這些,就讓我們通過這一篇文章去讓Dapper更像一個ORM吧。 ...
  • (1):C#讀取DB文件 第一步 下載DLL文件並安裝 DLL下載地址https://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki 選用版本sqlite-netFx46-setup-bundle-x64-2015-1.0 ...
  • 幾種常見的加密方法的實現 1.ACSII碼加密 //ACSII碼加密 private static string ACSIIPWd(string rpwd) { string Ret; byte[] array = System.Text.Encoding.ASCII.GetBytes(rpwd); ...
  • 上一篇文章(https://www.cnblogs.com/meowv/p/12961014.html)集成了定時任務處理框架Hangfire,完成了一個簡單的定時任務處理解決方案。 本篇緊接著來玩一下AutoMapper,AutoMapper可以很方便的搞定我們對象到對象之間的映射關係處理,同時a ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...