Docker进阶

1、镜像管理命令

1、查看当前的镜像

1、查看当前的镜像
[root@node1 wordpress]# docker images
REPOSITORY     TAG            IMAGE ID       CREATED         SIZE
nginx          v1             a80567189207   6 days ago      141MB
bjbfd/nginx    latest         605c77e624dd   17 months ago   141MB
bjbfd/nginx    v1             605c77e624dd   17 months ago   141MB
nginx          latest         605c77e624dd   17 months ago   141MB
wordpress      latest         c3c92cc3dcb1   17 months ago   616MB
mariadb        10.6.4-focal   12e05d5da3c5   19 months ago   409MB
bjbfd/stress   latest         89e5b79daa74   7 years ago     215MB

2、查看image命令

[root@node1 wordpress]# 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        Download an image from a registry
  push        Upload an image 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.

3、下载镜像(新增)

[root@node1 wordpress]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
说明 : 
 nginx : 镜像名
 latest : 镜像版本

4、查看镜像列表(列表查询)

docker images同等docker image ls
[root@node1 wordpress]# docker image ls
REPOSITORY     TAG            IMAGE ID       CREATED         SIZE
bjbfd/nginx    latest         605c77e624dd   17 months ago   141MB
bjbfd/nginx    v1             605c77e624dd   17 months ago   141MB
nginx          latest         605c77e624dd   17 months ago   141MB
wordpress      latest         c3c92cc3dcb1   17 months ago   616MB
mariadb        10.6.4-focal   12e05d5da3c5   19 months ago   409MB
bjbfd/stress   latest         89e5b79daa74   7 years ago     215MB
说明 :
REPOSITORY : 表示镜像的仓库源
TAG : 镜像的标签
IMAGE ID : ID
CREATED : 镜像创建事件
SIZE : 镜像大小

5、查看镜像列表(列表查询)

docker images 等同于 docker image ls
[root@node1 ~]# docker image ls
REPOSITORY     TAG            IMAGE ID       CREATED         SIZE
bjbfd/nginx    latest         605c77e624dd   17 months ago   141MB
bjbfd/nginx    v1             605c77e624dd   17 months ago   141MB
nginx          latest         605c77e624dd   17 months ago   141MB
wordpress      latest         c3c92cc3dcb1   17 months ago   616MB
mariadb        10.6.4-focal   12e05d5da3c5   19 months ago   409MB
bjbfd/stress   latest         89e5b79daa74   7 years ago     215MB

说明 :
REPOSITORY : 表示镜像的仓库源
TAG : 镜像的标签
IMAGE ID : 镜像ID
CREATED : 镜像创建时间
SIZE : 镜像大小
同一个仓库源可以有多个TAG,代表这个仓库源的不同版本,我们使用REPOSITORY:TAG来定义不同的镜像

6、运行容器

[root@node1 ~]# docker run -itd nginx:latest
0243fb0c27018ecba95d0a4afafa452017b1e91cb3b62568a7b2e322038a67b2
说明 :
-i : 交互式操作
-t : 终端
-d : 守护进程运行
nginx:latest : 镜像

7、查看镜像详情(详情查询)

docker image inspect nginx:latest
[root@node1 ~]# docker image inspect nginx:latest
[
    {
        "Id": "sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85",
        "RepoTags": [
            "bjbfd/nginx:latest",
            "bjbfd/nginx:v1",
            "nginx:latest"
        ],
        "RepoDigests": [
            "bjbfd/nginx@sha256:ee89b00528ff4f02f2405e4ee221743ebc3f8e8dd0bfd5c4c20a2fa2aaa7ede3",
            "nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2021-12-29T19:28:29.892199479Z",
        "Container": "ca3e48389f7160bc9d9a892d316fcbba459344ee3679998739b1c3cd8e56f7da",
        "ContainerConfig": {
            "Hostname": "ca3e48389f71",
            "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.21.5",
                "NJS_VERSION=0.7.1",
                "PKG_RELEASE=1~bullseye"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"nginx\" \"-g\" \"daemon off;\"]"
            ],
            "Image": "sha256:82941edee2f4d17c55563bb926387c3ae39fa1a99777f088bc9d3db885192209",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": [
                "/docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers "
            },
            "StopSignal": "SIGQUIT"
        },
        "DockerVersion": "20.10.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.21.5",
                "NJS_VERSION=0.7.1",
                "PKG_RELEASE=1~bullseye"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "Image": "sha256:82941edee2f4d17c55563bb926387c3ae39fa1a99777f088bc9d3db885192209",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": [
                "/docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers "
            },
            "StopSignal": "SIGQUIT"
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 141479488,
        "VirtualSize": 141479488,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/b00f6bf28e2dc408c796f09ad94d3f28c64e0a28cbdd6964087e2b26228b2328/diff:/var/lib/docker/overlay2/ca904171aa0883178d573b413ca24013e624e90395197a2e4914ebbae8388e7d/diff:/var/lib/docker/overlay2/4885892662a64db663878aee6f36c32d8196195e47a20ff2e0680256e28c20ac/diff:/var/lib/docker/overlay2/4eb0162b58d4f18bca9c0d48fb39f275dfbf92a59227dc380b7d36f614b45a4e/diff:/var/lib/docker/overlay2/a8c3e0d43b8b27fd19a575c3051c9cea6450d39d8fbb58c3d34c7d8b898d3b97/diff",
                "MergedDir": "/var/lib/docker/overlay2/9d155c02707c57771128e397e6e43c9c9691a544e8e5749b87da9ebc5857f554/merged",
                "UpperDir": "/var/lib/docker/overlay2/9d155c02707c57771128e397e6e43c9c9691a544e8e5749b87da9ebc5857f554/diff",
                "WorkDir": "/var/lib/docker/overlay2/9d155c02707c57771128e397e6e43c9c9691a544e8e5749b87da9ebc5857f554/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f",
                "sha256:e379e8aedd4d72bb4c529a4ca07a4e4d230b5a1d3f7a61bc80179e8f02421ad8",
                "sha256:b8d6e692a25e11b0d32c5c3dd544b71b1085ddc1fddad08e68cbd7fda7f70221",
                "sha256:f1db227348d0a5e0b99b15a096d930d1a69db7474a1847acbc31f05e4ef8df8c",
                "sha256:32ce5f6a5106cc637d09a98289782edf47c32cb082dc475dd47cbf19a4f866da",
                "sha256:d874fd2bc83bb3322b566df739681fbd2248c58d3369cb25908d68e7ed6040a6"
            ]
        },
        "Metadata": {
            "LastTagTime": "2023-05-29T22:25:44.056009028+08:00"
        }
    }
]

ContainerConfig : 容器配置
GraphDriver : 镜像驱动
RootFS : Docker文件系统

8、查看镜像构建历史(历史查询)

[root@node1 ~]# docker image history 605c77e624dd
IMAGE          CREATED         CREATED BY                                       SIZE      COMMENT
605c77e624dd   17 months ago   /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B
      17 months ago   /bin/sh -c #(nop)  STOPSIGNAL SIGQUIT            0B
      17 months ago   /bin/sh -c #(nop)  EXPOSE 80                     0B
      17 months ago   /bin/sh -c #(nop)  ENTRYPOINT ["/docker-entr…   0B
      17 months ago   /bin/sh -c #(nop) COPY file:09a214a3e07c919a…   4.61kB
      17 months ago   /bin/sh -c #(nop) COPY file:0fd5fca330dcd6a7…   1.04kB
      17 months ago   /bin/sh -c #(nop) COPY file:0b866ff3fc1ef5b0…   1.96kB
      17 months ago   /bin/sh -c #(nop) COPY file:65504f71f5855ca0…   1.2kB
      17 months ago   /bin/sh -c set -x     && addgroup --system -…   61.1MB
      17 months ago   /bin/sh -c #(nop)  ENV PKG_RELEASE=1~bullseye    0B
      17 months ago   /bin/sh -c #(nop)  ENV NJS_VERSION=0.7.1         0B
      17 months ago   /bin/sh -c #(nop)  ENV NGINX_VERSION=1.21.5      0B
      17 months ago   /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B
      17 months ago   /bin/sh -c #(nop)  CMD ["bash"]                  0B
      17 months ago   /bin/sh -c #(nop) ADD file:09675d11695f65c55…   80.4MB

