Docker是一个容器化系统,它打包并运行应用程序及其在容器内的依赖项。在使用 Docker 时,您必须了解几个 Docker 命令。这篇文章就是关于这一点的。
查看版本
您想知道的第一件事就是如何查看已安装的 Docker 版本。
# docker version
Client: Docker Engine - Community
Version: 20.10.11
API version: 1.41
Go version: go1.16.9
Git commit: dea9396
Built: Thu Nov 18 00:38:53 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.11
API version: 1.41 (minimum version 1.12)
Go version: go1.16.9
Git commit: 847da18
Built: Thu Nov 18 00:37:17 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.12
GitCommit: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
runc:
Version: 1.0.2
GitCommit: v1.0.2-0-g52b36a2
docker-init:
Version: 0.19.0
GitCommit: de40ad0
下载镜像
假设您需要从 dockerhub(docker 存储库)中提取docker映像。以下拉取 Apache HTTP 服务器映像的示例。
# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
eff15d958d66: Already exists
ba1caf8ba86c: Pull complete
ab86dc02235d: Pull complete
0d58b11d2867: Pull complete
e88da7cb925c: Pull complete
Digest: sha256:1d71eef54c08435c0be99877c408637f03112dc9f929fba3cccdd15896099b02
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest
查看镜像
列出在系统上拉取的所有 Docker 映像,其中包含映像详细信息,如 TAG/映像 ID/SIZE 等。
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
man-centos latest 5d9317c28b32 11 hours ago 275MB
rabbitmq latest c10b0f4fd126 8 days ago 220MB
openjdk latest 1b3756d6df61 12 days ago 471MB
httpd latest ad17c88403e2 12 days ago 143MB
redis latest 40c68ed3a4d2 2 weeks ago 113MB
postgres latest 577410342f45 2 weeks ago 374MB
nginx latest ea335eea17ab 2 weeks ago 141MB
mysql latest b05128b000dd 2 weeks ago 516MB
ubuntu latest ba6acccedd29 6 weeks ago 72.8MB
centos latest 5d0da3dc9764 2 months ago 231MB
percona/percona-xtrabackup latest 92f4f26039ff 3 months ago 419MB
percona/percona-server latest 8715c0443d34 13 months ago 749MB
运行
运行命令中提到的 docker 映像。此命令将创建一个 Docker 容器,Apache HTTP 服务器将在其中运行。
# docker run -it -d httpd
142ed1a73f859caf6f8947d98d8869747f5e58806c2d958ed0c8601042a4ab2f
正在运行什么?
ps
列出所有 docker 容器都使用容器详细信息运行。
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
142ed1a73f85 httpd "httpd-foreground" 33 seconds ago Up 32 seconds 80/tcp romantic_perlman
6391c1a0cac6 ubuntu "bash" 12 hours ago Up 20 minutes container-2
如您所见,Apache 服务器正在此 docker 容器中运行。
ps -a
列出所有正在运行/退出/停止的 Docker 容器,并提供容器详细信息。
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
142ed1a73f85 httpd "httpd-foreground" About a minute ago Up About a minute 80/tcp romantic_perlman
6391c1a0cac6 ubuntu "bash" 12 hours ago Up 21 minutes container-2
6a81a998658e ubuntu "bash" 12 hours ago Exited (0) 21 minutes ago container-1
3d151dc0651f percona/percona-xtrabackup "xtrabackup --backup…" 39 hours ago Exited (0) 39 hours ago percona-xtrabackup
4acfff0e1c89 percona/percona-server "/docker-entrypoint.…" 39 hours ago Exited (0) 38 hours ago percona-server-mysql
57b42fc7893c centos "bash" 41 hours ago Exited (0) 10 hours ago objective_hypatia
dfb9c121f722 centos "bash" 41 hours ago Exited (0) 41 hours ago eager_lamport
13696283ff0e centos "bash" 41 hours ago Exited (0) 41 hours ago affectionate_cray
3897010615c6 centos "echo Hello-World" 41 hours ago Exited (0) 41 hours ago happy_hofstadter
bb08674fca16 centos "/bin/bash" 41 hours ago Exited (0) 41 hours ago goofy_khayyam
4eeba8781795 centos "/bin/bash" 41 hours ago Exited (0) 41 hours ago confident_faraday
1b9c712a59ff postgres "docker-entrypoint.s…" 42 hours ago Exited (0) 41 hours ago postgres13
427bf832329e rabbitmq "docker-entrypoint.s…" 42 hours ago Exited (0) 41 hours ago busy_napier
6d5cae8593f0 redis "docker-entrypoint.s…" 42 hours ago Exited (0) 41 hours ago gallant_wilbur
83f190377bd9 openjdk "jshell" 42 hours ago Exited (0) 42 hours ago naughty_bardeen
94df620c9d43 nginx "/docker-entrypoint.…" 42 hours ago Exited (0) 41 hours ago dreamy_villani
fefe2df2c771 mysql "docker-entrypoint.s…" 4 days ago Exited (0) 4 days ago mysql-server
5bded3972894 centos "echo Hello-World" 8 days ago Exited (0) 8 days ago loving_almeida
执行命令
访问 docker 容器并在容器内运行命令。我正在访问此示例中的 apache 服务器容器。
# docker exec -it 142ed1a73f85 bash
root@142ed1a73f85:/usr/local/apache2# ls
bin build cgi-bin conf error htdocs icons include logs modules
键入exit并按 Enter 键以从容器中出来。
删除容器
删除命令中提到的容器 ID 的 docker 容器。
# docker rm 6a81a998658e
6a81a998658e
运行以下命令以检查容器是否已删除。
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
142ed1a73f85 httpd "httpd-foreground" 3 minutes ago Up 3 minutes 80/tcp romantic_perlman
6391c1a0cac6 ubuntu "bash" 12 hours ago Up 23 minutes container-2
3d151dc0651f percona/percona-xtrabackup "xtrabackup --backup…" 39 hours ago Exited (0) 39 hours ago percona-xtrabackup
4acfff0e1c89 percona/percona-server "/docker-entrypoint.…" 39 hours ago Exited (0) 39 hours ago percona-server-mysql
57b42fc7893c centos "bash" 41 hours ago Exited (0) 10 hours ago objective_hypatia
dfb9c121f722 centos "bash" 41 hours ago Exited (0) 41 hours ago eager_lamport
13696283ff0e centos "bash" 41 hours ago Exited (0) 41 hours ago affectionate_cray
3897010615c6 centos "echo Hello-World" 41 hours ago Exited (0) 41 hours ago happy_hofstadter
bb08674fca16 centos "/bin/bash" 41 hours ago Exited (0) 41 hours ago goofy_khayyam
4eeba8781795 centos "/bin/bash" 41 hours ago Exited (0) 41 hours ago confident_faraday
1b9c712a59ff postgres "docker-entrypoint.s…" 42 hours ago Exited (0) 41 hours ago postgres13
427bf832329e rabbitmq "docker-entrypoint.s…" 42 hours ago Exited (0) 41 hours ago busy_napier
6d5cae8593f0 redis "docker-entrypoint.s…" 42 hours ago Exited (0) 41 hours ago gallant_wilbur
83f190377bd9 openjdk "jshell" 42 hours ago Exited (0) 42 hours ago naughty_bardeen
94df620c9d43 nginx "/docker-entrypoint.…" 43 hours ago Exited (0) 41 hours ago dreamy_villani
fefe2df2c771 mysql "docker-entrypoint.s…" 4 days ago Exited (0) 4 days ago mysql-server
5bded3972894 centos "echo Hello-World" 8 days ago Exited (0) 8 days ago loving_almeida
删除镜像
删除命令中提到的镜像ID 的 docker 镜像
# docker rmi 5d9317c28b32
Untagged: man-centos:latest
Deleted: sha256:5d9317c28b32537589e96f08fd4db782e97e15c0db10226cd9cd9b9b957ad03d
Deleted: sha256:ba4535dc23788f4b3c4ee6ad14cb122f939232640a686dd216a12f6d2ff881f2
重启动容器
重启动命令中提到的容器ID 的 docker 容器
# docker restart romantic_perlman
romantic_perlman
运行以下命令并检查 STATUS 参数以验证容器最近是否启动。
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
142ed1a73f85 httpd "httpd-foreground" 7 minutes ago Up 24 seconds 80/tcp romantic_perlman
6391c1a0cac6 ubuntu "bash" 12 hours ago Up 28 minutes container-2
停止容器
停止具有命令中提到的容器 ID 的容器。
# docker stop romantic_perlman
romantic_perlman
运行以下命令以检查容器是否仍在运行或已停止。
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6391c1a0cac6 ubuntu "bash" 12 hours ago Up 29 minutes container-2
启动容器
Docker 中的此命令将使用命令中提到的容器 ID 启动 docker 容器。
# docker start romantic_perlman
romantic_perlman
运行以下命令以检查容器是否已启动。
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
142ed1a73f85 httpd "httpd-foreground" 9 minutes ago Up 18 seconds 80/tcp romantic_perlman
6391c1a0cac6 ubuntu "bash" 12 hours ago Up 30 minutes container-2
终止容器
立即停止 Docker 容器。Docker 停止命令可以正常地停止容器,这就是终止命令和停止命令之间的区别。
# docker kill romantic_perlman
romantic_perlman
运行以下命令以查看容器是否被杀死。
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6391c1a0cac6 ubuntu "bash" 12 hours ago Up 31 minutes container-2
保存容器为新镜像
使用本地系统上命令中提到的容器 ID 保存新的 docker 映像。在下面的示例中,myhttpd是图像名称。
# docker commit romantic_perlman myhttpd
sha256:ba3d17832e622b0f2f4df8979e70865a12335c9f7247419e019821b712f7f580
登录
登录到 Docker 中心。系统将要求您提供 Docker 中心凭据以进行登录。
# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: test
Password:
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
上传镜像
上传具有 dockerhub 上命令中提到的映像名称的 docker 映像。
# docker push myhttpd
The push refers to repository [docker.io/geekflare/httpd_image]
734d9104a6a2: Pushed
635721fc6973: Mounted from library/httpd
bea448567d6c: Mounted from library/httpd
bfaa5f9c3b51: Mounted from library/httpd
9d542ac296cc: Mounted from library/httpd
d8a33133e477: Mounted from library/httpd
latest: digest: sha256:3904662761df9d76ef04ddfa5cfab764b85e3eedaf10071cfbe2bf77254679ac size: 1574
网络
Docker 中的以下命令列出了群集中所有网络的详细信息。
# docker network ls
NETWORK ID NAME DRIVER SCOPE
94ea94e342cd bridge bridge local
6c276fc42752 host host local
12ac64f4da47 none null local
还有其他几个 docker 网络命令。
# docker network
Usage: docker network COMMAND
Manage networks
Commands:
connect Connect a container to a network
create Create a network
disconnect Disconnect a container from a network
inspect Display detailed information on one or more networks
ls List networks
prune Remove all unused networks
rm Remove one or more networks
Run 'docker network COMMAND --help' for more information on a command.
Docker 信息
获取有关系统上安装的 Docker 的详细信息,包括内核版本、容器和映像的数量等。
# docker info
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Build with BuildKit (Docker Inc., v0.6.3-docker)
scan: Docker Scan (Docker Inc., v0.9.0)
Server:
Containers: 17
Running: 1
Paused: 0
Stopped: 16
Images: 12
Server Version: 20.10.11
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
runc version: v1.0.2-0-g52b36a2
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-1160.45.1.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.701GiB
Name: docker
ID: CZKM:TNG7:T3B4:7FIF:UG5O:AUC4:JUKN:EONE:SLGG:L7IW:P6WZ:KWI3
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://docker.mirrors.ustc.edu.cn/
Live Restore Enabled: false
复制文件
将文件从 Docker 容器复制到本地系统。
在这个例子中,我将id为09ca6feb6efc的docker容器内的httpd.pid文件复制到/home/test/
# docker cp 09ca6feb6efc:/usr/local/apache2/logs/httpd.pid /home/test/
检查历史记录
显示具有命令中提到的映像名称的 docker 映像的历史记录。
# docker history httpd
IMAGE CREATED CREATED BY SIZE COMMENT
ad17c88403e2 12 days ago /bin/sh -c #(nop) CMD ["httpd-foreground"] 0B
<missing> 12 days ago /bin/sh -c #(nop) EXPOSE 80 0B
<missing> 12 days ago /bin/sh -c #(nop) COPY file:c432ff61c4993ecd… 138B
<missing> 12 days ago /bin/sh -c #(nop) STOPSIGNAL SIGWINCH 0B
<missing> 12 days ago /bin/sh -c set -eux; savedAptMark="$(apt-m… 60.5MB
<missing> 2 weeks ago /bin/sh -c #(nop) ENV HTTPD_PATCHES= 0B
<missing> 2 weeks ago /bin/sh -c #(nop) ENV HTTPD_SHA256=20e01d81… 0B
<missing> 2 weeks ago /bin/sh -c #(nop) ENV HTTPD_VERSION=2.4.51 0B
<missing> 2 weeks ago /bin/sh -c set -eux; apt-get update; apt-g… 2.6MB
<missing> 2 weeks ago /bin/sh -c #(nop) WORKDIR /usr/local/apache2 0B
<missing> 2 weeks ago /bin/sh -c mkdir -p "$HTTPD_PREFIX" && chow… 0B
<missing> 2 weeks ago /bin/sh -c #(nop) ENV PATH=/usr/local/apach… 0B
<missing> 2 weeks ago /bin/sh -c #(nop) ENV HTTPD_PREFIX=/usr/loc… 0B
<missing> 2 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 2 weeks ago /bin/sh -c #(nop) ADD file:a2405ebb9892d98be… 80.4MB
检查日志
显示命令中提到的包含 ID 的 docker 容器的日志。
# docker logs romantic_perlman
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
[Wed Dec 01 23:45:53.672497 2021] [mpm_event:notice] [pid 1:tid 139728892104000] AH00489: Apache/2.4.51 (Unix) configured -- resuming normal operations
[Wed Dec 01 23:45:53.681941 2021] [core:notice] [pid 1:tid 139728892104000] AH00094: Command line: 'httpd -D FOREGROUND'
[Wed Dec 01 23:53:22.335917 2021] [mpm_event:notice] [pid 1:tid 139728892104000] AH00492: caught SIGWINCH, shutting down gracefully
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
[Wed Dec 01 23:53:23.839929 2021] [mpm_event:notice] [pid 1:tid 140449118231872] AH00489: Apache/2.4.51 (Unix) configured -- resuming normal operations
[Wed Dec 01 23:53:23.840114 2021] [core:notice] [pid 1:tid 140449118231872] AH00094: Command line: 'httpd -D FOREGROUND'
[Wed Dec 01 23:54:36.147407 2021] [mpm_event:notice] [pid 1:tid 140449118231872] AH00492: caught SIGWINCH, shutting down gracefully
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
[Wed Dec 01 23:55:22.435968 2021] [mpm_event:notice] [pid 1:tid 140376060620096] AH00489: Apache/2.4.51 (Unix) configured -- resuming normal operations
[Wed Dec 01 23:55:22.436145 2021] [core:notice] [pid 1:tid 140376060620096] AH00094: Command line: 'httpd -D FOREGROUND'
搜索镜像
在 dockerhub 上搜索具有命令中提到的名称的 docker 映像。
# docker search hadoop
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
sequenceiq/hadoop-docker An easy way to try Hadoop 665 [OK]
uhopper/hadoop Base Hadoop image with dynamic configuration… 102 [OK]
harisekhon/hadoop Apache Hadoop (HDFS + Yarn, tags 2.2 - 2.8) 68 [OK]
bde2020/hadoop-namenode Hadoop namenode of a hadoop cluster 54 [OK]
bde2020/hadoop-datanode Hadoop datanode of a hadoop cluster 44 [OK]
bde2020/hadoop-base Base image to create hadoop cluster. 22 [OK]
izone/hadoop Hadoop 2.8.5 Ecosystem fully distributed, Ju… 17 [OK]
apache/hadoop Apache Hadoop convenience builds 15 [OK]
bde2020/hadoop-nodemanager Hadoop node manager docker image. 11 [OK]
bde2020/hadoop-resourcemanager Hadoop resource manager docker image. 10 [OK]
uhopper/hadoop-namenode Hadoop namenode 10 [OK]
uhopper/hadoop-datanode Hadoop datanode 8 [OK]
singularities/hadoop Apache Hadoop 8 [OK]
harisekhon/hadoop-dev Apache Hadoop (HDFS + Yarn) + Dev Tools + Gi… 7 [OK]
bde2020/hadoop-historyserver Hadoop history server docker image. 7 [OK]
anchorfree/hadoop-slave Hadoop slave image 5
portworx/hadoop-namenode Hadoop in Docker with Networking and Persist… 4 [OK]
flokkr/hadoop-hdfs-datanode Container for HDFS datanode of Apache Hadoop… 4
uhopper/hadoop-resourcemanager Hadoop resourcemanager 3 [OK]
portworx/hadoop-yarn Hadoop in Docker with Networking and Persist… 2 [OK]
newnius/hadoop Setup a Hadoop cluster with docker. 1 [OK]
openpai/hadoop-run 1
danisla/hadoop Hadoop image built with native libraries 1
flokkr/hadoop-yarn-nodemanager Container for YARN nodemanager of Apache Had… 1
flokkr/hadoop Apache Hadoop container with advanced config… 0
更新配置
更新容器配置。这将显示所有更新选项。
# docker update --help
Usage: docker update [OPTIONS] CONTAINER [CONTAINER...]
Update configuration of one or more containers
Options:
--blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--cpu-period int Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota
--cpu-rt-period int Limit the CPU real-time period in microseconds
--cpu-rt-runtime int Limit the CPU real-time runtime in microseconds
-c, --cpu-shares int CPU shares (relative weight)
--cpus decimal Number of CPUs
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
--kernel-memory bytes Kernel memory limit
-m, --memory bytes Memory limit
--memory-reservation bytes Memory soft limit
--memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--pids-limit int Tune container pids limit (set -1 for unlimited)
--restart string Restart policy to apply when a container exits
运行以下命令,使用命令中提到的容器 ID 更新 docker 容器的 CPU 配置。
# docker update -c 1 2f6fb3381078
创建卷
创建一个卷,Docker 容器将使用该卷来存储数据。
# docker volume create myvol
myvol
如果卷已创建,请运行以下命令。
# docker volume ls
DRIVER VOLUME NAME
local 8b00b51567b482016a45cdbb66fa463fd0f6204ca46ab2a975a3643fd21b8f23
local 8c857f623a2ccebbf498d99bc0ca312a225b2280b656c3cc24c2ed300056bfbb
local 17c69b702383401daa644c015333702e4de694d1b8aa4ecaca83f9b60f4afc30
local 97e29c57e8b19b2b560010e8fd697f77b2c3de7a6c43bc202bb12979a6f7a634
local 744b3fb48340c1196895238977205cce5f88d5b5022ba2e4aa89f22b270d8ea5
local 83812d58e35310600f4310504451d9e7c1425b17ce794c3dfc8c49cb4b5841b8
local f14bd7078ad64de5a7b79ad2b9388ce846c234db09f67eba5136ab1aa24805a5
local fa8e91f33a2c427d498690d41aea915542d91e9a9fc6c5f2ece4c3a2c46f85d5
local mysql-data
local myvol
local postgres-volume
安装插件
安装一个 docker 插件 vieux/sshfs,调试环境设置为 1。
# docker plugin install vieux/sshfs DEBUG=1
Plugin "vieux/sshfs" is requesting the following privileges:
- network: [host]
- mount: [/var/lib/docker/plugins/]
- mount: []
- device: [/dev/fuse]
- capabilities: [CAP_SYS_ADMIN]
Do you grant the above permissions? [y/N] y
latest: Pulling from vieux/sshfs
Digest: sha256:1d3c3e42c12138da5ef7873b97f7f32cf99fb6edde75fa4f0bcf9ed277855811
52d435ada6a4: Complete
Installed plugin vieux/sshfs
注销
从 dockerhub 注销。
# docker logout
Removing login credentials for https://index.docker.io/v1/
结论
我希望您现在已经对 Docker 命令有了一个基本的理解。在开发或实验室环境中试用这些命令以进行练习和学习。