• Docker基础命令操作


    Docker镜像生命周期

    在这里插入图片描述

    1、镜像操作

    1.1 镜像搜索(默认搜索官方仓库)【镜像名需指定】

    [root@johnnyg03 ~]# docker search mysql
    NAME                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
    mysql                           MySQL is a widely used, open-source relation…   12996     [OK]       
    mariadb                         MariaDB Server is a high performing open sou…   4981      [OK]       
    phpmyadmin                      phpMyAdmin - A web interface for MySQL and M596       [OK]       
    percona                         Percona Server is a fork of the MySQL relati…   583       [OK]       
    bitnami/mysql                   Bitnami MySQL Docker Image                      72                   [OK]
    linuxserver/mysql-workbench                                                     41                   
    linuxserver/mysql               A Mysql container, brought to you by LinuxSe…   37                   
    ubuntu/mysql                    MySQL open source fast, stable, multi-thread…   36                   
    circleci/mysql                  MySQL is a widely used, open-source relation…   26                   
    google/mysql                    MySQL server for Google Compute Engine          21                   [OK]
    rapidfort/mysql                 RapidFort optimized, hardened image for MySQL   13                   
    bitnami/mysqld-exporter                                                         3                    
    ibmcom/mysql-s390x              Docker image for mysql-s390x                    2                    
    newrelic/mysql-plugin           New Relic Plugin for monitoring MySQL databa…   1                    [OK]
    vitess/mysqlctld                vitess/mysqlctld                                1                    [OK]
    hashicorp/mysql-portworx-demo                                                   0                    
    docksal/mysql                   MySQL service images for Docksal - https://d…   0                    
    mirantis/mysql                                                                  0                    
    cimg/mysql                                                                      0                    
    drud/mysql                                                                      0                    
    silintl/mysql-backup-restore    Simple docker image to perform mysql backups…   0                    [OK]
    corpusops/mysql                 https://github.com/corpusops/docker-images/     0                    
    drud/mysql-local-57             ddev mysql local container                      0                    
    drud/mysql-docker-local-57      This repo has been deprecated, new tags are …   0                    
    drud/mysql-docker-local         docker containers for local womysql rk          0                    [OK]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    对应参数
    在这里插入图片描述

    1.2 镜像拉取

    //镜像名末尾什么都不加,默认拉取最新版本
    [root@johnnyg03 ~]# docker pull mysql
    Using default tag: latest
    latest: Pulling from library/mysql
    72a69066d2fe: Pull complete 
    93619dbc5b36: Pull complete 
    99da31dd6142: Pull complete 
    626033c43d70: Pull complete 
    37d5d7efb64e: Pull complete 
    ac563158d721: Pull complete 
    d2ba16033dad: Pull complete 
    688ba7d5c01a: Pull complete 
    00e060b6d11d: Pull complete 
    1c04857f594f: Pull complete 
    4d7cfa90e6ea: Pull complete 
    e0431212d27d: Pull complete 
    Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
    Status: Downloaded newer image for mysql:latest
    docker.io/library/mysql:latest
    //可以看到末尾写有latest表示最新版本
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    指定版本

    //docker pull mysql:版本
    [root@johnnyg03 ~]# docker pull mysql:8.0.30
    8.0.30: Pulling from library/mysql
    32c1bf40aba1: Pull complete 
    3ac22f3a638d: Pull complete 
    b1e7273ed05e: Pull complete 
    20be45a0c6ab: Pull complete 
    410a229693ff: Pull complete 
    1ce71e3a9b88: Pull complete 
    c93c823af05b: Pull complete 
    c6752c4d09c7: Pull complete 
    d7f2cfe3efcb: Pull complete 
    916f32cb0394: Pull complete 
    0d62a5f9a14f: Pull complete 
    Digest: sha256:ce2ae3bd3e9f001435c4671cf073d1d5ae55d138b16927268474fc54ba09ed79
    Status: Downloaded newer image for mysql:8.0.30
    docker.io/library/mysql:8.0.30
    //可以看到末尾写有8.0.30表示版本号
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    1.3 镜像查看

    方式一:

    [root@johnnyg03 ~]# docker image ls
    REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
    mysql        8.0.30    7b94cda7ffc7   6 days ago     446MB
    mysql        latest    3218b38490ce   7 months ago   516MB
    
    • 1
    • 2
    • 3
    • 4

    方式二:

    [root@johnnyg03 ~]# docker images
    REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
    mysql        8.0.30    7b94cda7ffc7   6 days ago     446MB
    mysql        latest    3218b38490ce   7 months ago   516MB
    
    • 1
    • 2
    • 3
    • 4

    1.4 镜像删除

    方法一:

    [root@johnnyg03 ~]# docker image rm mysql
    Untagged: mysql:latest
    Untagged: mysql@sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
    Deleted: sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b
    Deleted: sha256:aa81ca46575069829fe1b3c654d9e8feb43b4373932159fe2cad1ac13524a2f5
    Deleted: sha256:0558823b9fbe967ea6d7174999be3cc9250b3423036370dc1a6888168cbd224d
    Deleted: sha256:a46013db1d31231a0e1bac7eeda5ad4786dea0b1773927b45f92ea352a6d7ff9
    Deleted: sha256:af161a47bb22852e9e3caf39f1dcd590b64bb8fae54315f9c2e7dc35b025e4e3
    Deleted: sha256:feff1495e6982a7e91edc59b96ea74fd80e03674d92c7ec8a502b417268822ff
    Deleted: sha256:8805862fcb6ef9deb32d4218e9e6377f35fb351a8be7abafdf1da358b2b287ba
    Deleted: sha256:872d2f24c4c64a6795e86958fde075a273c35c82815f0a5025cce41edfef50c7
    Deleted: sha256:6fdb3143b79e1be7181d32748dd9d4a845056dfe16ee4c827410e0edef5ad3da
    Deleted: sha256:b0527c827c82a8f8f37f706fcb86c420819bb7d707a8de7b664b9ca491c96838
    Deleted: sha256:75147f61f29796d6528486d8b1f9fb5d122709ea35620f8ffcea0e0ad2ab0cd0
    Deleted: sha256:2938c71ddf01643685879bf182b626f0a53b1356138ef73c40496182e84548aa
    Deleted: sha256:ad6b69b549193f81b039a1d478bc896f6e460c77c1849a4374ab95f9a3d2cea2
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    方法二:

    [root@johnnyg03 ~]# docker rmi mysql:8.0.30
    Untagged: mysql:8.0.30
    Untagged: mysql@sha256:ce2ae3bd3e9f001435c4671cf073d1d5ae55d138b16927268474fc54ba09ed79
    Deleted: sha256:7b94cda7ffc7c59b01668e63f48e0f4ee3d16b427cc0b846193b65db671e9fa2
    Deleted: sha256:5941a477484b80a594761cb828f993b574a1ac2cd741300503209752f7790466
    Deleted: sha256:dd778772db51bcd70db26f7b9ae996220eb337721328090a409d1ab7919fb887
    Deleted: sha256:e94a7bdbca441b8d897023017cf70bdc1ba7bf8233f8da4800bb6e4ed49f14f6
    Deleted: sha256:d7d110459a2ceb8a080a2bd7264c66d9cc8699242aa320d1b9cdcde872c050a3
    Deleted: sha256:6e4a1a78cee71e12fce86c8610f0c8802144714b4673b7cc6878c5a23b58d134
    Deleted: sha256:38dc91f8c75eb2f05ad01169fdbfab02602dc250f2731322692eac67c9b41f71
    Deleted: sha256:5aa021ed35bcc6afdb63ce3a80bbe9f8995d168ed138cd83cc33dc7ec9ea6ee1
    Deleted: sha256:10a7f83cd06f6add8176d92056feea4efd45165367e5ba6fd5077c79468b6fe4
    Deleted: sha256:775289ef4a1150bcf8ccf7947134a092ef6d23fc78ab89f8f9feff7226768f40
    Deleted: sha256:a4470052a15f553513f12f02c1e52c811280ef56463a6774dcbd5ffd47a07045
    Deleted: sha256:cf4db719a36940c70d724cd9eaa2fccaf1f0174ba863ebb40f428eaf13da76cc
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    查看镜像情况

    [root@johnnyg03 ~]# docker images 
    REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
    //可以看到两个MySQL镜像均不存在
    
    • 1
    • 2
    • 3

    1.5 镜像导出

    方法一:

    [root@johnnyg03 ~]# docker save busybox:latest > docker-busybox-test1.tar.gz
    
    • 1

    方法二:

    [root@johnnyg03 ~]# docker save busybox:latest -o docker-busybox-test2.tar.gz
    
    • 1

    参数解释

    [root@johnnyg03 ~]# docker save --help
    
    Usage:  docker save [OPTIONS] IMAGE [IMAGE...]
    
    Save one or more images to a tar archive (streamed to STDOUT by default)
    
    Options:
      -o, --output string   Write to a file, instead of STDOUT
      -o 输出字符串
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    查看导出情况

    [root@johnnyg03 ~]# ll
    total 2872
    -rw-r--r-- 1 root root 1468416 Aug 10 14:12 docker-busybox-test1.tar.gz
    -rw------- 1 root root 1468416 Aug 10 14:12 docker-busybox-test2.tar.gz
    //可以看到均已存在
    
    • 1
    • 2
    • 3
    • 4
    • 5

    1.6 镜像导入

    预先删除busybox镜像

    [root@johnnyg03 ~]# docker rmi busybox:latest 
    Untagged: busybox:latest
    Untagged: busybox@sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678
    Deleted: sha256:beae173ccac6ad749f76713cf4440fe3d21d1043fe616dfbe30775815d1d0f6a
    Deleted: sha256:01fd6df81c8ec7dd24bbbd72342671f41813f992999a3471b9d9cbc44ad88374
    [root@johnnyg03 ~]# docker images
    REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    本地导入

    [root@johnnyg03 ~]# docker load -i docker-busybox-test1.tar.gz 
    01fd6df81c8e: Loading layer  1.459MB/1.459MB
    Loaded image: busybox:latest
    [root@johnnyg03 ~]# docker images
    REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
    busybox      latest    beae173ccac6   7 months ago   1.24MB
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    异地导入

    //传输过去johnnyg02主机
    [root@johnnyg03 ~]# scp docker-busybox-test2.tar.gz 10.0.2.11:~
    //查看是否存在
    [root@johnnyg02 ~]# ll
    total 599784
    -rw------- 1 root root   1468416 Aug 10 14:22 docker-busybox-test2.tar.gz
    
    [root@johnnyg02 ~]# docker load -i docker-busybox-test2.tar.gz 
    01fd6df81c8e: Loading layer  1.459MB/1.459MB
    Loaded image: busybox:latest
    [root@johnnyg02 ~]# docker images
    REPOSITORY                     TAG          IMAGE ID       CREATED         SIZE
    busybox                        latest       beae173ccac6   7 months ago    1.24MB
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    参数解释

    [root@johnnyg03 ~]# docker load --help
    
    Usage:  docker load [OPTIONS]
    
    Load an image from a tar archive or STDIN
    
    Options:
      -i, --input string   Read from tar archive file, instead of STDIN
      从tar归档文件读取镜像,而不是标准输入流
      -q, --quiet          Suppress the load output
      禁止读入输出
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    1.7 查看镜像详细信息

    方法一:

    [root@johnnyg03 ~]# docker image inspect busybox:latest 
    [
        {
            "Id": "sha256:beae173ccac6ad749f76713cf4440fe3d21d1043fe616dfbe30775815d1d0f6a",
            "RepoTags": [
                "busybox:latest"
            ],
            "RepoDigests": [],
            "Parent": "",
            "Comment": "",
            "Created": "2021-12-30T19:19:41.006954958Z",
            "Container": "a0007fa726185ffbcb68e90f8edabedd79a08949f32f4f0bcc6e5fed713a72c8",
            "ContainerConfig": {
                "Hostname": "a0007fa72618",
                "Domainname": "",
                "User": "",
                "AttachStdin": false,
                "AttachStdout": false,
                "AttachStderr": false,
                "Tty": false,
                "OpenStdin": false,
                "StdinOnce": false,
                "Env": [
                    "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
                ],
                "Cmd": [
                    "/bin/sh",
                    "-c",
                    "#(nop) ",
                    "CMD [\"sh\"]"
                ],
                "Image": "sha256:da658412c37aa24e561eb7e16c61bc82a9711340d8fb5cf1a8f39d8e96d7f723",
                "Volumes": null,
                "WorkingDir": "",
                "Entrypoint": null,
                "OnBuild": null,
                "Labels": {}
            },
            "DockerVersion": "20.10.7",
            "Author": "",
            "Config": {
                "Hostname": "",
                "Domainname": "",
                "User": "",
                "AttachStdin": false,
                "AttachStdout": false,
                "AttachStderr": false,
                "Tty": false,
                "OpenStdin": false,
                "StdinOnce": false,
                "Env": [
                    "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
                ],
                "Cmd": [
                    "sh"
                ],
                "Image": "sha256:da658412c37aa24e561eb7e16c61bc82a9711340d8fb5cf1a8f39d8e96d7f723",
                "Volumes": null,
                "WorkingDir": "",
                "Entrypoint": null,
                "OnBuild": null,
                "Labels": null
            },
            "Architecture": "amd64",
            "Os": "linux",
            "Size": 1239820,
            "VirtualSize": 1239820,
            "GraphDriver": {
                "Data": {
                    "MergedDir": "/var/lib/docker/overlay2/b0d7b835ecde86b17ed707c21ca09d7fa1c4969d1fcfa30174cf779016b5ee14/merged",
                    "UpperDir": "/var/lib/docker/overlay2/b0d7b835ecde86b17ed707c21ca09d7fa1c4969d1fcfa30174cf779016b5ee14/diff",
                    "WorkDir": "/var/lib/docker/overlay2/b0d7b835ecde86b17ed707c21ca09d7fa1c4969d1fcfa30174cf779016b5ee14/work"
                },
                "Name": "overlay2"
            },
            "RootFS": {
                "Type": "layers",
                "Layers": [
                    "sha256:01fd6df81c8ec7dd24bbbd72342671f41813f992999a3471b9d9cbc44ad88374"
                ]
            },
            "Metadata": {
                "LastTagTime": "0001-01-01T00:00:00Z"
            }
        }
    ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86

    方法二:

    [root@johnnyg03 ~]# docker inspect busybox:latest
    
    • 1

    2、容器操作

    2.1 容器创建(不推荐)

    [root@johnnyg03 ~]# docker create 镜像名
    
    • 1

    2.2 容器启动

    [root@johnnyg03 ~]# docker start 容器名
    
    • 1

    2.3 容器停止

    方法一:

    [root@johnnyg03 ~]# docker stop 容器名
    
    • 1

    方法二:

    [root@johnnyg03 ~]# docker kill 容器名
    
    • 1

    2.4 容器创建启动一体式(推荐)

    格式

    [root@johnnyg03 ~]# docker run --help
    
    Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
    [root@johnnyg03 ~]# docker run 镜像名
    [root@johnnyg03 ~]# docker run 参数 镜像名
    
    • 1
    • 2
    • 3
    • 4
    • 5

    常用参数解释

    Options:
    	-t:打开一个终端,像使用交换机一样使用容器
    	-i:交互式访问
    	--name:容器名字,不指定会随机设置容器名
    	--network:指定网络类型(后面会单独介绍)
    	--rm:容器一停,自动删除
    	-d:剥离与当前终端的关系;否则会一直占据着终端
    	-p:端口映射,将容器内服务的端口映射在宿主机的指定端口
    		-p格式:
    			-p
    			-p :
    			-p ::
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    示例

    //拉取nginx镜像作为测试
    [root@johnnyg03 ~]# docker pull nginx
    Using default tag: latest
    latest: Pulling from library/nginx
    a2abf6c4d29d: Pull complete 
    a9edb18cadd1: Pull complete 
    589b7251471a: Pull complete 
    186b1aaa4aa6: Pull complete 
    b4df32aa5a72: Pull complete 
    a0bcbecc962e: Pull complete 
    Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
    Status: Downloaded newer image for nginx:latest
    docker.io/library/nginx:latest
    
    [root@johnnyg03 ~]# docker images
    REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
    busybox      latest    beae173ccac6   7 months ago   1.24MB
    nginx        latest    605c77e624dd   7 months ago   141MB
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    测试一(配置后直接进入容器内)

    [root@johnnyg03 ~]# docker run -it --name web1 nginx:latest /bin/bash
    root@bab7ddfa7f4e:/# ls
    bin   dev                  docker-entrypoint.sh  home  lib64  mnt  proc  run   srv  tmp  var
    boot  docker-entrypoint.d  etc                   lib   media  opt  root  sbin  sys  usr
    root@bab7ddfa7f4e:/# pwd
    /
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    复制新会话并查看容器状态

    [root@johnnyg03 ~]# docker ps -a
    CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS     NAMES
    bab7ddfa7f4e   nginx:latest   "/docker-entrypoint.…"   59 seconds ago   Up 58 seconds   80/tcp    web1
    //可以看到当前容器为Up状态开启状态
    
    • 1
    • 2
    • 3
    • 4

    退出容器

    root@bab7ddfa7f4e:/# exit
    exit
    [root@johnnyg03 ~]# docker ps -a
    CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS                     PORTS     NAMES
    bab7ddfa7f4e   nginx:latest   "/docker-entrypoint.…"   About a minute ago   Exited (0) 3 seconds ago             web1
    //可以看到退出后当前容器为Exited关闭状态
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    测试二(退出容器自动删除容器)

    [root@johnnyg03 ~]# docker run -it --rm --name web2 nginx:latest /bin/bash
    root@cac6d1bac43c:/# ls
    bin   dev                  docker-entrypoint.sh  home  lib64  mnt  proc  run   srv  tmp  var
    boot  docker-entrypoint.d  etc                   lib   media  opt  root  sbin  sys  usr
    root@cac6d1bac43c:/# pwd
    /
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    复制新会话并查看容器状态

    [root@johnnyg03 ~]# docker ps -a
    CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS                          PORTS     NAMES
    cac6d1bac43c   nginx:latest   "/docker-entrypoint.…"   7 seconds ago   Up 6 seconds                    80/tcp    web2
    bab7ddfa7f4e   nginx:latest   "/docker-entrypoint.…"   2 minutes ago   Exited (0) About a minute ago             web1
    //可以看到两个容器均为Up状态开启状态
    
    • 1
    • 2
    • 3
    • 4
    • 5

    退出容器

    root@cac6d1bac43c:/# exit
    exit
    [root@johnnyg03 ~]# docker ps -a
    CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS                          PORTS     NAMES
    bab7ddfa7f4e   nginx:latest   "/docker-entrypoint.…"   2 minutes ago   Exited (0) About a minute ago             web1
    //可以看到退出后web2自动删除已不存在
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    测试三(容器剥离终端挂在后台)

    【注:需要使用可以通过命令docker start 容器名进行容器开启,后面再通过命令docker exec 参数 容器名访问进入终端】

    [root@johnnyg03 ~]# docker run -d --name web3 nginx:latest 
    6f4f6ae8ea6bbfd4796bc3cfc4711b9edff8f9b85469fbcf27f55227f081a247
    
    [root@johnnyg03 ~]# docker ps -a
    CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS                     PORTS     NAMES
    6f4f6ae8ea6b   nginx:latest   "/docker-entrypoint.…"   5 seconds ago   Up 4 seconds               80/tcp    web3
    bab7ddfa7f4e   nginx:latest   "/docker-entrypoint.…"   3 minutes ago   Exited (0) 2 minutes ago             web1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    测试四(端口映射)

    [root@johnnyg03 ~]# docker run -it --name web4 -p 80:80 nginx:latest /bin/bash
    root@9513c393471f:/# ls
    bin   dev                  docker-entrypoint.sh  home  lib64  mnt  proc  run   srv  tmp  var
    boot  docker-entrypoint.d  etc                   lib   media  opt  root  sbin  sys  usr
    root@9513c393471f:/# pwd
    /
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    复制新会话并查看容器状态

    [root@johnnyg03 ~]# docker ps -a
    CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                     PORTS                               NAMES
    9513c393471f   nginx:latest   "/docker-entrypoint.…"   37 seconds ago   Up 36 seconds              0.0.0.0:80->80/tcp, :::80->80/tcp   web4
    6f4f6ae8ea6b   nginx:latest   "/docker-entrypoint.…"   7 minutes ago    Up 7 minutes               80/tcp                              web3
    bab7ddfa7f4e   nginx:latest   "/docker-entrypoint.…"   10 minutes ago   Exited (0) 9 minutes ago                                       web1
    //可以看到web4容器PORTS项中写有端口映射情况
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    查看端口映射

    [root@johnnyg03 ~]# docker port web4
    80/tcp -> 0.0.0.0:80
    80/tcp -> :::80
    
    • 1
    • 2
    • 3

    配置nginx服务并进行访问

    root@9513c393471f:/# echo "hello world" > /usr/share/nginx/html/index.html
    
    //开启nginx服务
    root@9513c393471f:/# /usr/sbin/nginx
    2022/08/10 07:31:22 [notice] 8#8: using the "epoll" event method
    2022/08/10 07:31:22 [notice] 8#8: nginx/1.21.5
    2022/08/10 07:31:22 [notice] 8#8: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
    2022/08/10 07:31:22 [notice] 8#8: OS: Linux 3.10.0-1160.66.1.el7.x86_64
    2022/08/10 07:31:22 [notice] 8#8: getrlimit(RLIMIT_NOFILE): 1048576:1048576
    2022/08/10 07:31:22 [notice] 9#9: start worker processes
    2022/08/10 07:31:22 [notice] 9#9: start worker process 10
    2022/08/10 07:31:22 [notice] 9#9: start worker process 11
    2022/08/10 07:31:22 [notice] 9#9: start worker process 12
    2022/08/10 07:31:22 [notice] 9#9: start worker process 13
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    其它主机访问

    [root@johnnyg02 ~]# curl 1.13.249.218:80
    hello world
    
    • 1
    • 2

    2.5 容器详细信息

    [root@johnnyg03 ~]# docker inspect 容器名
    
    • 1

    2.6 容器删除

    [root@johnnyg03 ~]# docker rm 容器名
    
    • 1

    如果容器为启动状态(-f 强制删除)【不建议】

    [root@johnnyg03 ~]# docker rm -f 容器名
    
    • 1
    [root@johnnyg03 ~]# docker rm web1
    web1
    
    [root@johnnyg03 ~]# docker rm -f web3
    web3
    
    [root@johnnyg03 ~]# docker ps -a
    CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                     PORTS     NAMES
    9513c393471f   nginx:latest   "/docker-entrypoint.…"   28 minutes ago   Exited (0) 5 minutes ago             web4
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2.7 对Up启动状态的容器执行指定命令exec

    格式

    [root@johnnyg03 ~]# docker exec --help
    
    Usage:  docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
    [root@johnnyg03 ~]# docker exec 参数 容器名
    
    • 1
    • 2
    • 3
    • 4

    常用参数解释

    Options:
    	-d:在后台运行命令
    	-e:设置环境变量
    	-i:交互式
    	-t:打开一个终端
    	-u:用户名或UID
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    示例1(进入终端)

    [root@johnnyg03 ~]# docker exec -it web4 /bin/bash
    root@9513c393471f:/# ls
    bin   dev                  docker-entrypoint.sh  home  lib64  mnt  proc  run   srv  tmp  var
    boot  docker-entrypoint.d  etc                   lib   media  opt  root  sbin  sys  usr
    root@9513c393471f:/# pwd
    /
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    示例2(执行命令并回显)

    [root@johnnyg03 ~]# docker exec web4 hostname
    9513c393471f
    [root@johnnyg03 ~]# docker exec web4 pwd
    /
    
    • 1
    • 2
    • 3
    • 4

    其余参数不演示

    2.8 查看容器内部日志

    [root@johnnyg03 ~]# docker logs web4
    
    • 1

    2.9 一图总结对容器的操作命令

    在这里插入图片描述

  • 相关阅读:
    手把手教你搭建SpringCloud项目
    Cron 表达式详解及最新版本使用
    mysql的redo和undo-log
    linux内核中CMA内存分配
    【快乐离散数学】Discrete Structure 课程计划书
    横向控制- Stanley算法
    【云原生】Docker Compose 构建 Jenkins
    python常见爬虫库以及案例
    系统升级数量超微软预期,Win10/11盗版激活被封杀
    EasyX趣味化编程note2,绘制基本图形
  • 原文地址:https://blog.csdn.net/JohnnyG2000/article/details/126263767