5分钟带你掌握Docker全部命令

本文涉及的产品
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: 5分钟带你掌握Docker全部命令

Docker Run

基于镜像创建并运行新的容器

$ docker run -p 80:80 -d --restart=always --name nginx nginx:1.19.10
Unable to find image 'nginx:1.19.10' locally
1.19.10: Pulling from library/nginx
fcad0c936ea5: Pull complete
d5c473a8294d: Pull complete
8e31422295d0: Pull complete
209e8d4861a8: Pull complete
3314b286b1b3: Pull complete
574020532afe: Pull complete
Digest: sha256:df13abe416e37eb3db4722840dd479b00ba193ac6606e7902331dcea50f4f1f2
Status: Downloaded newer image for nginx:1.19.10
c1e3fca4ca4a37c83b56164bb48c2704b79c3a9f6362d541dbb6768980c78cb0

参数:

  • -p:端口映射,[宿主机端口]:[容器内部端口]
  • -d:后台运行
  • -- restart:容器重启策略,包含3个选项:no,on-failure[:times],always,unless-stopped
  • -- name:容器名称
  • nginx:1.19.10:镜像名称:命令版本号
  • -v:本参数用于指定容器卷

Docker Ps

查看正在运行的容器

$ docker ps
CONTAINER ID   IMAGE           COMMAND                   CREATED          STATUS          PORTS                NAMES
c1e3fca4ca4a   nginx:1.19.10   "/docker-entrypoint.…"   10 minutes ago   Up 10 minutes   0.0.0.0:80->80/tcp   nginx

参数:

  • -a:命令是用来展示所有所有的容器,包括未运行的容器

    Docker Images

    查看可用的镜像

    $ docker images
    REPOSITORY                                                   TAG       IMAGE ID       CREATED       SIZE
    nginx                                                        1.19.10   5aa5901ac9c7   2 years ago   126MB
    

    Docker Pull

    下载容器镜像

    $ docker pull mysql
    Using default tag: latest
    latest: Pulling from library/mysql
    1817bc1e6309: Pull complete
    740bd54462bc: Pull complete
    a7e5ed4e69b3: Pull complete
    8fdf88d7bbb7: Pull complete
    a1a5f8560950: Pull complete
    6d2014e71aa1: Pull complete
    a65eef362898: Pull complete
    1086465c0f61: Pull complete
    6d718a732c3a: Pull complete
    c724fd4b412f: Pull complete
    74fcbdd515b3: Pull complete
    Digest: sha256:51c4dc55d3abf4517a5a652794d1f0adb2f2ed1d1bedc847d6132d91cdb2ebbf
    Status: Downloaded newer image for mysql:latest
    docker.io/library/mysql:latest
    

    Docker Build

    基于Dockerfile构建镜像

    $ docker build -t < image_name >  < path_to_dockerfile >
    

    Docker Stop

    停止正在运行的容器

    $ docker stop nginx
    nginx
    

    Docker Start

    启用一个停止的容器

    $ docker start nginx
    nginx
    

    Docker Stop

    重新启动正在运行的容器

    $ docker restart nginx
    nginx
    

    Docker Rm

    删除已停止的容器

    $ docker rm nginx
    nginx
    

    Docker Rmi

    删除镜像

    $ docker rmi 5aa5901ac9c7
    Untagged: nginx:1.19.10
    Untagged: nginx@sha256:df13abe416e37eb3db4722840dd479b00ba193ac6606e7902331dcea50f4f1f2
    Deleted: sha256:5aa5901ac9c75b399796aacfd0113e2f04c0d58b87ba5057da539d5db91eb52b
    Deleted: sha256:e48a726b96dc3a89a57cd69e34b102c0327040dec7c53c6e294d7c17945ebe6c
    Deleted: sha256:bc4342d853f3c5272a21e8cdfc2490a35fc9e32dc858a656ee903392d4783e4e
    Deleted: sha256:002a6c87f9c8025acaac9a303dcdfc77b1d4b4a41510f93da1aa9b7b2910218d
    Deleted: sha256:d9f0cadb7a4d62e901ebd720a75cd2ba7abaacfe7d6131a7103e0f6e59998610
    Deleted: sha256:afbad4369b35c33f8f12849ebdddc9eb040be25d4997d8094f0d49c20668bc2c
    Deleted: sha256:388ba420b88d4b92c45f4f822e152056f8df29f17937b54f52abfd652563df6b
    

    参数:

  • 5aa5901ac9c7:镜像ID

Docker Exec

在运行中的容器内执行命令

$ docker exec nginx pwd
/

参数:

  • nginx:容器名称
  • pwd:命令

    Docker Cp

    在容器与宿主机之间复制文件与目录
    ```shell

    从容器Copy到宿主机

    $ docker cp nginx:/etc/nginx/nginx.conf .
    Successfully copied 2.56kB to /Users/chuanzhang/.
    $ ls | grep nginx
    nginx.conf

从宿主机Copy到容器

$ touch test # 创建test文件
$ docker cp test nginx:/opt
Successfully copied 1.54kB to nginx:/opt
$ docker exec nginx ls -l /opt/ # 通过命令确认copy到test文件
total 0
-rw-r--r-- 1 501 dialout 0 Jul 30 11:12 test

## Docker logs
查看容器的日志
```shell
$ docker logs nginx
/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

Docker Inspect

查看容器详情

