在线浏览思维导图

  • 容器命令

    • docker run [OPTIONS] IMAGE [COMMAND] [ARG...]J基本命令

      OPTIONS说明

      • --name=容器新名字为容器指定一个名称

        [root@localhost ~]# docker run -it --name=my_ubuntu ubuntu /bin/bash

        image-20230223125859132

      • -d后台运行容器,并返回容器ID,就是启动守护式容器(后台运行)

      • -i以交互模式运行容器,通常与-t同时使用

      • -t为容器分配一个伪输入终端,通常与-i同时使用

        [root@localhost ~]# docker run -it ubuntu /bin/bash 

        image-20230223125550509

      • -P随机分配端口(大写)

      • -p指定分配端口(小写)

    • docker ps列出所有正在运行的实例

      image-20230223125720495

      image-20230223125839650

      OPTIONS说明

      • -a列出所有正在运行的容器以及历史上运行过的

        image-20230223130300709

      • -l显示最近创建的容器

        image-20230223130406765

      • -n num显示最近创建的num个容器

        image-20230223130512616

      • -q静默模式,只显示容器的ID

        image-20230223130609207

    • exit在容器内部使用该命令退出,容器停止

      image-20230223131106057

      image-20230223131112876

      image-20230223131119347

    • ctrl+p+q在容器内部使用该命令退出容器,但是后台继续运行

      image-20230223131234253

    • docker start 容器ID|容器名称 启动容器

      image-20230223131444789

    • docker restart 容器ID|容器名称 重启容器

    • docker stop 容器ID|容器名称 停止容器

    • docker kill 容器ID|容器名称 强制停止容器

    • docker rm 容器ID|容器名称 删除已停止的容器

      image-20230223131809071

      -f强制删除

      image-20230223133245934

      docker rm -f $(docker ps -a -q) #删除全部容器

      image-20230223133513279

    • docker logs 容器ID|容器名称 查看实例运行的日志

      image-20230223135155479

    • docker top 容器ID|容器名称 查看容器内部的运行进程

      image-20230223135432256

    • docker inspect 容器ID|容器名称 查看容器内部的细节

      [root@localhost /]# docker inspect 201ee0f0e066
      [
          {
              "Id": "201ee0f0e066f2f685a81778ebda162cf16a00ceb56b103a4b2349ff6a1bfc51",
              "Created": "2023-02-23T05:42:50.600374754Z",
              "Path": "docker-entrypoint.sh",
              "Args": [
                  "redis-server"
              ],
              "State": {
                  "Status": "running",
                  "Running": true,
                  "Paused": false,
                  "Restarting": false,
                  "OOMKilled": false,
                  "Dead": false,
                  "Pid": 12109,
                  "ExitCode": 0,
                  "Error": "",
                  "StartedAt": "2023-02-23T05:42:51.129776954Z",
                  "FinishedAt": "0001-01-01T00:00:00Z"
              },
              "Image": "sha256:16ecd277293476392b71021cdd585c40ad68f4a7488752eede95928735e39df4",
              "ResolvConfPath": "/var/lib/docker/containers/201ee0f0e066f2f685a81778ebda162cf16a00ceb56b103a4b2349ff6a1bfc51/resolv.conf",
              "HostnamePath": "/var/lib/docker/containers/201ee0f0e066f2f685a81778ebda162cf16a00ceb56b103a4b2349ff6a1bfc51/hostname",
              "HostsPath": "/var/lib/docker/containers/201ee0f0e066f2f685a81778ebda162cf16a00ceb56b103a4b2349ff6a1bfc51/hosts",
              "LogPath": "/var/lib/docker/containers/201ee0f0e066f2f685a81778ebda162cf16a00ceb56b103a4b2349ff6a1bfc51/201ee0f0e066f2f685a81778ebda162cf16a00ceb56b103a4b2349ff6a1bfc51-json.log",
              "Name": "/my_redis",
              "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": [
                      23,
                      116
                  ],
                  "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/145e020b8af46eb498ff733e9fdd936aa7683fa349427c11b0c3293b50682b58-init/diff:/var/lib/docker/overlay2/a0a5e8c6eb47686e0c8285c3808a31f261d2618ef418ff1fb9ffe829383ccc1c/diff:/var/lib/docker/overlay2/2c985bc6807ddcbb1b121f1a70acc71b062e3f6ff46e54f8eee89932b71ee4ec/diff:/var/lib/docker/overlay2/92b4ad43e9d71cf23b6faeffd909b6fd45ab9bd92e2c13ab6c7b575671fce663/diff:/var/lib/docker/overlay2/7bc3017228eadbdfcf74a1f314fa7b0dda21f609ec785fd8f3f581d2fd8d328c/diff:/var/lib/docker/overlay2/3ae45773c030dbb58d4a36b5cdee19b613cedada96a3924388dd92d2a63c9ae2/diff:/var/lib/docker/overlay2/3a7991dc887dc9f1f59be4bc5a221eff5fae395f1a08d1d9418306af7a2a4077/diff",
                      "MergedDir": "/var/lib/docker/overlay2/145e020b8af46eb498ff733e9fdd936aa7683fa349427c11b0c3293b50682b58/merged",
                      "UpperDir": "/var/lib/docker/overlay2/145e020b8af46eb498ff733e9fdd936aa7683fa349427c11b0c3293b50682b58/diff",
                      "WorkDir": "/var/lib/docker/overlay2/145e020b8af46eb498ff733e9fdd936aa7683fa349427c11b0c3293b50682b58/work"
                  },
                  "Name": "overlay2"
              },
              "Mounts": [
                  {
                      "Type": "volume",
                      "Name": "a2a875de809671355c501cc92aef3ef6a394e893a73b40b3f69827b2ab1abe7d",
                      "Source": "/var/lib/docker/volumes/a2a875de809671355c501cc92aef3ef6a394e893a73b40b3f69827b2ab1abe7d/_data",
                      "Destination": "/data",
                      "Driver": "local",
                      "Mode": "",
                      "RW": true,
                      "Propagation": ""
                  }
              ],
              "Config": {
                  "Hostname": "201ee0f0e066",
                  "Domainname": "",
                  "User": "",
                  "AttachStdin": false,
                  "AttachStdout": false,
                  "AttachStderr": false,
                  "ExposedPorts": {
                      "6379/tcp": {}
                  },
                  "Tty": false,
                  "OpenStdin": false,
                  "StdinOnce": false,
                  "Env": [
                      "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                      "GOSU_VERSION=1.12",
                      "REDIS_VERSION=6.0.8",
                      "REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-6.0.8.tar.gz",
                      "REDIS_DOWNLOAD_SHA=04fa1fddc39bd1aecb6739dd5dd73858a3515b427acd1e2947a66dadce868d68"
                  ],
                  "Cmd": [
                      "redis-server"
                  ],
                  "Image": "redis:6.0.8",
                  "Volumes": {
                      "/data": {}
                  },
                  "WorkingDir": "/data",
                  "Entrypoint": [
                      "docker-entrypoint.sh"
                  ],
                  "OnBuild": null,
                  "Labels": {}
              },
              "NetworkSettings": {
                  "Bridge": "",
                  "SandboxID": "51036380c26087df1309c90d57f749d3496b1fddcc3ca67b0209bfada0fe5c53",
                  "HairpinMode": false,
                  "LinkLocalIPv6Address": "",
                  "LinkLocalIPv6PrefixLen": 0,
                  "Ports": {
                      "6379/tcp": null
                  },
                  "SandboxKey": "/var/run/docker/netns/51036380c260",
                  "SecondaryIPAddresses": null,
                  "SecondaryIPv6Addresses": null,
                  "EndpointID": "39abfccf67cb2d5604bb0cf01ea203daf1d3ee3a852a8999c66d37e88c0e6e75",
                  "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": "85e20ab616803f9c79cad58520c8fc16412e44b3f18d082ce68d5ca5beb68c06",
                          "EndpointID": "39abfccf67cb2d5604bb0cf01ea203daf1d3ee3a852a8999c66d37e88c0e6e75",
                          "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
                      }
                  }
              }
          }
      ]
    • docker exec -it 容器ID|容器名称 bash 重新进入容器内(需要容器实例正在运行中)

      image-20230223140342987

    • docker attach 容器ID|容器名称 重新进去容器内

      问:使用execattach的区别是什么?

      答:attach直接进入容器终端,使用exit命令退出,会导致容器停止;exit启动新终端进入容器,使用exit命令不会导致容器停止

      image-20230223141106169

    • docker cp 容器ID|容器名称:容器路径 主机路径复制容器内的文件到宿主机

      image-20230223142110912

      image-20230223142126002

      反之,宿主机的也可以复制到容器内

      image-20230223142330723

      image-20230223142340070

    • docker export 镜像ID|镜像名称 > 文件名.tar 把容器的内容导出到一个tar文件

      image-20230223142824214

    • cat 文件名.tar | docker import - 镜像用户/镜像名:镜像版本号

      image-20230223143110299

  • 前后台启动区别

    • 前台交互启动

      docker run -it redis:6.0.8

      适用于需要交互或者无法一直运行中的实例

    • 后台守护启动

      docker run -d redis:6.0.8
  • 视频教程资料

    0

    attach    Attach to a running container                 # 当前 shell 下 attach 连接指定运行镜像
    build     Build an image from a Dockerfile              # 通过 Dockerfile 定制镜像
    commit    Create a new image from a container changes   # 提交当前容器为新的镜像
    cp        Copy files/folders from the containers filesystem to the host path   #从容器中拷贝指定文件或者目录到宿主机中
    create    Create a new container                        # 创建一个新的容器,同 run,但不启动容器
    diff      Inspect changes on a container's filesystem   # 查看 docker 容器变化
    events    Get real time events from the server          # 从 docker 服务获取容器实时事件
    exec      Run a command in an existing container        # 在已存在的容器上运行命令
    export    Stream the contents of a container as a tar archive   # 导出容器的内容流作为一个 tar 归档文件[对应 import ]
    history   Show the history of an image                  # 展示一个镜像形成历史
    
    iages    List images                                   # 列出系统当前镜像
    import    Create a new filesystem image from the contents of a tarball # 从tar包中的内容创建一个新的文件系统映像[对应export]
    info      Display system-wide information               # 显示系统相关信息
    inspect   Return low-level information on a container   # 查看容器详细信息
    kill      Kill a running container                      # kill 指定 docker 容器
    load      Load an image from a tar archive              # 从一个 tar 包中加载一个镜像[对应 save]
    login     Register or Login to the docker registry server    # 注册或者登陆一个 docker 源服务器
    logout    Log out from a Docker registry server          # 从当前 Docker registry 退出
    logs      Fetch the logs of a container                 # 输出当前容器日志信息
    port      Lookup the public-facing port which is NAT-ed to PRIVATE_PORT    # 查看映射端口对应的容器内部源端口
    pause     Pause all processes within a container        # 暂停容器
    ps        List containers                               # 列出容器列表
    pull      Pull an image or a repository from the docker registry server   # 从docker镜像源服务器拉取指定镜像或者库镜像
    push      Push an image or a repository to the docker registry server    # 推送指定镜像或者库镜像至docker源服务器
    restart   Restart a running container                   # 重启运行的容器
    rm        Remove one or more containers                 # 移除一个或者多个容器
    rmi       Remove one or more images       # 移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除]
    run       Run a command in a new container              # 创建一个新的容器并运行一个命令
    save      Save an image to a tar archive                # 保存一个镜像为一个 tar 包[对应 load]
    search    Search for an image on the Docker Hub         # 在 docker hub 中搜索镜像
    start     Start a stopped containers                    # 启动容器
    stop      Stop a running containers                     # 停止容器
    tag       Tag an image into a repository                # 给源中镜像打标签
    top       Lookup the running processes of a container   # 查看容器中运行的进程信息
    unpause   Unpause a paused container                    # 取消暂停容器
    version   Show the docker version information           # 查看 docker 版本号
    wait      Block until a container stops, then print its exit code   # 截取容器停止时的退出状态值