1. 程式人生 > 實用技巧 >Docker 安裝及常用基礎命令使用

Docker 安裝及常用基礎命令使用

docker的安裝

  • centos
# centos7
[root@centos7 ~]#wget -P /etc/yum.repos.d/ https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

[root@centos7 ~]#yum repolist 

[root@centos7 ~]#yum list docker-ce* --showduplicates | sort -r

[root@centos7 ~]#yum -y install docker-ce-19.03.9-3.el7 docker-ce-cli-19.03.9-3.el7

# centos8
[root@centos8 ~]#cat > /etc/yum.repos.d/docker.repo <<EOF
[docker]
name=docker
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/8/x86_64/stable/
gpgcheck=0
EOF

[root@centos8 ~]#dnf -y install docker-ce
  • ubuntu
[root@ubuntu2004:~]#apt update
[root@ubuntu2004:~]#apt -y install apt-transport-https ca-certificates curl software-properties-common
[root@ubuntu2004:~]#curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
[root@ubuntu2004:~]#add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
[root@ubuntu2004:~]#apt update
[root@ubuntu2004:~]#apt-cache madison docker-ce
 docker-ce | 5:19.03.13~3-0~ubuntu-focal | https://mirrors.aliyun.com/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:19.03.12~3-0~ubuntu-focal | https://mirrors.aliyun.com/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:19.03.11~3-0~ubuntu-focal | https://mirrors.aliyun.com/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:19.03.10~3-0~ubuntu-focal | https://mirrors.aliyun.com/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:19.03.9~3-0~ubuntu-focal | https://mirrors.aliyun.com/docker-ce/linux/ubuntu focal/stable amd64 Packages
[root@ubuntu2004:~]#apt -y  install docker-ce=5:19.03.13~3-0~ubuntu-focal docker-ce-cli=5:19.03.13~3-0~ubuntu-focal

映象加速

# 使用國內映象加速
# 阿里雲(需要登入賬號分配地址)
# 網易雲 https://vgunv6qp.mirror.aliyuncs.com
# 騰訊雲 https://mirror.ccs.tencentyun.com
# 中科大 https://docker.mirrors.ustc.edu.cn
# docker中國 https://registry.docker-cn.com
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
    "registry-mirrors": [
        "https://mirror.ccs.tencentyun.com",
        "https://docker.mirrors.ustc.edu.cn",
        "https://registry.docker-cn.com"
    ]
}
EOF
systemctl daemon-reload
systemctl restart docker

Docker 常用命令

比較簡單的命令就不介紹了,自己敲一遍就能知道效果了哦@_@~~ 。

# docker 所有命令
[root@ubuntu2004:~]# docker 
attach     create     image      logout     pull       search     system     wait
build      diff       images     logs       push       secret     tag        
builder    engine     import     network    rename     service    top        
commit     events     info       node       restart    stack      trust      
config     exec       inspect    pause      rm         start      unpause    
container  export     kill       plugin     rmi        stats      update     
context    help       load       port       run        stop       version    
cp         history    login      ps         save       swarm      volume 

docker images命令

[root@ubuntu2004:~]# docker images --help

Usage:	docker images [OPTIONS] [REPOSITORY[:TAG]]

List images

Options:
  -a, --all             Show all images (default hides intermediate images)
      --digests         Show digests
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print images using a Go template
      --no-trunc        Don't truncate output
  -q, --quiet           Only show numeric IDs

檢視本地的所有映象