9、查看仓库镜像版本

Docker进阶_第1张图片

10、删除镜像

[root@node1 ~]# docker rmi bjbfd/stress:latest
Untagged: bjbfd/stress:latest
Untagged: bjbfd/stress@sha256:9d78f7f2cda8f1d9e8014bb087f6c53ee964400493adcc1f8cc3515d7b25321d
Deleted: sha256:89e5b79daa74679427a7c5dc8e24c8716f2a649aee03d8eeaed700ce7e2f8c07
Deleted: sha256:1bae8237f7ac02fc5b42c0c87bc634fc689baad852f16adbc4a5db757f7fd1e9
Deleted: sha256:c9b25ca08023c9a9d6a5cc555770aacd71d413a70b2a5cdd39d6e070e4e65732
Deleted: sha256:89a5271a5486c5d5ff56be49c9867b1a7ef5cae3ef1e3fd1a2d7e714f78f5fab
Deleted: sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef

11、删除不再使用的镜像

[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED          STATUS          PORTS     NAMES
0243fb0c2701   nginx:latest   "/docker-entrypoint.…"   13 minutes ago   Up 13 minutes   80/tcp    confident_noether
[root@node1 ~]# docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
[root@node1 ~]# docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
[root@node1 ~]# docker images
REPOSITORY    TAG            IMAGE ID       CREATED         SIZE
bjbfd/nginx   latest         605c77e624dd   17 months ago   141MB
bjbfd/nginx   v1             605c77e624dd   17 months ago   141MB
nginx         latest         605c77e624dd   17 months ago   141MB
wordpress     latest         c3c92cc3dcb1   17 months ago   616MB
mariadb       10.6.4-focal   12e05d5da3c5   19 months ago   409MB
[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: bjbfd/nginx:latest
untagged: bjbfd/nginx:v1
untagged: bjbfd/nginx@sha256:ee89b00528ff4f02f2405e4ee221743ebc3f8e8dd0bfd5c4c20a2fa2aaa7ede3
untagged: wordpress:latest
untagged: wordpress@sha256:fc33b796b04162a0db2e9ea9b4c361a07058b21597b1317ad9ab3ea4593de241
deleted: sha256:c3c92cc3dcb1a903fed0374a837f38d716ae104d0e4c9705bddb53a76419534d
deleted: sha256:e03d610209901c4c643d9787f53e556f3a034ece25b597205d8333db2ff81872
deleted: sha256:d016493a41b04f201d91ac317b607a0fc2f87a8d825d6dfb6b8dc1bf2fba4efe
deleted: sha256:7904e413594a78ddb5e5909909e5c37255d7be1ada4b4bf16e33368200ddea2d
deleted: sha256:291dc2654c9399be32d82521744e650eb3d899d6356856dfd497f180782b76b8
deleted: sha256:2c7d4e23a0ce5d99dc09041e86f5bcdc2404d703e769189fddc8cc45322bbda9
deleted: sha256:d3e712f7ab059427794f7f209f448f94fc60dee3e4e40eb82cd99605ab55af3c
deleted: sha256:10ab8416164c9b2c408ac1317419e9dd113091f6290d33f73cf1cc9c9781fb2b
deleted: sha256:d61093d47caf389668eb96344afab2454620a5c586b1de3859e17d255a19ba89
deleted: sha256:3cccde4cd0f42cdd4b0a2c088a88785183e11b4e7c425ff4c4f54326e8e9764b
deleted: sha256:1053961a55147906b29c3be9c1186d5d9563db08c1e5b63c4e7b286f3dc043f6
deleted: sha256:1e1b1a779971b332e7e954d0219153cd320cdd27521a15a97da4151ef45e3d7d
deleted: sha256:682226507754fd5f59ce67ff0801c9df859b106cd5a3db24defb073650cf7fb5
deleted: sha256:782d3b9de219e51086f1cab57778a10e7a88784008cb8b629e02e173c6683cc1
deleted: sha256:c3d02f3fbe0afe22bc647345d3d217f2a26133062c1ada547645afdd1243cacc
deleted: sha256:eec2c2512d15a9611866e4ddf17af2c480009045dbca2a3a22f30becb2050ae2
deleted: sha256:8c360a4ebc0a45f7de7228d7d4ae497ddcf9c73218c5b4e628188f22cae1c663
deleted: sha256:ee5205a969dcf1186060d0b8719db08647c3f86ccf33770b83b6ef1c989258e1
deleted: sha256:6b322a9c05d5df05b87396796502965c8e6212aeb07ced777ed206f660c7a098
deleted: sha256:c688355f4fe75990c63df6c38a962e3cadfaa0d84c826a920cf2a43fa0975270
deleted: sha256:895915dadaf75a7370a1817ba4e54f0ee5b329b81aab80a3552736c10b065fc5
untagged: mariadb:10.6.4-focal
untagged: mariadb@sha256:c014ba1efc5dbd711d0520c7762d57807f35549de3414eb31e942a420c8a2ed2
deleted: sha256:12e05d5da3c5223e9877e8eb90d68560ff66cedcb955131061d60d093a908f0c
deleted: sha256:ffb9fb143b85a783ff0e83cf0c594c8fd6838b889dee7d14c0a2a3e0c9df419d
deleted: sha256:067860988e6658468a17d2d0d57fb3e74ef45bf3b38167a796f5bfa61d14ed4d
deleted: sha256:52da81964394b8f090e4e3405eb3478cbb274d84e023680b5eb21be463d4fb72
deleted: sha256:4bd99e1fe00faaa2737267cb775982ad444ad62999d37df6a9fa92f458d9f10c
deleted: sha256:9f4f0dd33c9816176e9bd3077cee199d0f14bd05ef5b7e2f01a80a1ea3d48316
deleted: sha256:46c59a61ed6209c1e6a55e8811345216f5dfe704d4411ba3e37e1cb345d7deeb
deleted: sha256:4bf277548ca07781855a8ce8c8b299a89fd5742151003f83053026e0b01f8470
deleted: sha256:715a1b962166ede06c7a0e87d068a4b686e6066e0eca5ecab6f4d6cfab2121fe
deleted: sha256:97ab3baee34d0c75ee10e65c63a06cbc87d20d695c17d14ad565d4ff1b8dc2ca
deleted: sha256:9f54eef412758095c8079ac465d494a2872e02e90bf1fb5f12a1641c0d1bb78b

Total reclaimed space: 944.5MB
[root@node1 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        latest    605c77e624dd   17 months ago   141MB

docker image prune -a 会清理掉所有不再运行的容器的镜像

12、备份镜像

[root@node1 ~]# docker save -o nginx.tar nginx:latest
[root@node1 ~]# ls
nginx.tar

13、加载镜像

[root@node1 ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
[root@node1 ~]# ls
nginx.tar
[root@node1 ~]# docker image load -i nginx.tar
Loaded image: nginx:latest
[root@node1 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        latest    605c77e624dd   17 months ago   141MB

14、导入镜像

[root@node1 ~]# docker image import nginx.tar
sha256:c1f8bc3f7d5a0882c590483936f9edf23f51e995e84dafaba301071f58cee6f3
[root@node1 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
           c1f8bc3f7d5a   5 seconds ago   146MB

15、构建镜像

[root@node1 nginx]# cat Dockerfile
FROM nginx
RUN echo '这是一个本地构建的nginx镜像' > /usr/share/nginx/html/index.html
[root@node1 nginx]# docker build -t bjbfd/nginx:v1 .
[+] Building 0.0s (6/6) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                                                0.0s
 => => transferring dockerfile: 133B                                                                                                                                                                0.0s
 => [internal] load .dockerignore                                                                                                                                                                   0.0s
 => => transferring context: 2B                                                                                                                                                                     0.0s
 => [internal] load metadata for docker.io/library/nginx:latest                                                                                                                                     0.0s
 => [1/2] FROM docker.io/library/nginx                                                                                                                                                              0.0s
 => CACHED [2/2] RUN echo '这是一个本地构建的nginx镜像' > /usr/share/nginx/html/index.html                                                                                                                     0.0s
 => exporting to image                                                                                                                                                                              0.0s
 => => exporting layers                                                                                                                                                                             0.0s
 => => writing image sha256:a805671892075472b21360ead42fb53a2ad798b0ab56bab450c52d428ce1441b                                                                                                        0.0s
 => => naming to docker.io/bjbfd/nginx:v1                                                                                                                                                           0.0s
[root@node1 nginx]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
bjbfd/nginx   v1        a80567189207   6 days ago      141MB
nginx         latest    605c77e624dd   17 months ago   141MB

-t : 指定要创建的目标镜像名
. : Dockerfile文件所在的目录,可以指定Dockerfile的绝对路径

16、给镜像打Tag

[root@node1 nginx]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
bjbfd/nginx   v1        a80567189207   6 days ago      141MB
nginx         latest    605c77e624dd   17 months ago   141MB
[root@node1 nginx]# docker image tag bjbfd/nginx:v1 bjbfd/nginx:v2
[root@node1 nginx]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
bjbfd/nginx   v1        a80567189207   6 days ago      141MB
bjbfd/nginx   v2        a80567189207   6 days ago      141MB
nginx         latest    605c77e624dd   17 months ago   141MB

17、推送镜像仓库

[root@node1 nginx]# docker push bjbfd/nginx:v1
The push refers to repository [docker.io/bjbfd/nginx]
b84ae2a5c7a5: Pushed
d874fd2bc83b: Layer already exists
32ce5f6a5106: Layer already exists
f1db227348d0: Layer already exists
b8d6e692a25e: Layer already exists
e379e8aedd4d: Layer already exists
2edcec3590a4: Layer already exists
v1: digest: sha256:76d0541f94a712a86c3e1c5683cc0e03ed5e500360a73dcaa7b085ff2a540c04 size: 1777

2、镜像管理命令

1、运行容器

[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@node1 ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
bjbfd/nginx   v1        a80567189207   7 days ago      141MB
bjbfd/nginx   v2        a80567189207   7 days ago      141MB
bjbfd/nginx   v3        a80567189207   7 days ago      141MB
nginx         latest    605c77e624dd   17 months ago   141MB
[root@node1 ~]# docker run -itd nginx:latest]
docker: invalid reference format.
See 'docker run --help'.
[root@node1 ~]# docker run -itd nginx:latest
d7ac05b931e96fc4c5a15ca82cb7b6a093f9f28fd70debc9c364ac6f85a52ecf
[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS         PORTS     NAMES
d7ac05b931e9   nginx:latest   "/docker-entrypoint.…"   3 seconds ago   Up 2 seconds   80/tcp    cranky_hertz

2、创建一个容器

[root@node1 ~]# docker create nginx:latest
983199af829ab0125b246a6e311823ec908628fd91276c3b7d464716a3edb30a
[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS    PORTS     NAMES
983199af829a   nginx:latest   "/docker-entrypoint.…"   3 seconds ago   Created             gallant_boyd
[root@node1 ~]# docker exec -it 983199af829a /bin/bash
Error response from daemon: Container 983199af829ab0125b246a6e311823ec908628fd91276c3b7d464716a3edb30a is not running
注意 : 发现了没有,创建完毕容器,是不能docker exec -it进入容器的,发现状态是CREATED状态,是需要进行start的才能运行

3、启动容器

[root@node1 ~]# docker start 983199af829a
983199af829a
[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS          PORTS     NAMES
983199af829a   nginx:latest   "/docker-entrypoint.…"   2 minutes ago   Up 14 seconds   80/tcp    gallant_boyd
[root@node1 ~]# docker exec -it 983199af829a /bin/bash
root@983199af829a:/# curl 127.0.0.1



Welcome to nginx!



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 nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

4、停止容器

[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS              PORTS     NAMES
983199af829a   nginx:latest   "/docker-entrypoint.…"   3 minutes ago   Up About a minute   80/tcp    gallant_boyd
[root@node1 ~]# docker stop 983199af829a
983199af829a
[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS                     PORTS     NAMES
983199af829a   nginx:latest   "/docker-entrypoint.…"   3 minutes ago   Exited (0) 3 seconds ago             gallant_boyd
[root@node1 ~]# docker start 983199af829a
983199af829a
[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS         PORTS     NAMES
983199af829a   nginx:latest   "/docker-entrypoint.…"   4 minutes ago   Up 2 seconds   80/tcp    gallant_boyd

注意 : 发现停止容器之后还是可以进行start的

5、查看容器列表

docker ps -a和docker container ls是相同的命令,都可以

[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS              PORTS     NAMES
983199af829a   nginx:latest   "/docker-entrypoint.…"   5 minutes ago   Up About a minute   80/tcp    gallant_boyd
[root@node1 ~]# docker container ls
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS              PORTS     NAMES
983199af829a   nginx:latest   "/docker-entrypoint.…"   5 minutes ago   Up About a minute   80/tcp    gallant_boyd

6、重启容器

[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS         PORTS     NAMES
983199af829a   nginx:latest   "/docker-entrypoint.…"   6 minutes ago   Up 2 minutes   80/tcp    gallant_boyd
[root@node1 ~]# docker restart 983199af829a
983199af829a
[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS         PORTS     NAMES
983199af829a   nginx:latest   "/docker-entrypoint.…"   6 minutes ago   Up 2 seconds   80/tcp    gallant_boyd

7、杀死容器

[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS              PORTS     NAMES
983199af829a   nginx:latest   "/docker-entrypoint.…"   7 minutes ago   Up About a minute   80/tcp    gallant_boyd
[root@node1 ~]# docker kill 983199af829a
983199af829a
[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS                       PORTS     NAMES
983199af829a   nginx:latest   "/docker-entrypoint.…"   8 minutes ago   Exited (137) 9 seconds ago             gallant_boyd
[root@node1 ~]# docker start 983199af829a
983199af829a
[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS         PORTS     NAMES
983199af829a   nginx:latest   "/docker-entrypoint.…"   8 minutes ago   Up 5 seconds   80/tcp    gallant_boyd

注意 : kill掉的容器也是可以重新start的,只是标识是强制退出还是柔性的退出而已

8、挂起容器

[root@node1 ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
bjbfd/nginx   v1        a80567189207   7 days ago      141MB
bjbfd/nginx   v2        a80567189207   7 days ago      141MB
bjbfd/nginx   v3        a80567189207   7 days ago      141MB
nginx         latest    605c77e624dd   17 months ago   141MB
[root@node1 ~]# docker run -itd -p 8888:80 nginx:latest
5ea91b92d19307772aa39bbdd73b822104e89d62a58b7d4d8a3cb76b15286dcc
[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS         PORTS                                   NAMES
5ea91b92d193   nginx:latest   "/docker-entrypoint.…"   6 minutes ago   Up 6 minutes   0.0.0.0:8888->80/tcp, :::8888->80/tcp   flamboyant_shannon
[root@node1 ~]# curl 127.0.0.1:8888



Welcome to nginx!



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 nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

[root@node1 ~]# docker pause 5ea91b92d193 5ea91b92d193 [root@node1 ~]# curl 127.0.0.1:8888 ^C [root@node1 ~]# 注意 : 其实就是让容器不堆外服务

9、接挂容器

[root@node1 ~]# docker unpause 5ea91b92d193
5ea91b92d193
[root@node1 ~]# curl 127.0.0.1:8888



Welcome to nginx!



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 nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

10、删除容器

[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS         PORTS                                   NAMES
5ea91b92d193   nginx:latest   "/docker-entrypoint.…"   9 minutes ago   Up 9 minutes   0.0.0.0:8888->80/tcp, :::8888->80/tcp   flamboyant_shannon
[root@node1 ~]# docker rm -f 5ea91b92d193
5ea91b92d193

11、删除没有用容器

[root@node1 ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
bjbfd/nginx   v1        a80567189207   7 days ago      141MB
bjbfd/nginx   v2        a80567189207   7 days ago      141MB
bjbfd/nginx   v3        a80567189207   7 days ago      141MB
nginx         latest    605c77e624dd   17 months ago   141MB
[root@node1 ~]# docker run -itd nginx:latest
d777a7f12f3e1fe3ad5e2b50e119a25a6381cbb86862dc2ec5d7b950b7bfb2f6
[root@node1 ~]# docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
[root@node1 ~]# docker stop d777a7f12f3e1fe3ad5e2b50e119a25a6381cbb86862dc2ec5d7b950b7bfb2f6
d777a7f12f3e1fe3ad5e2b50e119a25a6381cbb86862dc2ec5d7b950b7bfb2f6
[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED          STATUS                     PORTS     NAMES
d777a7f12f3e   nginx:latest   "/docker-entrypoint.…"   19 seconds ago   Exited (0) 2 seconds ago             keen_galois
[root@node1 ~]# docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
d777a7f12f3e1fe3ad5e2b50e119a25a6381cbb86862dc2ec5d7b950b7bfb2f6

Total reclaimed space: 1.093kB
[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@node1 ~]#

12、查看容器日志

[root@node1 ~]# docker logs -n 100  e2d34d63ca4c
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/06/03 07:47:03 [notice] 1#1: using the "epoll" event method
2023/06/03 07:47:03 [notice] 1#1: nginx/1.21.5
2023/06/03 07:47:03 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2023/06/03 07:47:03 [notice] 1#1: OS: Linux 3.10.0-1127.19.1.el7.x86_64
2023/06/03 07:47:03 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/06/03 07:47:03 [notice] 1#1: start worker processes
2023/06/03 07:47:03 [notice] 1#1: start worker process 31
2023/06/03 07:47:03 [notice] 1#1: start worker process 32

13、进入容器

[root@node1 ~]# docker exec -it e2d34d63ca4c /bin/bash
root@e2d34d63ca4c:/#

14、拷贝容器文件

将本地文件拷贝到容器中
[root@node1 ~]# ls
nginx.tar
[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS         PORTS     NAMES
e2d34d63ca4c   nginx:latest   "/docker-entrypoint.…"   3 minutes ago   Up 3 minutes   80/tcp    youthful_keller
[root@node1 ~]# docker cp nginx.tar e2d34d63ca4c:/
Successfully copied 146MB to e2d34d63ca4c:/
[root@node1 ~]# docker exec -it e2d34d63ca4c /bin/bash
root@e2d34d63ca4c:/# ls
bin  boot  dev    docker-entrypoint.d  docker-entrypoint.sh  etc    home  lib  lib64  media  mnt  nginx.tar  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@e2d34d63ca4c:/# ls nginx.tar
nginx.tar

将容器文件拷贝到本地
root@e2d34d63ca4c:/# touch test.txt
[root@node1 ~]# docker cp e2d34d63ca4c:/test.txt .
Successfully copied 1.54kB to /root/.
[root@node1 ~]# ls
nginx.tar  test.txt
[root@node1 ~]#

15、容器重命名

[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS         PORTS     NAMES
e2d34d63ca4c   nginx:latest   "/docker-entrypoint.…"   6 minutes ago   Up 6 minutes   80/tcp    youthful_keller
[root@node1 ~]# docker rename e2d34d63ca4c nginx_container
[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS         PORTS     NAMES
e2d34d63ca4c   nginx:latest   "/docker-entrypoint.…"   6 minutes ago   Up 6 minutes   80/tcp    nginx_container
[root@node1 ~]#

16、更新容器信息

[root@node1 ~]# docker update -m 128m --memory-swap -1 e2d34d63ca4c
e2d34d63ca4c

17、查看容器所占的资源

[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED          STATUS          PORTS     NAMES
e2d34d63ca4c   nginx:latest   "/docker-entrypoint.…"   14 minutes ago   Up 14 minutes   80/tcp    nginx_container
[root@node1 ~]# docker stats e2d34d63ca4c
CONTAINER ID   NAME              CPU %     MEM USAGE / LIMIT   MEM %     NET I/O     BLOCK I/O       PIDS
e2d34d63ca4c   nginx_container   0.01%     2.094MiB / 128MiB   1.64%     656B / 0B   193kB / 4.1kB   3
CONTAINER ID   NAME              CPU %     MEM USAGE / LIMIT   MEM %     NET I/O     BLOCK I/O       PIDS

18、查看端口

[root@node1 ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
bjbfd/nginx   v1        a80567189207   7 days ago      141MB
bjbfd/nginx   v2        a80567189207   7 days ago      141MB
bjbfd/nginx   v3        a80567189207   7 days ago      141MB
nginx         latest    605c77e624dd   17 months ago   141MB
[root@node1 ~]# docker run -itd -p 8888:80 nginx:latest
7f916ec2c2ad7c0cf8a8e90101edda75e152885db88f6316e5317f05e408344c
[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS         PORTS                                   NAMES
7f916ec2c2ad   nginx:latest   "/docker-entrypoint.…"   3 seconds ago   Up 2 seconds   0.0.0.0:8888->80/tcp, :::8888->80/tcp   charming_benz
[root@node1 ~]# docker port 7f916ec2c2ad
80/tcp -> 0.0.0.0:8888
80/tcp -> [::]:8888
[root@node1 ~]#

19、查看容器进程

[root@node1 ~]# docker top 7f916ec2c2ad
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                4007                3986                0                   16:03               pts/0               00:00:00            nginx: master process nginx -g daemon off;
101                 4051                4007                0                   16:03               pts/0               00:00:00            nginx: worker process
101                 4052                4007                0                   16:03               pts/0               00:00:00            nginx: worker process

20、从容器创建一个新的镜像

[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS         PORTS                                   NAMES
7f916ec2c2ad   nginx:latest   "/docker-entrypoint.…"   4 minutes ago   Up 4 minutes   0.0.0.0:8888->80/tcp, :::8888->80/tcp   charming_benz
[root@node1 ~]# docker commit -a "journey" -m "add by journey" 7f916ec2c2ad
sha256:6d4beeb788dda4d53f991789736373a4488f048bd6f34236aa44f0e7e80c7143
[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS         PORTS                                   NAMES
7f916ec2c2ad   nginx:latest   "/docker-entrypoint.…"   4 minutes ago   Up 4 minutes   0.0.0.0:8888->80/tcp, :::8888->80/tcp   charming_benz
[root@node1 ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED          SIZE
            6d4beeb788dd   12 seconds ago   141MB
bjbfd/nginx   v1        a80567189207   7 days ago       141MB
bjbfd/nginx   v2        a80567189207   7 days ago       141MB
bjbfd/nginx   v3        a80567189207   7 days ago       141MB
nginx         latest    605c77e624dd   17 months ago    141MB

21、查看容器信息

[root@node1 ~]# docker inspect 7f916ec2c2ad
[
    {
        "Id": "7f916ec2c2ad7c0cf8a8e90101edda75e152885db88f6316e5317f05e408344c",
        "Created": "2023-06-03T08:03:57.016540909Z",
        "Path": "/docker-entrypoint.sh",
        "Args": [
            "nginx",
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 4007,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2023-06-03T08:03:57.268472187Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85",
        "ResolvConfPath": "/var/lib/docker/containers/7f916ec2c2ad7c0cf8a8e90101edda75e152885db88f6316e5317f05e408344c/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/7f916ec2c2ad7c0cf8a8e90101edda75e152885db88f6316e5317f05e408344c/hostname",
        "HostsPath": "/var/lib/docker/containers/7f916ec2c2ad7c0cf8a8e90101edda75e152885db88f6316e5317f05e408344c/hosts",
        "LogPath": "/var/lib/docker/containers/7f916ec2c2ad7c0cf8a8e90101edda75e152885db88f6316e5317f05e408344c/7f916ec2c2ad7c0cf8a8e90101edda75e152885db88f6316e5317f05e408344c-json.log",
        "Name": "/charming_benz",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "80/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "8888"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "ConsoleSize": [
                28,
                201
            ],
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": [],
            "BlkioDeviceWriteBps": [],
            "BlkioDeviceReadIOps": [],
            "BlkioDeviceWriteIOps": [],
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/8e6dca3763335b09a41eaa6d81211cebaee9602c65002329a8489b21b55f84d0-init/diff:/var/lib/docker/overlay2/9d155c02707c57771128e397e6e43c9c9691a544e8e5749b87da9ebc5857f554/diff:/var/lib/docker/overlay2/b00f6bf28e2dc408c796f09ad94d3f28c64e0a28cbdd6964087e2b26228b2328/diff:/var/lib/docker/overlay2/ca904171aa0883178d573b413ca24013e624e90395197a2e4914ebbae8388e7d/diff:/var/lib/docker/overlay2/4885892662a64db663878aee6f36c32d8196195e47a20ff2e0680256e28c20ac/diff:/var/lib/docker/overlay2/4eb0162b58d4f18bca9c0d48fb39f275dfbf92a59227dc380b7d36f614b45a4e/diff:/var/lib/docker/overlay2/a8c3e0d43b8b27fd19a575c3051c9cea6450d39d8fbb58c3d34c7d8b898d3b97/diff",
                "MergedDir": "/var/lib/docker/overlay2/8e6dca3763335b09a41eaa6d81211cebaee9602c65002329a8489b21b55f84d0/merged",
                "UpperDir": "/var/lib/docker/overlay2/8e6dca3763335b09a41eaa6d81211cebaee9602c65002329a8489b21b55f84d0/diff",
                "WorkDir": "/var/lib/docker/overlay2/8e6dca3763335b09a41eaa6d81211cebaee9602c65002329a8489b21b55f84d0/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "7f916ec2c2ad",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.21.5",
                "NJS_VERSION=0.7.1",
                "PKG_RELEASE=1~bullseye"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "Image": "nginx:latest",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": [
                "/docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers "
            },
            "StopSignal": "SIGQUIT"
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "0277334aaf076407c0d61b71d9db0832af2f4d36ca0233e986e093a76d4d5e99",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "8888"
                    },
                    {
                        "HostIp": "::",
                        "HostPort": "8888"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/0277334aaf07",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "cb94381c2ee768a2b16e3337df23140f243721ce071eedd8ac78eba010a12669",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "5ceeded040a83e20aa47366ee6fb8c6abb7cdf048f9eb235fec7b4247292c1b4",
                    "EndpointID": "cb94381c2ee768a2b16e3337df23140f243721ce071eedd8ac78eba010a12669",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

3、仓库管理命令

1、Docker Hub

仓库(Repository)是集中存放镜像的地方。Docker官方维护了一个公共仓库Docker Hub。大部分需求都可以通过在Docker Hub中直接下载镜像来实现

2、Docker Hub注册

Docker进阶_第2张图片

3、Docker Hub 登录

[root@node1 ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: bjbfd
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

4、Docker Hub 退出

[root@node1 ~]# docker logout
Removing login credentials for https://index.docker.io/v1/

5、镜像搜索

[root@node1 ~]# docker search nginx
NAME                                              DESCRIPTION                                      STARS     OFFICIAL   AUTOMATED
nginx                                             Official build of Nginx.                         18593     [OK]
unit                                              Official build of NGINX Unit: Universal Web …   5         [OK]
nginxproxy/nginx-proxy                            Automated Nginx reverse proxy for docker con…   88
nginxproxy/acme-companion                         Automated ACME SSL certificate generation fo…   113
bitnami/nginx                                     Bitnami nginx Docker Image                       164                  [OK]
bitnami/nginx-ingress-controller                  Bitnami Docker Image for NGINX Ingress Contr…   29                   [OK]
ubuntu/nginx                                      Nginx, a high-performance reverse proxy & we…   95
nginxproxy/docker-gen                             Generate files from docker container meta-da…   12
kasmweb/nginx                                     An Nginx image based off nginx:alpine and in…   6
rancher/nginx-ingress-controller                                                                   11
rancher/nginx-ingress-controller-defaultbackend                                                    2
bitnami/nginx-exporter                                                                             3
rancher/nginx                                                                                      2
rapidfort/nginx-ib                                RapidFort optimized, hardened image for NGIN…   10
rapidfort/nginx                                   RapidFort optimized, hardened image for NGINX    14
vmware/nginx-photon                                                                                1
bitnami/nginx-ldap-auth-daemon                                                                     3
rapidfort/nginx-official                          RapidFort optimized, hardened image for NGIN…   10
vmware/nginx                                                                                       2
rancher/nginx-conf                                                                                 0
linuxserver/nginx                                 An Nginx container, brought to you by LinuxS…   201
nginxproxy/forego                                 Foreman in Go                                    0
privatebin/nginx-fpm-alpine                       PrivateBin running on an Nginx, php-fpm & Al…   72                   [OK]
bitnami/nginx-intel                                                                                1
bitnamicharts/nginx                                                                                0
[root@node1 ~]#

6、下载镜像到本地

[root@node1 ~]# docker pull nginx:1.16.0
1.16.0: Pulling from library/nginx
9fc222b64b0a: Pull complete
30e9fc7d9c5b: Pull complete
4b3a8aeaa40e: Pull complete
Digest: sha256:3e373fd5b8d41baeddc24be311c5c6929425c04cabf893b874ac09b72a798010
Status: Downloaded newer image for nginx:1.16.0
docker.io/library/nginx:1.16.0
[root@node1 ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
            6d4beeb788dd   17 hours ago    141MB
bjbfd/nginx   v1        a80567189207   7 days ago      141MB
bjbfd/nginx   v2        a80567189207   7 days ago      141MB
bjbfd/nginx   v3        a80567189207   7 days ago      141MB
nginx         latest    605c77e624dd   17 months ago   141MB
nginx         1.16.0    ae893c58d83f   3 years ago     109MB
[root@node1 ~]#

7、推送镜像

[root@node1 nginx]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
bjbfd/nginx   v4        a80567189207   7 days ago    141MB
nginx         1.16.0    ae893c58d83f   3 years ago   109MB
[root@node1 nginx]# docker push bjbfd/nginx:v4
The push refers to repository [docker.io/bjbfd/nginx]
b84ae2a5c7a5: Layer already exists
d874fd2bc83b: Layer already exists
32ce5f6a5106: Layer already exists
f1db227348d0: Layer already exists
b8d6e692a25e: Layer already exists
e379e8aedd4d: Layer already exists
2edcec3590a4: Layer already exists
v4: digest: sha256:76d0541f94a712a86c3e1c5683cc0e03ed5e500360a73dcaa7b085ff2a540c04 size: 1777

4、网络管理命令

1、网络管理帮助文档

[root@node1 nginx]# docker network --help

Usage:  docker network COMMAND

Manage networks

Commands:
  connect     Connect a container to a network
  create      Create a network
  disconnect  Disconnect a container from a network
  inspect     Display detailed information on one or more networks
  ls          List networks
  prune       Remove all unused networks
  rm          Remove one or more networks

Run 'docker network COMMAND --help' for more information on a command.

2、新建网络

[root@node1 nginx]# docker network create journey_network
13c87663bccf0eb404edc4441c9cb819072faeebfceed4ad6bf07b2ca1f8f3fc

3、查看网络列表

[root@node1 nginx]# docker network ls
NETWORK ID     NAME                DRIVER    SCOPE
743d1d364d1d   bridge              bridge    local
4882bc599fcc   docker_gwbridge     bridge    local
06d4d17eacbf   host                host      local
zylych55j9kk   ingress             overlay   swarm
13c87663bccf   journey_network     bridge    local
276755b5e003   none                null      local
0e99973cc931   wordpress_default   bridge    local

4、删除网络

[root@node1 nginx]# docker network rm journey_network
journey_network

5、删除未使用的网络

[root@node1 nginx]# docker network prune
WARNING! This will remove all custom networks not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Networks:
wordpress_defaul

6、容器的网络设置

[root@node1 nginx]# docker network ls
NETWORK ID     NAME              DRIVER    SCOPE
743d1d364d1d   bridge            bridge    local
4882bc599fcc   docker_gwbridge   bridge    local
06d4d17eacbf   host              host      local
zylych55j9kk   ingress           overlay   swarm
f905066b895c   journey_network   bridge    local
276755b5e003   none              null      local
[root@node1 nginx]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED          STATUS          PORTS     NAMES
d92e103aae40   ae893c58d83f   "nginx -g 'daemon of…"   48 seconds ago   Up 47 seconds   80/tcp    unruffled_curie
[root@node1 nginx]# docker inspect d92e103aae40
[
    {
        "Id": "d92e103aae404fe3e4944d0eb4bb55fd1483b5de8c295d15599489acede221e2",
        "Created": "2023-06-04T01:41:25.482832723Z",
        "Path": "nginx",
        "Args": [
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 2515,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2023-06-04T01:41:25.735005762Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:ae893c58d83fe2bd391fbec97f5576c9a34fea55b4ee9daf15feb9620b14b226",
        "ResolvConfPath": "/var/lib/docker/containers/d92e103aae404fe3e4944d0eb4bb55fd1483b5de8c295d15599489acede221e2/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/d92e103aae404fe3e4944d0eb4bb55fd1483b5de8c295d15599489acede221e2/hostname",
        "HostsPath": "/var/lib/docker/containers/d92e103aae404fe3e4944d0eb4bb55fd1483b5de8c295d15599489acede221e2/hosts",
        "LogPath": "/var/lib/docker/containers/d92e103aae404fe3e4944d0eb4bb55fd1483b5de8c295d15599489acede221e2/d92e103aae404fe3e4944d0eb4bb55fd1483b5de8c295d15599489acede221e2-json.log",
        "Name": "/unruffled_curie",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "ConsoleSize": [
                28,
                201
            ],
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": [],
            "BlkioDeviceWriteBps": [],
            "BlkioDeviceReadIOps": [],
            "BlkioDeviceWriteIOps": [],
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/4933d9dc5e21b63cf7216cabe44d41eb9c8a1a924527f3e3e02332fc62987023-init/diff:/var/lib/docker/overlay2/50fbc6f52a459bf5fa29df4d85fd3e50a59d1aaa101fb43134df9438dcc37fcd/diff:/var/lib/docker/overlay2/6540b047fa36aa6293df76649dd30e54cc05becec17471951e8d8c23de789a80/diff:/var/lib/docker/overlay2/578b8f6e57cd1ed1b68a3de2d1986f5ef64fb917c5ddf3b759a5790ea9a61439/diff",
                "MergedDir": "/var/lib/docker/overlay2/4933d9dc5e21b63cf7216cabe44d41eb9c8a1a924527f3e3e02332fc62987023/merged",
                "UpperDir": "/var/lib/docker/overlay2/4933d9dc5e21b63cf7216cabe44d41eb9c8a1a924527f3e3e02332fc62987023/diff",
                "WorkDir": "/var/lib/docker/overlay2/4933d9dc5e21b63cf7216cabe44d41eb9c8a1a924527f3e3e02332fc62987023/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "d92e103aae40",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.16.0",
                "NJS_VERSION=0.3.1",
                "PKG_RELEASE=1~stretch"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "Image": "ae893c58d83f",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers "
            },
            "StopSignal": "SIGTERM"
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "bad2c90004b767ec71885ff9128aaecb16b98dbd2d6aa4eaa259254ea0f0856f",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/bad2c90004b7",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "c4bab2cdcb8e5750eabd7b6347ca6105884b7a016692d85571fc72932613b881",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "743d1d364d1d930cc86d311ec72231e9452077128bfdad7af65154d9f04002a3",
                    "EndpointID": "c4bab2cdcb8e5750eabd7b6347ca6105884b7a016692d85571fc72932613b881",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
] 

注意 : 
 "NetworkID": "743d1d364d1d930cc86d311ec72231e9452077128bfdad7af65154d9f04002a3" 可以看到默认走的是 743d1d364d1d   bridge            bridge    local 的网络,如果想换一个我们自定义的网络,怎么办呢?

[root@node1 nginx]# docker network connect journey_network d92e103aae40
[root@node1 nginx]# docker inspect d92e103aae40
[
    {
        "Id": "d92e103aae404fe3e4944d0eb4bb55fd1483b5de8c295d15599489acede221e2",
        "Created": "2023-06-04T01:41:25.482832723Z",
        "Path": "nginx",
        "Args": [
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 2515,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2023-06-04T01:41:25.735005762Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:ae893c58d83fe2bd391fbec97f5576c9a34fea55b4ee9daf15feb9620b14b226",
        "ResolvConfPath": "/var/lib/docker/containers/d92e103aae404fe3e4944d0eb4bb55fd1483b5de8c295d15599489acede221e2/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/d92e103aae404fe3e4944d0eb4bb55fd1483b5de8c295d15599489acede221e2/hostname",
        "HostsPath": "/var/lib/docker/containers/d92e103aae404fe3e4944d0eb4bb55fd1483b5de8c295d15599489acede221e2/hosts",
        "LogPath": "/var/lib/docker/containers/d92e103aae404fe3e4944d0eb4bb55fd1483b5de8c295d15599489acede221e2/d92e103aae404fe3e4944d0eb4bb55fd1483b5de8c295d15599489acede221e2-json.log",
        "Name": "/unruffled_curie",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "ConsoleSize": [
                28,
                201
            ],
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": [],
            "BlkioDeviceWriteBps": [],
            "BlkioDeviceReadIOps": [],
            "BlkioDeviceWriteIOps": [],
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/4933d9dc5e21b63cf7216cabe44d41eb9c8a1a924527f3e3e02332fc62987023-init/diff:/var/lib/docker/overlay2/50fbc6f52a459bf5fa29df4d85fd3e50a59d1aaa101fb43134df9438dcc37fcd/diff:/var/lib/docker/overlay2/6540b047fa36aa6293df76649dd30e54cc05becec17471951e8d8c23de789a80/diff:/var/lib/docker/overlay2/578b8f6e57cd1ed1b68a3de2d1986f5ef64fb917c5ddf3b759a5790ea9a61439/diff",
                "MergedDir": "/var/lib/docker/overlay2/4933d9dc5e21b63cf7216cabe44d41eb9c8a1a924527f3e3e02332fc62987023/merged",
                "UpperDir": "/var/lib/docker/overlay2/4933d9dc5e21b63cf7216cabe44d41eb9c8a1a924527f3e3e02332fc62987023/diff",
                "WorkDir": "/var/lib/docker/overlay2/4933d9dc5e21b63cf7216cabe44d41eb9c8a1a924527f3e3e02332fc62987023/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "d92e103aae40",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.16.0",
                "NJS_VERSION=0.3.1",
                "PKG_RELEASE=1~stretch"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "Image": "ae893c58d83f",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers "
            },
            "StopSignal": "SIGTERM"
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "bad2c90004b767ec71885ff9128aaecb16b98dbd2d6aa4eaa259254ea0f0856f",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/bad2c90004b7",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "c4bab2cdcb8e5750eabd7b6347ca6105884b7a016692d85571fc72932613b881",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "743d1d364d1d930cc86d311ec72231e9452077128bfdad7af65154d9f04002a3",
                    "EndpointID": "c4bab2cdcb8e5750eabd7b6347ca6105884b7a016692d85571fc72932613b881",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                },
                "journey_network": {
                    "IPAMConfig": {},
                    "Links": null,
                    "Aliases": [
                        "d92e103aae40"
                    ],
                    "NetworkID": "f905066b895cd55108dd0b8a5117408e2ac7f78fbe2038761ca075f02f144a03",
                    "EndpointID": "82a6740dbaed45e4e36438b605c9a059580f35760c79fe353fa4b689666458a7",
                    "Gateway": "172.20.0.1",
                    "IPAddress": "172.20.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:14:00:02",
                    "DriverOpts": {}
                }
            }
        }
    }
]

注意 : 
 "NetworkID": "f905066b895cd55108dd0b8a5117408e2ac7f78fbe2038761ca075f02f144a03" 是不是就是我们自定义的 f905066b895c   journey_network   bridge    local 的网络

7、容器的网络删除

[root@node1 nginx]# docker network disconnect journey_network d92e103aae40

注意 : 发现删除自定义的网络,就是默认的网络了,就是之前默认设置的网络(743d1d364d1d bridge bridge local)

5、存储卷管理

1、docker持久化测试

[root@node1 ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
bjbfd/nginx   v4        a80567189207   8 days ago    141MB
nginx         1.16.0    ae893c58d83f   3 years ago   109MB
[root@node1 ~]# docker run -itd nginx:1.16.0
d24cfc2451e1f2d789ec5813a15d43d2bca4def4ed21f26fea1379ed2be4d0c4
[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS         PORTS     NAMES
d24cfc2451e1   nginx:1.16.0   "nginx -g 'daemon of…"   3 seconds ago   Up 2 seconds   80/tcp    hardcore_dewdney
[root@node1 ~]# docker exec -it d24cfc2451e1 /bin/bash
root@d24cfc2451e1:/# ls
bin  boot  dev    etc  home  lib    lib64  media  mnt  opt    proc  root  run  sbin  srv  sys  tmp  usr  var
root@d24cfc2451e1:/# touch my.txt
root@d24cfc2451e1:/# cat > my.txt
aaa
^C
root@d24cfc2451e1:/# cat my.txt
aaa
root@d24cfc2451e1:/# ls
bin  boot  dev    etc  home  lib    lib64  media  mnt  my.txt  opt    proc  root  run  sbin  srv  sys  tmp  usr  var
root@d24cfc2451e1:/# exit
exit
[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED          STATUS          PORTS     NAMES
d24cfc2451e1   nginx:1.16.0   "nginx -g 'daemon of…"   45 seconds ago   Up 44 seconds   80/tcp    hardcore_dewdney
[root@node1 ~]# docker stop d24cfc2451e1
d24cfc2451e1
[root@node1 ~]# docker start d24cfc2451e1
d24cfc2451e1
[root@node1 ~]# docker exec -it d24cfc2451e1 /bin/bash
root@d24cfc2451e1:/# cat my.txt
aaa
root@d24cfc2451e1:/#

测试发现,docker的容器是支持文件持久化的。停止再启动容器,文件是不会丢失的

2、删除再通过镜像启动容器,文件是否丢失测试

发现,如果我们删除容器,再通过镜像启动容器,原来容器里面的my.txt文件丢失

2、docker volume

1、docker数据持久化

  • bind mount : 挂载任意目录或文件
  • volumns : 固定路径的目录,docker管理
  • tmpfs mounts : 存放宿主机内存中
    Docker进阶_第3张图片

2、docker容器共享

Docker进阶_第4张图片

3、docker数据持久化应用

3.1、bind mount

[root@node1 ~]# docker run -itd --name nginx_node1 -v /data/nginx/:/usr/share/nginx/html nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
a2abf6c4d29d: Already exists
a9edb18cadd1: Already exists
589b7251471a: Already exists
186b1aaa4aa6: Already exists
b4df32aa5a72: Already exists
a0bcbecc962e: Already exists
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
0a62e4026a157ae1a267991253f9f54371a0e3d706a2c36f3a9698949ae81df4
[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                   CREATED          STATUS          PORTS     NAMES
0a62e4026a15   nginx     "/docker-entrypoint.…"   36 seconds ago   Up 35 seconds   80/tcp    nginx_node1
[root@node1 ~]# docker exec -it 0a62e4026a15 /bin/bash
root@0a62e4026a15:/# curl 127.0.0.1

403 Forbidden

403 Forbidden


nginx/1.21.5
[root@node1 ~]# cd /data/nginx/ [root@node1 nginx]# ls [root@node1 nginx]# echo "docker volume" > index.html root@0a62e4026a15:/# curl 127.0.0.1 docker volume root@0a62e4026a15:/# root@0a62e4026a15:/# root@0a62e4026a15:/# cd /usr/share/nginx/html/ root@0a62e4026a15:/usr/share/nginx/html# ls index.html root@0a62e4026a15:/usr/share/nginx/html# cat index.html docker volume root@0a62e4026a15:/usr/share/nginx/html#

为了保证Dockerfile可移植,构建镜像不能使用bind mount,很简单,就是外部管理,而不是docker自己管理

3.2、tmpfs mounts

docker run -itd --name tmptest --tmpfs /app nginx:latest
或者
docker run -itd --name tmptest --mount type=tmpfs,destination=/app nginx:latest

[root@node1 ~]# docker run -itd --name tmptest --tmpfs /app nginx:latest
12ae398d6eb6392d33dfc08d56137f29c2f28b994605123adfae311dfb25231f
[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS        PORTS     NAMES
12ae398d6eb6   nginx:latest   "/docker-entrypoint.…"   2 seconds ago   Up 1 second   80/tcp    tmptest
[root@node1 ~]# docker exec -it 12ae398d6eb6 /bin/bash
root@12ae398d6eb6:/# ls
app  bin  boot    dev  docker-entrypoint.d  docker-entrypoint.sh    etc  home  lib    lib64  media  mnt  opt    proc  root  run  sbin  srv  sys  tmp  usr  var
root@12ae398d6eb6:/# df -h
Filesystem      Size  Used Avail Use% Mounted on
overlay          40G  3.5G   34G  10% /
tmpfs            64M     0   64M   0% /dev
tmpfs           1.8G     0  1.8G   0% /sys/fs/cgroup
shm              64M     0   64M   0% /dev/shm
tmpfs           1.8G     0  1.8G   0% /app
/dev/vda1        40G  3.5G   34G  10% /etc/hosts
tmpfs           1.8G     0  1.8G   0% /proc/acpi
tmpfs           1.8G     0  1.8G   0% /proc/scsi
tmpfs           1.8G     0  1.8G   0% /sys/firmware
root@12ae398d6eb6:/# cd app/
root@12ae398d6eb6:/app# ls
root@12ae398d6eb6:/app# df -h
Filesystem      Size  Used Avail Use% Mounted on
overlay          40G  3.5G   34G  10% /
tmpfs            64M     0   64M   0% /dev
tmpfs           1.8G     0  1.8G   0% /sys/fs/cgroup
shm              64M     0   64M   0% /dev/shm
tmpfs           1.8G     0  1.8G   0% /app
/dev/vda1        40G  3.5G   34G  10% /etc/hosts
tmpfs           1.8G     0  1.8G   0% /proc/acpi
tmpfs           1.8G     0  1.8G   0% /proc/scsi
tmpfs           1.8G     0  1.8G   0% /sys/firmware
root@12ae398d6eb6:/app# dd if=/dev/zero of=./test.txt count=2048 bs=1M
dd: error writing './test.txt': No space left on device
1824+0 records in
1823+0 records out
1911582720 bytes (1.9 GB, 1.8 GiB) copied, 0.904957 s, 2.1 GB/s

发现使用tmpfs 写的速率能达到 2.1 GB/s

[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED          STATUS          PORTS     NAMES
12ae398d6eb6   nginx:latest   "/docker-entrypoint.…"   57 seconds ago   Up 56 seconds   80/tcp    tmptest
[root@node1 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        latest    605c77e624dd   17 months ago   141MB
[root@node1 ~]# docker run -itd nginx
0ca7f5aa086815a9891f23e8ecca47783f1e2dbcd0d4e5c99e170f577c609cdc
[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED              STATUS              PORTS     NAMES
0ca7f5aa0868   nginx          "/docker-entrypoint.…"   4 seconds ago        Up 2 seconds        80/tcp    gracious_rosalind
12ae398d6eb6   nginx:latest   "/docker-entrypoint.…"   About a minute ago   Up About a minute   80/tcp    tmptest
[root@node1 ~]# docker exec -it 0ca7f5aa0868
"docker exec" requires at least 2 arguments.
See 'docker exec --help'.

Usage:  docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Execute a command in a running container
[root@node1 ~]# docker exec -it 0ca7f5aa0868 /bin/bash
root@0ca7f5aa0868:/# dd if=/dev/zero of=./test.txt count=2048 bs=1M
^C690+0 records in
690+0 records out
723517440 bytes (724 MB, 690 MiB) copied, 2.0691 s, 350 MB/s

发现普通的磁盘写的速率能达到 350 MB/s

tmpfs会映射到宿主机的内存中,所以块,但是同样不稳定

3.3、volumns

1、走默认docker volume的创建
[root@node1 ~]# docker run -itd --name nginx_node -v /usr/share/nginx/html nginx
4191c61a053fcacc5e661e3b663227814503eb6e571275659da724186e4a44a2
[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                   CREATED         STATUS        PORTS     NAMES
4191c61a053f   nginx     "/docker-entrypoint.…"   2 seconds ago   Up 1 second   80/tcp    nginx_node
[root@node1 ~]# docker volume ls
DRIVER    VOLUME NAME
local     e2117fbeda605fed2a98e06185ecaeb06104c63a84616d8267fc94a3a324b1e8
local     wordpress_db_data
local     wordpress_wp_data

发现自动创建了一个volume e2117fbeda605fed2a98e06185ecaeb06104c63a84616d8267fc94a3a324b1e8
[root@node1 ~]# docker volume inspect e2117fbeda605fed2a98e06185ecaeb06104c63a84616d8267fc94a3a324b1e8
[
    {
        "CreatedAt": "2023-06-06T17:28:36+08:00",
        "Driver": "local",
        "Labels": {
            "com.docker.volume.anonymous": ""
        },
        "Mountpoint": "/var/lib/docker/volumes/e2117fbeda605fed2a98e06185ecaeb06104c63a84616d8267fc94a3a324b1e8/_data",
        "Name": "e2117fbeda605fed2a98e06185ecaeb06104c63a84616d8267fc94a3a324b1e8",
        "Options": null,
        "Scope": "local"
    }

修改docker管理的volume文件
[root@node1 _data]# vim index.html



Welcome to nginx!



Welcome to nginx!,journey

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 nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

[root@node1 _data]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4191c61a053f nginx "/docker-entrypoint.…" 38 minutes ago Up 38 minutes 80/tcp nginx_node [root@node1 _data]# docker exec -it 4191c61a053f /bin/bash root@4191c61a053f:/# curl 127.0.0.1 Welcome to nginx!

Welcome to nginx!,journey

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 nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

2、自己创建一个volume [root@node1 _data]# docker volume ls DRIVER VOLUME NAME local e2117fbeda605fed2a98e06185ecaeb06104c63a84616d8267fc94a3a324b1e8 local wordpress_db_data local wordpress_wp_data [root@node1 _data]# docker volume create journey_volume journey_volume [root@node1 _data]# docker volume ls DRIVER VOLUME NAME local e2117fbeda605fed2a98e06185ecaeb06104c63a84616d8267fc94a3a324b1e8 local journey_volume local wordpress_db_data local wordpress_wp_data [root@node1 _data]# docker volume inspect journey_volume [ { "CreatedAt": "2023-06-06T18:14:29+08:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/journey_volume/_data", "Name": "journey_volume", "Options": null, "Scope": "local" } ] [root@node1 _data]# cd /var/lib/docker/volumes/journey_volume/_data [root@node1 _data]# ls [root@node1 _data]# cat > index.html hello journey ^C [root@node1 _data]# cat index.html hello journey [root@node1 _data]# docker run -itd --name nginx_journey_volume -p 8080:80 -v journey_volume:/usr/share/nginx/html nginx 49a95de0de50bd4ab00c6005a9d27053e02aef51487fe2bb7dcf34f9796488c5 [root@node1 _data]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 49a95de0de50 nginx "/docker-entrypoint.…" 8 seconds ago Up 7 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp nginx_journey_volume [root@node1 _data]# docker exec -it 49a95de0de50 /bin/bash root@49a95de0de50:/# exit [root@node1 _data]# curl 127.0.0.1:8080 hello journey

建议docker的数据持久化采用volume方式

4、存储卷管理命令

1、新建volume
[root@node1 _data]# docker volume create journey_volume
journey_volume

2、查看volume列表
[root@node1 _data]# docker volume ls
DRIVER    VOLUME NAME
local     journey_volume

3、查看volume详细信息
[root@node1 _data]# docker volume inspect journey_volume
[
    {
        "CreatedAt": "2023-06-06T18:22:15+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/journey_volume/_data",
        "Name": "journey_volume",
        "Options": null,
        "Scope": "local"
    }
]

4、删除volume
[root@node1 _data]# docker volume rm journey_volume
journey_volume

5、容器使用volume
[root@node1 _data]# docker run -itd --name nginx_journey_volume -p 8080:80 -v journey_volume:/usr/share/nginx/html nginx

删除所有正在运行的容器
docker rm -f $(docker ps -a -q)

你可能感兴趣的:(docker)