$ docker inspect nginx
[
    {
        "Id": "1b18695a0cba1e9b868e8d00d9bed6159575fd9aa572fafc8efcb94ef03b2ad0",
        "Created": "2023-07-30T11:09:17.459773916Z",
        "Path": "/docker-entrypoint.sh",
        "Args": [
            "nginx",
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 11471,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2023-07-30T11:09:17.679558583Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:5aa5901ac9c75b399796aacfd0113e2f04c0d58b87ba5057da539d5db91eb52b",
        "ResolvConfPath": "/var/lib/docker/containers/1b18695a0cba1e9b868e8d00d9bed6159575fd9aa572fafc8efcb94ef03b2ad0/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/1b18695a0cba1e9b868e8d00d9bed6159575fd9aa572fafc8efcb94ef03b2ad0/hostname",
        "HostsPath": "/var/lib/docker/containers/1b18695a0cba1e9b868e8d00d9bed6159575fd9aa572fafc8efcb94ef03b2ad0/hosts",
        "LogPath": "/var/lib/docker/containers/1b18695a0cba1e9b868e8d00d9bed6159575fd9aa572fafc8efcb94ef03b2ad0/1b18695a0cba1e9b868e8d00d9bed6159575fd9aa572fafc8efcb94ef03b2ad0-json.log",
        "Name": "/nginx",
        "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": "80"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "always",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "ConsoleSize": [
                36,
                200
            ],
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "private",
            "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": null,
            "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/89dd504f61449497682358a404f447c3a822532a22a02bcdabafe7316d6c690b-init/diff:/var/lib/docker/overlay2/b63891a8d0cd62734f73a35d761ddd3393cb7d1a2ff9e4a3d4374861af3bc8c2/diff:/var/lib/docker/overlay2/dd62dde50a162103d7086505dc8840ddae32e619b85ee94fbaa50ec74bee8690/diff:/var/lib/docker/overlay2/9a38961a92062a9266fd00d3f547374e3fed7e930de9ac1206409216e425928f/diff:/var/lib/docker/overlay2/478a51c0e9b4049e18b87a6149ac552233d7bd3508da6b302edb607c6ddbd1b0/diff:/var/lib/docker/overlay2/e4a629b33969b511c39de7795af158de44b4a478d3cfbcd5e0bf1d71affa3f59/diff:/var/lib/docker/overlay2/27d9fd58215a2d235aeaaf2eab6124b871ac8871b0b45efd529dbf3f425d40b5/diff",
                "MergedDir": "/var/lib/docker/overlay2/89dd504f61449497682358a404f447c3a822532a22a02bcdabafe7316d6c690b/merged",
                "UpperDir": "/var/lib/docker/overlay2/89dd504f61449497682358a404f447c3a822532a22a02bcdabafe7316d6c690b/diff",
                "WorkDir": "/var/lib/docker/overlay2/89dd504f61449497682358a404f447c3a822532a22a02bcdabafe7316d6c690b/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "1b18695a0cba",
            "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.19.10",
                "NJS_VERSION=0.5.3",
                "PKG_RELEASE=1~buster"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "Image": "nginx:1.19.10",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": [
                "/docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers \u003cdocker-maint@nginx.com\u003e"
            },
            "StopSignal": "SIGQUIT"
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "5b98bf99514ab4c13d6bc93ba1d0710079ad3e0be8b982fa13bae6624e3de094",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "80"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/5b98bf99514a",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "96b8755912b23ea85ab28a6abe8a7141aca735bb7a1215bf8e85c0cd1852f369",
            "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": "f72918adb5183fc2895dee8c4d8cb5bdbedaa29df3426464cd2889fd993fe1c1",
                    "EndpointID": "96b8755912b23ea85ab28a6abe8a7141aca735bb7a1215bf8e85c0cd1852f369",
                    "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 Network Create

创建Docker网络

$ docker network create <网络名称>

Docker Network Connect

将容器链接到指定的网络

$ docker network connect <网络名称>  <容器>
相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
19天前
|
安全 Docker 容器
|
19天前
|
网络安全 数据安全/隐私保护 Docker
|
6天前
|
Java 应用服务中间件 Linux
【Docker容器化技术】docker安装与部署、常用命令、容器数据卷、应用部署实战、Dockerfile、服务编排docker-compose、私有仓库
本文主要讲解了Docker的安装与部署、常用命令、容器数据卷、应用部署实战、Dockerfile、服务编排docker-compose、私有仓库以及Docker容器虚拟化与传统虚拟机比较。
137 6
【Docker容器化技术】docker安装与部署、常用命令、容器数据卷、应用部署实战、Dockerfile、服务编排docker-compose、私有仓库
|
18天前
|
存储 监控 安全
|
2月前
|
Linux Docker 容器
Docker操作 :容器命令
Docker操作 (四)
|
18天前
|
存储 关系型数据库 MySQL
|
26天前
|
网络协议 Docker 容器
docker pull命令拉取镜像失败的解决方案
docker pull命令拉取镜像失败的解决方案
601 1
|
28天前
|
安全 网络安全 开发者
Docker学习笔记(一):Docker命令总结
本文介绍了Docker的基本概念、优点以及常用命令。Docker是一个开源的应用容器引擎,它通过容器打包应用程序及其依赖项,实现快速部署和迁移。主要优点包括轻量级、可移植性、易于管理、安全性和开源性。文章还区分了镜像和容器的概念,并提供了构建镜像、查看容器、运行容器、停止和删除容器等常用Docker命令的示例。
124 0
|
1月前
|
Linux 网络安全 Docker
安装后无法使用 Docker 命令
【10月更文挑战第3天】
143 2
|
1月前
|
安全 Shell Linux
docker进入容器命令
docker进入容器命令
下一篇
无影云桌面