[root@ubuntu2004:~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              1                   2e8c11ff9b98        13 hours ago        215MB
wordpress           latest              6edecd0f5c75        13 days ago         546MB
nginx               1.19.4-alpine       e5dcd7aa4b5e        13 days ago         21.8MB
nginx               latest              c39a868aad02        13 days ago         133MB
alpine              latest              d6e46aa2470d        4 weeks ago         5.57MB
busybox             latest              f0b02e9d092d        5 weeks ago         1.23MB
mysql               5.7.31              42cdba9f1b08        5 weeks ago         448MB
centos              latest              0d120b6ccaa8        3 months ago        215MB

# 檢視所有本地映象ID
[root@ubuntu2004:~]# docker images -q
2e8c11ff9b98
6edecd0f5c75
e5dcd7aa4b5e
c39a868aad02
d6e46aa2470d
f0b02e9d092d
42cdba9f1b08
0d120b6ccaa8

docker run命令

docker run [選項] [映象名] [shell命令] [引數]
# 常用選項: 
-i, --interactive		Keep STDIN open even if not attached,通常和-t一起使用
-t, --tty				分配pseudo-TTY,通常和-i一起使用,注意對應的容器必須執行shell才支援進入
-d, --detach			Run container in background and print container ID
--name string			Assign a name to the container
--h, --hostname			string Container host name 
--rm					Automatically remove the container when it exits
-p, --publish list		Publish a container's port(s) to the host
-P, --publish-all		Publish all exposed ports to random ports
--dns list				Set custom DNS servers
--entrypoint string		Overwrite the default ENTRYPOINT of the image
--restart policy		docker容器是否隨宿主機重啟而重啟
--privileged			Give extended privileges to container
-e, --env=[]			Set environment variables
--env-file=[]			Read in a line delimited file of environment variables

建立容器並退出

注意:容器啟動後,如果容器內沒有前臺執行的程序,將自動退出停止

# exit正常退出後容器停止
[root@ubuntu2004:~]# docker run -it --name alpine-1 alpine
/ # cat /etc/issue 
Welcome to Alpine Linux 3.12
Kernel \r on an \m (\l)

/ # exit
[root@ubuntu2004:~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

# 同時按ctrl+p+q 三個鍵退出後,容器不停止
[root@ubuntu2004:~]# docker run -it --name alpine-2 alpine
/ # [root@ubuntu2004:docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
c9c8946e7788        alpine              "/bin/sh"                15 seconds ago      Up 14 seconds                               alpine-2

建立守護式容器

守護式容器的三個特點:

  • 能夠長期執行
  • 無需互動式會話
  • 適合執行應用程式和服務
# 直接docker run 守護式程序,程序會以前臺方式執行
[root@ubuntu2004:~]# docker run nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
bb79b6b2107f: Pull complete 
5a9f1c0027a7: Pull complete 
b5c20b2b484f: Pull complete 
166a2418f7e8: Pull complete 
1966ea362d23: Pull complete 
Digest: sha256:aeade65e99e5d5e7ce162833636f692354c227ff438556e5f3ed0335b7cc2f1b
Status: Downloaded newer image for nginx:latest
/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: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: 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: Configuration complete; ready for start up

# 使用-d選項後臺執行
[root@ubuntu2004:~]# docker run -d nginx
940ad0d3d4dfb425918ab796b3b49351b621cd64793c8cbb534b8dc6f14f4743

# 不是守護式程序的容器加上-d後也無法保持後臺持續執行
[root@ubuntu2004:~]# docker run -d --name alpine-3 alpine
01db2a63e66f26c4ac087566ef7eb32ee4405862e2bdb2da25a87c7bac6f228d
[root@ubuntu2004:~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

執行docker官方文件容器

[root@ubuntu2004:~]#docker run -it -d -p 4000:4000 docs/docker.github.io:latest

# -p 4000:4000   將本地的4000埠對映到容器中的4000埠
# 埠對映的本質就是利用NAT技術實現的 可以使用iptables -vnL -t nat檢視

# 用瀏覽器訪問http://localhost:4000/可以看到和docker官方一摸一樣的文件資料

docker ps命令

docker ps [OPTIONS]
docker container ls [OPTIONS]
# 常用選項:
-a,--all		Show all containers (default shows just running)
-q,--quiet 		Only display numeric IDs
-s,--size		Display total file sizes
-f,--filter		filter Filter output based on conditions provided
-l,--latest		Show the latest created container (includes all states)
-n,--last int	Show n last created containers (includes all states)(default -1)

檢視正在執行的容器

[root@ubuntu2004:~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
c0cd1845f05a        nginx:latest        "/docker-entrypoint.…"   28 minutes ago      Up 28 minutes       80/tcp              busy_kowalevski
7b904e7642e3        nginx:latest        "/docker-entrypoint.…"   28 minutes ago      Up 28 minutes       80/tcp              gallant_wilson

檢視所有容器

[root@ubuntu2004:~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
10fce7a84d79        centos              "/bin/bash"              26 seconds ago      Exited (0) 25 seconds ago                       gallant_gauss
c0cd1845f05a        nginx:latest        "/docker-entrypoint.…"   28 minutes ago      Up 28 minutes               80/tcp              busy_kowalevski
7b904e7642e3        nginx:latest        "/docker-entrypoint.…"   28 minutes ago      Up 28 minutes               80/tcp              gallant_wilson
d764f04ef242        nginx:latest        "/docker-entrypoint.…"   28 minutes ago      Exited (0) 28 minutes ago                       youthful_chaplygin

只檢視容器ID

[root@ubuntu2004:~]# docker ps -aq
10fce7a84d79
c0cd1845f05a
7b904e7642e3
d764f04ef242

檢視容器大小

[root@ubuntu2004:~]# docker ps -as
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES                SIZE
10fce7a84d79        centos              "/bin/bash"              2 minutes ago       Exited (0) 2 minutes ago                        gallant_gauss        0B (virtual 215MB)
c0cd1845f05a        nginx:latest        "/docker-entrypoint.…"   29 minutes ago      Up 29 minutes               80/tcp              busy_kowalevski      1.12kB (virtual 133MB)
7b904e7642e3        nginx:latest        "/docker-entrypoint.…"   29 minutes ago      Up 29 minutes               80/tcp              gallant_wilson       1.12kB (virtual 133MB)
d764f04ef242        nginx:latest        "/docker-entrypoint.…"   30 minutes ago      Exited (0) 30 minutes ago                       youthful_chaplygin   1.11kB (virtual 133MB)

docker top命令

[root@ubuntu2004:~]# docker top --help

Usage:	docker top CONTAINER [ps OPTIONS]

Display the running processes of a container

檢視容器內的程序

[root@ubuntu2004:~]# docker top c0cd1845f05a
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                62612               62592               0                   01:15               ?                   00:00:00            nginx: master process nginx -g daemon off;
systemd+            62664               62612               0                   01:15               ?                   00:00:00            nginx: worker process

docker stats命令

docker stats [OPTIONS] [CONTAINER...]
Display a live stream of container(s) resource usage statistics
Options:
-a,
--all Show all containers (default shows just running)
--format string Pretty-print images using a Go template
--no-stream Disable streaming stats and only pull the first result
--no-trunc Do not truncate output

檢視所有容器資源使用情況

[root@ubuntu2004:~]# docker stats -a
CONTAINER ID        NAME                 CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
10fce7a84d79        gallant_gauss        0.00%               0B / 0B               0.00%               0B / 0B             0B / 0B             0
c0cd1845f05a        busy_kowalevski      0.00%               3.52MiB / 3.817GiB    0.09%               1.5kB / 378B        0B / 8.19kB         2
7b904e7642e3        gallant_wilson       0.00%               3.543MiB / 3.817GiB   0.09%               1.45kB / 0B         7.64MB / 8.19kB     2
d764f04ef242        youthful_chaplygin   0.00%               0B / 0B               0.00%               0B / 0B             0B / 0B             0

docker inspect命令

docker inspect 可以檢視docker各種物件的詳細資訊,包括:映象,容器,卷組,網路等。

[root@ubuntu2004:~]# docker inspect --help

Usage:	docker inspect [OPTIONS] NAME|ID [NAME|ID...]

Return low-level information on Docker objects

Options:
  -f, --format string   Format the output using the given Go template
  -s, --size            Display total file sizes if the type is container
      --type string     Return JSON for specified type

檢視容器的詳細資訊

[root@ubuntu2004:~]# docker inspect c0cd1845f05a
[
    {
        "Id": "c0cd1845f05aa3d79cd827e466b45a89cc6c79b746559cf26207f8dca9c0f658",
        "Created": "2020-11-19T01:15:09.19046664Z",
        "Path": "/docker-entrypoint.sh",
        "Args": [
            "nginx",
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 62612,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-11-19T01:15:09.460776077Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:c39a868aad02a383c7e490e0fc4a5b0217f667f2de764bc2755e315a5adf64a1",
        "ResolvConfPath": "/var/lib/docker/containers/c0cd1845f05aa3d79cd827e466b45a89cc6c79b746559cf26207f8dca9c0f658/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/c0cd1845f05aa3d79cd827e466b45a89cc6c79b746559cf26207f8dca9c0f658/hostname",
        "HostsPath": "/var/lib/docker/containers/c0cd1845f05aa3d79cd827e466b45a89cc6c79b746559cf26207f8dca9c0f658/hosts",
        "LogPath": "/var/lib/docker/containers/c0cd1845f05aa3d79cd827e466b45a89cc6c79b746559cf26207f8dca9c0f658/c0cd1845f05aa3d79cd827e466b45a89cc6c79b746559cf26207f8dca9c0f658-json.log",
        "Name": "/busy_kowalevski",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "docker-default",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Capabilities": null,
            "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",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "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/5e45ea52bb1c970374d2e8b0ab0a5aebd4c60db2fc64c41dc0c5fe69b359d4f2-init/diff:/var/lib/docker/overlay2/4796d1618a7729d7484d22d694f49281c05555f1739a4bbd30d4712d4101ad26/diff:/var/lib/docker/overlay2/db1e998c2e21ae249b429c5d218493c7a7e40531eb497970e74da558526f7861/diff:/var/lib/docker/overlay2/d11973088fb534a1e44a34a34242c95ff1cc0bd69e60e34480b8e5eaffdae675/diff:/var/lib/docker/overlay2/23168c08eb2cf3f6d2a9793c9b173eea9324c998a956e5dd693665c7131d78c2/diff:/var/lib/docker/overlay2/137f16ad5a65b60a4e5709d3943097e5e4187ff3435c1b223043ec5888af4e3a/diff",
                "MergedDir": "/var/lib/docker/overlay2/5e45ea52bb1c970374d2e8b0ab0a5aebd4c60db2fc64c41dc0c5fe69b359d4f2/merged",
                "UpperDir": "/var/lib/docker/overlay2/5e45ea52bb1c970374d2e8b0ab0a5aebd4c60db2fc64c41dc0c5fe69b359d4f2/diff",
                "WorkDir": "/var/lib/docker/overlay2/5e45ea52bb1c970374d2e8b0ab0a5aebd4c60db2fc64c41dc0c5fe69b359d4f2/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "c0cd1845f05a",
            "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.4",
                "NJS_VERSION=0.4.4",
                "PKG_RELEASE=1~buster"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "Image": "nginx:latest",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": [
                "/docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <[email protected]>"
            },
            "StopSignal": "SIGTERM"
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "6027eb3645da8c3b08df8225974f2edac3ceabadfb19309cbe507d9482dd05f7",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/6027eb3645da",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "9aa986512651600b92d8ca025d0286861b0e376e87388706992074ba415e8bbc",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.3",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:03",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "6b30f92d502d0ef889d6a05dfca2c8f5b70b58baa775ab5b88c10dbbf49f0b4f",
                    "EndpointID": "9aa986512651600b92d8ca025d0286861b0e376e87388706992074ba415e8bbc",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:03",
                    "DriverOpts": null
                }
            }
        }
    }
]

# 使用-f命令過濾想要檢視的內容
[root@ubuntu2004:~]# docker inspect -f "{{.Id}}" c0cd1845f05a
c0cd1845f05aa3d79cd827e466b45a89cc6c79b746559cf26207f8dca9c0f658

[root@ubuntu2004:~]# docker inspect -f "{{.Name}}" c0cd1845f05
nginx1

[root@ubuntu2004:~]# docker inspect -f "{{.NetworkSettings.Networks.bridge.IPAddress}}" c0cd1845f05a
172.17.0.3

檢視映象的詳細資訊

[root@ubuntu2004:~]# docker inspect centos:latest
[
    {
        "Id": "sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566",
        "RepoTags": [
            "centos:latest"
        ],
        "RepoDigests": [
            "centos@sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2020-08-10T18:19:49.837885498Z",
        "Container": "3b04ecd9fb2d3f921f12d858edf5f3a6aa7c36c8e1e6f74bd32555fd4d7f7da2",
        "ContainerConfig": {
            "Hostname": "3b04ecd9fb2d",
            "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 [\"/bin/bash\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:69587a438b2c9b803db8ed4f6e6b5abce25a6b8ec2583a394807cf82bfd23693",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20200809",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "DockerVersion": "18.09.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": [
                "/bin/bash"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:69587a438b2c9b803db8ed4f6e6b5abce25a6b8ec2583a394807cf82bfd23693",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20200809",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 215102299,
        "VirtualSize": 215102299,
        "GraphDriver": {
            "Data": {
                "MergedDir": "/var/lib/docker/overlay2/3b7195f52b9dd270452778ff94a6696f4a08048f4bc05ece189527b864454576/merged",
                "UpperDir": "/var/lib/docker/overlay2/3b7195f52b9dd270452778ff94a6696f4a08048f4bc05ece189527b864454576/diff",
                "WorkDir": "/var/lib/docker/overlay2/3b7195f52b9dd270452778ff94a6696f4a08048f4bc05ece189527b864454576/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:291f6e44771a7b4399b0c6fb40ab4fe0331ddf76eda11080f052b003d96c7726"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]

檢視卷組的詳細資訊

# docker volume ls命令檢視所有卷組
[root@ubuntu2004:~]# docker volume ls
DRIVER              VOLUME NAME
local               nginx1

[root@ubuntu2004:~]# docker inspect nginx1
[
    {
        "CreatedAt": "2020-11-18T02:56:51Z",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/nginx1/_data",
        "Name": "nginx1",
        "Options": {},
        "Scope": "local"
    }
]

docker rm命令

docker rm命令可以刪除容器,即使容器正在運行當中,也可以被強制刪除掉。

[root@ubuntu2004:~]# docker rm --help

Usage:	docker rm [OPTIONS] CONTAINER [CONTAINER...]

Remove one or more containers

Options:
  -f, --force     Force the removal of a running container (uses SIGKILL)
  -l, --link      Remove the specified link
  -v, --volumes   Remove anonymous volumes associated with the container

刪除指定容器

# 檢視所有映象
[root@ubuntu2004:~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES
10fce7a84d79        centos              "/bin/bash"              59 minutes ago      Exited (0) 59 minutes ago                          gallant_gauss
c0cd1845f05a        nginx:latest        "/docker-entrypoint.…"   About an hour ago   Up About an hour               80/tcp              busy_kowalevski
7b904e7642e3        nginx:latest        "/docker-entrypoint.…"   About an hour ago   Up About an hour               80/tcp              gallant_wilson
d764f04ef242        nginx:latest        "/docker-entrypoint.…"   About an hour ago   Exited (0) About an hour ago                       youthful_chaplygin

# 刪除退出狀態的映象
[root@ubuntu2004:~]# docker rm 10fce7a84d79
10fce7a84d79

# 正在執行的容器無法直接刪除,需要加上-f選項
[root@ubuntu2004:~]# docker rm c0cd1845f05a
Error response from daemon: You cannot remove a running container c0cd1845f05aa3d79cd827e466b45a89cc6c79b746559cf26207f8dca9c0f658. Stop the container before attempting removal or force remove

[root@ubuntu2004:~]# docker rm -f c0cd1845f05a
c0cd1845f05a

刪除所有映象

[root@ubuntu2004:~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                   PORTS               NAMES
7b904e7642e3        nginx:latest        "/docker-entrypoint.…"   2 hours ago         Up 2 hours               80/tcp              gallant_wilson
d764f04ef242        nginx:latest        "/docker-entrypoint.…"   2 hours ago         Exited (0) 2 hours ago                       youthful_chaplygin

[root@ubuntu2004:~]# docker ps -aq
7b904e7642e3
d764f04ef242

[root@ubuntu2004:~]# docker rm -f `docker ps -aq`
7b904e7642e3
d764f04ef242

[root@ubuntu2004:~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

刪除指定狀態的容器

[root@ubuntu2004:~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
ac6c12ad0109        centos              "/bin/bash"              6 seconds ago       Exited (0) 5 seconds ago                        charming_lalande
bf763ce9bd7a        nginx               "/docker-entrypoint.…"   10 seconds ago      Up 8 seconds                80/tcp              quirky_allen
4bfe2c25e29c        nginx               "/docker-entrypoint.…"   12 seconds ago      Up 10 seconds               80/tcp              peaceful_wilson
a4361960b5c2        centos              "/bin/bash"              18 seconds ago      Exited (0) 16 seconds ago                       hopeful_gagarin

[root@ubuntu2004:~]# docker ps -qf status=exited
ac6c12ad0109
a4361960b5c2

[root@ubuntu2004:~]# docker rm `docker ps -qf status=exited`
ac6c12ad0109
a4361960b5c2

# 刪除所有退出狀態的容器
[root@ubuntu2004:~]# docker container prune -f

docker rmi命令

[root@ubuntu2004:~]# docker rmi --help

Usage:	docker rmi [OPTIONS] IMAGE [IMAGE...]

Remove one or more images

Options:
  -f, --force      Force removal of the image
      --no-prune   Do not delete untagged parents

刪除本地映象

[root@ubuntu2004:~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
centos                  1                   2e8c11ff9b98        13 hours ago        215MB
wordpress               latest              6edecd0f5c75        13 days ago         546MB
nginx                   1.19.4-alpine       e5dcd7aa4b5e        13 days ago         21.8MB
nginx                   latest              c39a868aad02        13 days ago         133MB
alpine                  latest              d6e46aa2470d        4 weeks ago         5.57MB
busybox                 latest              f0b02e9d092d        5 weeks ago         1.23MB
mysql                   5.7.31              42cdba9f1b08        5 weeks ago         448MB
centos                  latest              0d120b6ccaa8        3 months ago        215MB
docs/docker.github.io   latest              32ed84d97e30        5 months ago        1GB

# 刪除沒有正在使用的映象
[root@ubuntu2004:~]# docker rmi 2e8c11ff9b98
Untagged: centos:1
Deleted: sha256:2e8c11ff9b9826da52d3a910f907b1a1f5aefbea7bf8e958900c712696b7c93b
Deleted: sha256:99d7ed662d3e138b2359f6a62e2b1da1dbff05ee9f400945e96c00e69aa947c5

# 正在使用的映象必須先刪除容器才能刪除本地映象
[root@ubuntu2004:~]# docker ps
CONTAINER ID        IMAGE                          COMMAND                  CREATED             STATUS              PORTS                            NAMES
53c10fb222ac        nginx                          "/docker-entrypoint.…"   4 seconds ago       Up 3 seconds        80/tcp                           quizzical_pascal
e646b05f701f        docs/docker.github.io:latest   "/docker-entrypoint.…"   6 minutes ago       Up 6 minutes        80/tcp, 0.0.0.0:4000->4000/tcp   docs

[root@ubuntu2004:~]# docker rmi -f c39a868aad02
Error response from daemon: conflict: unable to delete c39a868aad02 (cannot be forced) - image is being used by running container 53c10fb222ac

[root@ubuntu2004:~]# docker rm -f 53c10fb222ac
53c10fb222ac

[root@ubuntu2004:~]# docker rmi -f c39a868aad02
Untagged: nginx:latest
Untagged: nginx@sha256:4a6486628e485df22567dbc750ad7de9c3d899e5554f99bc4c955550c35029ca
Deleted: sha256:c39a868aad02a383c7e490e0fc4a5b0217f667f2de764bc2755e315a5adf64a1
Deleted: sha256:1af47386c12cb24b362bd785e71058ab675fbdcbda8e7a7ba0dd10c3cff75a2a
Deleted: sha256:25ee02f9e42f8cdca7ca8bc522cb69ac4a86a55401818a13c7a01e52a81894fe
Deleted: sha256:b1d2bc3292aa3d64794f99d2885d35b454993b4af24ad350a969d27925db7ebb
Deleted: sha256:4216e20d59f9fc90bccdd14493452532806d4803a25514366de2a9c5560624d0

# 刪除所有映象
# 生產中不建議使用這個命令,除非機器上已經不再執行任何服務了。
[root@ubuntu2004:~]# docker rmi -f `docker images -q`
Untagged: wordpress:latest
Untagged: wordpress@sha256:20bffad04c9c3e696b3c6fbc48d769c5948718b57af8c9457d9a0f28b5066b4b
Deleted: sha256:6edecd0f5c75d6a23917083f2b3117584298b63491083e0b51a732a7e7525e46
Deleted: sha256:d693b131be3ff494f72ee7857c3bce92d6ca86ab0163c1340146f1dfcbf83194
Deleted: sha256:d977da63444a00b6484111ea2c5df982199526d3bbe1c9191af0c7e0a8b2ef57
Deleted: sha256:39fd1eff143aefee7e281bd18d6b1669f7695d48dbd349a8707d5e6f6ff63df8
Deleted: sha256:bf06c6976b0bb74588eee20b82d48ae662e998c9142fbff983a29623f95a310b
Deleted: sha256:0de1ae7c536707e6622a66eb61bdc8c7faa54e628be57008231ec737dae0bc21
Deleted: sha256:db608032d5af7ff5f9c0fe835c7d3166dda03e8e1be6febaa4a5c86f3b242755
Deleted: sha256:fb1f713ddd75765363062c1b8216b19b0db4dfd4a8e4931acd8d6309e086c80b
Deleted: sha256:5b18b3de0df071206fac12304bfa6c125ad62543a95f799cf0d8d5c87e1550ab
Deleted: sha256:ac8ffd06941e4362594dd525cd5b89c260dfc4891434c61965b89745eddd58e0
Deleted: sha256:66dc4c95859eb8a6c621a978e3b95c3b583814bb9ce681004f0cff84785136ca
Deleted: sha256:b71509cb2fefe70d716bf6fa4298c6af24183efbf6b266540655e1941b92be3f
Deleted: sha256:ef631a93873f1adc380552cb05a5da7240c5a34a9421b38e81bd50cc0964dc63
Deleted: sha256:20a8271743f421bd5c9fe211563b31b8d963011649cf06016cc3422c200af462
Deleted: sha256:7c9f87dcf11402adaca42628822e456d57ef72650d7bb74a7017cb6dd23f3f4e
Deleted: sha256:41d757f252acd6d3e9a1e74ecd6b8206b59bc9750d5e483f2cb296a8a9b46b1e
Deleted: sha256:78449f08345c06fb39159f25655c69ccddf1a442c4748b22b4e4f2cf0dc5a0bf
Deleted: sha256:6205e1b7938bba4ffd3ae73a7568fed537eb11d501797f5fd78aacbd23081c26
Deleted: sha256:4c558280255ffc3b925ee5d49d3e9fea4d1510752d3e1a21906715643ad0810a
Deleted: sha256:0d982cc52a565e630304ea9311bdaac4d4247fcbfd4006fa5fa43b1e1de932d2
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:fff16eea1a8ae92867721d90c59a75652ea66d29c05294e6e2f898704bdb8cf1
Deleted: sha256:d70eaf7277eada08fca944de400e7e4dd97b1262c06ed2b1011500caa4decaf1
Deleted: sha256:778d52487737cf5362fd95086fa55793001dbc7b331344a540a594824e2994fd
Deleted: sha256:7011438f48b79cbf5fce3bfba74aed2e53fe5fe6a3b7fd6fe03018d28caee7a3
Deleted: sha256:47dde53750b4a8ed24acebe52cf31ad131e73a9611048fc2f92c9b6274ab4bf3
Untagged: alpine:latest
Untagged: alpine@sha256:c0e9560cda118f9ec63ddefb4a173a2b2a0347082d7dff7dc14272e7841a5b5a
Deleted: sha256:d6e46aa2470df1d32034c6707c8041158b652f38d2a9ae3d7ad7e7532d22ebe0
Untagged: busybox:latest
Untagged: busybox@sha256:a9286defaba7b3a519d585ba0e37d0b2cbee74ebfe590960b0b1d6a5e97d1e1d
Deleted: sha256:f0b02e9d092d905d0d87a8455a1ae3e9bb47b4aa3dc125125ca5cd10d6441c9f
Deleted: sha256:d2421964bad195c959ba147ad21626ccddc73a4f2638664ad1c07bd9df48a675
Untagged: mysql:5.7.31
Untagged: mysql@sha256:b3dc8d10307ab7b9ca1a7981b1601a67e176408be618fc4216d137be37dae10b
Deleted: sha256:42cdba9f1b0840cd63254898edeaf6def81a503a6a53d57301c3b38e69cd8f15
Deleted: sha256:d163436e5e1e8180feaf30af2ecbfd3b6a2e86a66b805dcd1e6e0e9ba26be92f
Deleted: sha256:eae434fad508aeec80daf1a0fc16ba89f65707f76a31db7f63883bebf208b743
Deleted: sha256:78b3fc624268ff3cf2ff6558e8c9d47babda61033966f4443e2430dd2607163b
Deleted: sha256:10a3c92754ac4860a385e2095dbe35a1486b4aa2109421a58f3c91f3ce6dbfce
Deleted: sha256:33134afe9e842a2898e36966f222fcddcdb2ab42e65dcdc581a4b38b2852c2e0
Deleted: sha256:dd053ec71039c3646324372061452778609b9ba42d1501f6a72c0272c94f8965
Deleted: sha256:2d4c647f875f6256f210459e5c401aad27ad223d0fa3bada7c4088a6040f8ba4
Deleted: sha256:4bded7e9aa769cb0560e60da97421e2314fa896c719179b926318640324d7932
Deleted: sha256:5fd9447ef700dfe5975c4cba51d3c9f2e757b34782fe145c1d7a12dfbee1da2f
Deleted: sha256:5ee7cbb203f3a7e90fe67330905c28e394e595ea2f4aa23b5d3a06534a960553
Deleted: sha256:d0fe97fa8b8cefdffcef1d62b65aba51a6c87b6679628a2b50fc6a7a579f764c
Untagged: centos:latest
Untagged: centos@sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd
Deleted: sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566
Deleted: sha256:291f6e44771a7b4399b0c6fb40ab4fe0331ddf76eda11080f052b003d96c7726
Error response from daemon: conflict: unable to delete 32ed84d97e30 (cannot be forced) - image is being used by running container e646b05f701f

# 正在執行容器的映象並沒有被刪除
[root@ubuntu2004:~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docs/docker.github.io   latest              32ed84d97e30        5 months ago        1GB

docker exec命令

docker exec命令可以在執行的容器中開啟一個新程序,並分配一個終端進入到正在執行中的容器,使用exit退出也不影響容器的繼續執行。也可以不進入容器,僅執行單次命令。

[root@ubuntu2004:~]# docker exec --help

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

Run a command in a running container

Options:
  -d, --detach               Detached mode: run command in the background
      --detach-keys string   Override the key sequence for detaching a container
  -e, --env list             Set environment variables
  -i, --interactive          Keep STDIN open even if not attached
      --privileged           Give extended privileges to the command
  -t, --tty                  Allocate a pseudo-TTY
  -u, --user string          Username or UID (format: <name|uid>[:<group|gid>])
  -w, --workdir string       Working directory inside the container

執行一次性命令

[root@ubuntu2004:~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
bf763ce9bd7a        nginx               "/docker-entrypoint.…"   10 minutes ago      Up 10 minutes       80/tcp              quirky_allen
4bfe2c25e29c        nginx               "/docker-entrypoint.…"   10 minutes ago      Up 10 minutes       80/tcp              peaceful_wilson

[root@ubuntu2004:~]# docker exec bf763ce9bd7a cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

進入容器

[root@ubuntu2004:~]# docker exec -it bf763ce9bd7a sh
# ls
bin   docker-entrypoint.d   home   media  proc	sbin  tmp
boot  docker-entrypoint.sh  lib    mnt	  root	srv   usr
dev   etc		    lib64  opt	  run	sys   var
# cat /etc/hosts    
127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
172.17.0.3	bf763ce9bd7a
# exit

# exit退出後容器繼續執行
[root@ubuntu2004:~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
bf763ce9bd7a        nginx               "/docker-entrypoint.…"   12 minutes ago      Up 12 minutes       80/tcp              quirky_allen
4bfe2c25e29c        nginx               "/docker-entrypoint.…"   12 minutes ago      Up 12 minutes       80/tcp              peaceful_wilson

docker logs命令

[root@ubuntu2004:~]# docker logs --help

Usage:	docker logs [OPTIONS] CONTAINER

Fetch the logs of a container

Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37) or
                       relative (e.g. 42m for 42 minutes)
      --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps
      --until string   Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or
                       relative (e.g. 42m for 42 minutes)

檢視容器web服務日誌

# 官方幫助文件web服務
[root@ubuntu2004:~]# docker run -it -d --name docs -p 4000:4000 docs/docker.github.io:latest
e646b05f701fc8dc595deef4ee4a76c5b2109a4086eeb52747fe7649089aa141

# 啟動日誌追蹤
[root@ubuntu2004:~]# docker logs -f docs
Docker docs are viewable at:
http://0.0.0.0:4000

# 訪問web服務 http://10.0.0.201:4000/
[root@ubuntu2004:~]# docker logs -f docs
Docker docs are viewable at:
http://0.0.0.0:4000
10.0.0.1 - - [19/Nov/2020:03:13:43 +0000] "GET / HTTP/1.1" 200 40519 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
10.0.0.1 - - [19/Nov/2020:03:13:44 +0000] "GET /css/font-awesome.min.css HTTP/1.1" 200 31000 "http://10.0.0.201:4000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
10.0.0.1 - - [19/Nov/2020:03:13:44 +0000] "GET /css/bootstrap.min.css HTTP/1.1" 200 121200 "http://10.0.0.201:4000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
10.0.0.1 - - [19/Nov/2020:03:13:44 +0000] "GET /css/pygments/perldoc.css HTTP/1.1" 200 3749 "http://10.0.0.201:4000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
10.0.0.1 - - [19/Nov/2020:03:13:44 +0000] "GET /css/style.css HTTP/1.1" 200 38777 "http://10.0.0.201:4000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
10.0.0.1 - - [19/Nov/2020:03:13:44 +0000] "GET /css/github.css HTTP/1.1" 200 1152 "http://10.0.0.201:4000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
10.0.0.1 - - [19/Nov/2020:03:13:44 +0000] "GET /js/bootstrap.min.js HTTP/1.1" 200 37045 "http://10.0.0.201:4000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
10.0.0.1 - - [19/Nov/2020:03:13:44 +0000] "GET /js/landing-page.js HTTP/1.1" 200 1322 "http://10.0.0.201:4000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
10.0.0.1 - - [19/Nov/2020:03:13:44 +0000] "GET /js/jquery.js HTTP/1.1" 200 95785 "http://10.0.0.201:4000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
10.0.0.1 - - [19/Nov/2020:03:13:44 +0000] "GET /js/search.js HTTP/1.1" 200 10352 "http://10.0.0.201:4000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
10.0.0.1 - - [19/Nov/2020:03:13:44 +0000] "GET /js/metadata.js HTTP/1.1" 200 109909 "http://10.0.0.201:4000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
10.0.0.1 - - [19/Nov/2020:03:13:45 +0000] "GET /images/docker-con-2020.svg HTTP/1.1" 200 20666 "http://10.0.0.201:4000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
10.0.0.1 - - [19/Nov/2020:03:13:45 +0000] "GET /images/docker-docs-logo.svg HTTP/1.1" 200 20499 "http://10.0.0.201:4000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
10.0.0.1 - - [19/Nov/2020:03:13:45 +0000] "GET /fonts/fontawesome-webfont.woff2?v=4.7.0 HTTP/1.1" 200 64464 "http://10.0.0.201:4000/css/font-awesome.min.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
10.0.0.1 - - [19/Nov/2020:03:13:45 +0000] "GET /fonts/geomanist/hinted-Geomanist-Regular.woff2 HTTP/1.1" 200 23884 "http://10.0.0.201:4000/css/style.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
10.0.0.1 - - [19/Nov/2020:03:13:45 +0000] "GET /fonts/geomanist/hinted-Geomanist-Book.woff2 HTTP/1.1" 200 23504 "http://10.0.0.201:4000/css/style.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
10.0.0.1 - - [19/Nov/2020:03:13:45 +0000] "GET /images/download-docker.svg HTTP/1.1" 200 4032 "http://10.0.0.201:4000/css/style.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
10.0.0.1 - - [19/Nov/2020:03:13:45 +0000] "GET /images/whats-new.svg HTTP/1.1" 200 751 "http://10.0.0.201:4000/css/style.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
10.0.0.1 - - [19/Nov/2020:03:13:45 +0000] "GET /images/manuals.svg HTTP/1.1" 200 688 "http://10.0.0.201:4000/css/style.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
10.0.0.1 - - [19/Nov/2020:03:13:45 +0000] "GET /images/reference.svg HTTP/1.1" 200 580 "http://10.0.0.201:4000/css/style.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
10.0.0.1 - - [19/Nov/2020:03:13:45 +0000] "GET /images/laptop.svg HTTP/1.1" 200 29077 "http://10.0.0.201:4000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
10.0.0.1 - - [19/Nov/2020:03:13:45 +0000] "GET /images/arrow.svg HTTP/1.1" 200 539 "http://10.0.0.201:4000/css/style.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
10.0.0.1 - - [19/Nov/2020:03:13:45 +0000] "GET /images/rocket.svg HTTP/1.1" 200 1362 "http://10.0.0.201:4000/css/style.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
10.0.0.1 - - [19/Nov/2020:03:13:45 +0000] "GET /images/guides.svg HTTP/1.1" 200 1046 "http://10.0.0.201:4000/css/style.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
10.0.0.1 - - [19/Nov/2020:03:13:45 +0000] "GET /favicons/[email protected] HTTP/1.1" 200 3542 "http://10.0.0.201:4000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"

docker cp命令

docker cp命令類似於scp命令,可以實現宿主機和容器之間的檔案互傳

[root@ubuntu2004:~]# docker cp --help

Usage:	docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
	docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

Copy files/folders between a container and the local filesystem

Use '-' as the source to read a tar archive from stdin
and extract it to a directory destination in a container.
Use '-' as the destination to stream a tar archive of a
container source to stdout.

Options:
  -a, --archive       Archive mode (copy all uid/gid information)
  -L, --follow-link   Always follow symbol link in SRC_PATH

容器中的檔案傳到宿主機

[root@ubuntu2004:~]# docker exec -it docs sh
/usr/share/nginx/html # ls
404.html                     docker-trusted-registry      mackit
50x.html                     docsarchive                  manuals
LICENSE                      edge                         network
README.md                    ee                           notary
apidocs                      en                           opensource
app                          engine                       project
articles                     enterprise                   redirects.json
buildx                       favicon.ico                  reference
cluster                      favicons                     registry
compliance                   fonts                        release-notes
components                   get-docker                   robots.txt
compose                      get-started                  samples
config                       getstarted                   search
contributing                 getting-started              security
cs-engine                    glossary                     sitemap.xml
css                          google161104f9fdea6089.html  storage
datacenter                   googlecbe7fee896be512c.html  swarm
desktop                      images                       test
develop                      index.html                   toolbox
docker-cloud                 install                      ucp
docker-ee-for-windows        installation                 userguide
docker-for-mac               introduction                 v17.06
docker-for-win               js                           v18.03
docker-for-windows           kitematic                    v18.09
docker-hub                   learn                        windows
docker-hub-enterprise        linux                        winkit
docker-id                    mac
docker-store                 machine
/usr/share/nginx/html # exit

# 從容器中下載404.html檔案
[root@ubuntu2004:~]# docker cp docs:/usr/share/nginx/html/404.html ./
[root@ubuntu2004:~]# ls
404.html  Dockerfile  env.list  init_for_centos_or_ubuntu.sh  snap

宿主機的檔案傳到容器中

[root@ubuntu2004:~]# docker cp Dockerfile docs:/root/
[root@ubuntu2004:~]# docker exec -it docs sh
/usr/share/nginx/html # cd
~ # ls
Dockerfile