概述
帮助命令
docker version # 显示 docker 的版本信息
docker info # 显示 docker 的详细信息
docker [命令] --help # 万能帮助命令
Docker 官网帮助文档:https://docs.docker.com/reference/
镜像命令
docker images 查看镜像信息
➜ ~ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 4 months ago 13.3kB
# 解释
REPOSITORY # 仓库中镜像的名称
TAG # 镜像的版本信息
IMAGE ID # 镜像的ID
CREATED # 创建时间
SIZE # 大小
# 可选项
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 image IDs
docker search 搜索镜像
➜ ~ docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11082 [OK]
mariadb MariaDB Server is a high performing open sou… 4199 [OK]
# 可选项
Options:
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print search using a Go template
--limit int Max number of search results (default 25)
--no-trunc Don't truncate output
# 过滤 mysql 收藏数大于 5000 的镜像
➜ ~ docker search mysql --filter=STARS=5000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11082 [OK]
docker pull 下载镜像
➜ ~ docker pull mysql
Using default tag: latest # 不指定版本,默认下载 latest 版本
latest: Pulling from library/mysql
b4d181a07f80: Pull complete # 分层下载,docker image 的核心,联合文件系统
a462b60610f5: Pull complete
578fafb77ab8: Pull complete
524046006037: Pull complete
d0cbe54c8855: Pull complete
aa18e05cc46d: Pull complete
32ca814c833f: Pull complete
9ecc8abdb7f5: Pull complete
ad042b682e0f: Pull complete
71d327c6bb78: Pull complete
165d1d10a3fa: Pull complete
2f40c47d0626: Pull complete
Digest: sha256:52b8406e4c32b8cf0557f1b74517e14c5393aff5cf0384eff62d9e81f4985d4b # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址
# docker pull mysql 等价于 docker pull docker.io/library/mysql:latest
# 指定版本下载
➜ ~ docker pull mysql:5.7
5.7: Pulling from library/mysql
b4d181a07f80: Already exists
a462b60610f5: Already exists
578fafb77ab8: Already exists
524046006037: Already exists
d0cbe54c8855: Already exists
aa18e05cc46d: Already exists
32ca814c833f: Already exists
52645b4af634: Pull complete
bca6a5b14385: Pull complete
309f36297c75: Pull complete
7d75cacde0f8: Pull complete
Digest: sha256:1a2f9cd257e75cc80e9118b303d1648366bc2049101449bf2c8d82b022ea86b7
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
docker rmi 删除镜像
➜ ~ docker images # 查询镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 09361feeb475 10 days ago 447MB
mysql latest 5c62e459e087 10 days ago 556MB
hello-world latest d1165f221234 4 months ago 13.3kB
➜ ~ docker rmi -f 09361feeb475 # 通过 IMAGE_ID 删除指定镜像
Untagged: mysql:5.7
Untagged: mysql@sha256:1a2f9cd257e75cc80e9118b303d1648366bc2049101449bf2c8d82b022ea86b7
Deleted: sha256:09361feeb4753ac9da80ead4d46e2b21247712c13c9ee3f1e5d55630c64c544f
Deleted: sha256:e454d1e47d2f346e0b2365c612cb6f12476ac4a3568ad5f62d96aa15bccf3e19
Deleted: sha256:e0457c6e331916c8ac6838ef4b22a6f62b21698facf4e143aa4b3863f08cf7d2
Deleted: sha256:ed73046ee2cd915c08ed37a545e1b89da70dc9bafeacfbd9fddff8f967373941
Deleted: sha256:419d7a76abf4ca51b81821da16a6c8ca6b59d02a0f95598a2605a1ed77c012eb
➜ ~ docker rmi -f $(docker images -aq) # 强制删除所有镜像
Untagged: mysql:latest
Untagged: mysql@sha256:52b8406e4c32b8cf0557f1b74517e14c5393aff5cf0384eff62d9e81f4985d4b
Deleted: sha256:5c62e459e087e3bd3d963092b58e50ae2af881076b43c29e38e2b5db253e0287
Deleted: sha256:b92a81bddd621ceee73e48583ed5c4f0d34392a5c60adf37c0d7acc98177e414
Deleted: sha256:265829a9fa8318ae1224f46ab7bc0a10d12ebb90d5f65d71701567f014685a9e
Deleted: sha256:2b9144b43d615572cb4a8fb486dfad0f78d1748241e49adab91f6072183644e9
Deleted: sha256:944ffc10a452573e587652116c3217cf571a32c45a031b79fed518524c21fd4f
Deleted: sha256:b9108f19e3abf550470778a9d91959ce812731d3268d7224e328b0f7d8a73d26
Deleted: sha256:9aecb80117a5517daf84c1743af298351a08e48fa04b8e99dcb63c817326a748
Deleted: sha256:d8773288899b1230986eba7486009df11d5dd6c628b1d4fd0443e873c6b00f70
Deleted: sha256:45a0a6bb39a4d7b37a6c598ae6af47f8a36ef63eaa9ef92d565137156aa36f54
Deleted: sha256:341f6b75346e72e9fa503aeb5362d1fe4f00449e02d3320e5c68f3052b7c2c13
Deleted: sha256:023f47f19f876ffa0225502a85b30954a44e54dc8223329fec32b336315c75c3
Deleted: sha256:058c443dffe18a5d2aad04cd5451a8540c7272ce9f8515d27e815303b1c25b59
Deleted: sha256:764055ebc9a7a290b64d17cf9ea550f1099c202d83795aa967428ebdf335c9f7
Untagged: hello-world:latest
Untagged: hello-world@sha256:9f6ad537c5132bcce57f7a0a20e317228d382c3cd61edae14650eec68b2b345c
Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726
容器命令
有了镜像才可以创建容器
docker pull 下载一个 CentOS 镜像用来学习
➜ ~ docker pull centos
Using default tag: latest
latest: Pulling from library/centos
7a0437f04f83: Pull complete
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
docker run 新建容器并启动
docker run [可选参数] image
# 参数说明
--name="Name" # 容器的名字 tomcat-01、tomcat-02,用来区分容器
-d # 后台方式运行
-it # 使用交互方式运行,进入容器查看内容
-p # 指定容器运行的端口 -p 8080:8080
# -p [主机端口]:[容器端口] 【常用端口指定方式】
# -p [IP]:[主机端口]:[容器端口]
# -p [主机端口]
# -p [容器端口]
-P # 随机指定端口
# ==============【测试】==============
➜ ~ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f 6 months ago 209MB
➜ ~ docker run -it centos /bin/bash # 启动并进入容器
[root@f29ef0e09229 /]# ls # 查看容器的的 CentOS
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@f29ef0e09229 /]# exit # 从容器中退回主机
exit
➜ ~ ls
Applications Documents Library Music Public resource temp workspace
Desktop Downloads Movies Pictures getting-started software tools zookeeper.out
docker ps 查看容器进程状态
➜ ~ docker ps -a # 查看所有运行过的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1119fc4a8561 hello-world "/hello" 17 seconds ago Exited (0) 16 seconds ago hardcore_brahmagupta
f29ef0e09229 centos "/bin/bash" 6 minutes ago Exited (0) 3 minutes ago goofy_northcutt
ebc82701c1a8 hello-world "/hello" 4 hours ago Exited (0) 4 hours ago condescending_banzai
➜ ~ docker ps -n=1 # 查看最近运行过的 n 个容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1119fc4a8561 hello-world "/hello" 3 minutes ago Exited (0) 3 minutes ago hardcore_brahmagupta
# 参数说明
-a # 查看所有运行过的容器,包括正在运行和曾经运行过的容器
-n # 查看最近运行过的 n 个容器
-q # 只显示容器的编号
退出容器
[root@f29ef0e09229 /]# exit # 退出并停止容器
CTRL + P + Q # 退出但不停止容器
docker rm 删除容器
docker rm [容器ID] # 删除指定容器,不能删除正在运行的容器
docker rm -f $(docker ps -aq) # 删除所有容器
docker ps -a -q|xargs docker rm # 删除所有容器
# ==============【测试】==============
➜ ~ docker ps -a # 查看所有的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a830fd363ddd centos "/bin/bash" 5 minutes ago Up 5 minutes condescending_chaum
1119fc4a8561 hello-world "/hello" 14 minutes ago Exited (0) 14 minutes ago hardcore_brahmagupta
f29ef0e09229 centos "/bin/bash" 20 minutes ago Exited (0) 17 minutes ago goofy_northcutt
ebc82701c1a8 hello-world "/hello" 4 hours ago Exited (0) 4 hours ago condescending_banzai
➜ ~ docker rm a830fd363ddd # 删除正在运行的容器,删除失败
Error response from daemon: You cannot remove a running container a830fd363dddabeb62f3a9fa9b046b5e803b7a98ad39e36b4845c7e7e0573250. Stop the container before attempting removal or force remove
➜ ~ docker rm 1119fc4a8561 # 通过容器ID删除指定容器
1119fc4a8561
➜ ~ docker ps -a # 查看删除结果
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a830fd363ddd centos "/bin/bash" 6 minutes ago Up 6 minutes condescending_chaum
f29ef0e09229 centos "/bin/bash" 21 minutes ago Exited (0) 17 minutes ago goofy_northcutt
ebc82701c1a8 hello-world "/hello" 4 hours ago Exited (0) 4 hours ago condescending_banzai
➜ ~ docker rm -f $(docker ps -aq) # 强制删除所有容器
a830fd363ddd
f29ef0e09229
ebc82701c1a8
➜ ~ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
启动 | 停止容器
docker start [容器ID] # 启动容器
docker restart [容器ID] # 重启容器
docker stop [容器ID] # 停止当前正在运行的容器
docker kill [容器ID] # 强制停止当前容器
常用的其他命令
后台启动容器
➜ ~ docker run -d centos
0d27e298e5a69994027041ebee3e26cba0f167eba2aee229874e51fbdef5f5be
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
问题
通过
docker run -d [镜像名称]
启动容器,再通过docker ps
查看发现容器停止了常见的坑:docker 容器使用后台运行,就必须有前台进程,docker 发现没有前台进程,就会自动停止容器
如 Nginx 容器启动后,发现自己没有提供服务,就会立刻停止
docker logs 查看日志
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6b3ae26a35f5 centos "/bin/bash" 4 minutes ago Up 4 minutes adoring_robinson
➜ ~ docker logs -f -t --tail 10 6b3ae26a35f5 # 查看对应容器的10行日志,但此时无日志
# 自己编写一段脚本生成日志
"while true;do echo hello neo; sleep 1;done"
➜ ~ docker run -d centos /bin/sh -c "while true;do echo hello neo; sleep 1;done"
b3edcecf1b666f63930bcf38b39bae21e2a6ae639855cc3d20f7dcecee1e5e93
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b3edcecf1b66 centos "/bin/sh -c 'while t…" 33 seconds ago Up 32 seconds elated_bohr
➜ ~ docker logs -f -t --tail 10 b3edcecf1b66 # 显示日志
2021-07-04T07:31:59.610787700Z hello neo
2021-07-04T07:32:00.583336300Z hello neo
2021-07-04T07:32:01.593195900Z hello neo
2021-07-04T07:32:02.601397500Z hello neo
# 参数说明
-f # 跟踪日志输出
-t # 带时间戳格式的日志
--tail # 显示日志的行数
docker top 查看容器中的进程
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b3edcecf1b66 centos "/bin/sh -c 'while t…" 6 minutes ago Up 6 minutes elated_bohr
➜ ~ docker top b3edcecf1b66
UID PID PPID C STIME TTY TIME CMD
root 4565 4537 0 07:31 ? 00:00:00 /bin/sh -c while true;do echo hello neo; sleep 1;done
root 5022 4565 0 07:38 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
docker inspect 查看容器元数据
➜ ~ docker inspect b3edcecf1b66
[
{
"Id": "b3edcecf1b666f63930bcf38b39bae21e2a6ae639855cc3d20f7dcecee1e5e93",
"Created": "2021-07-04T07:31:20.9968824Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo hello neo; sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 4565,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-07-04T07:31:21.2811662Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/b3edcecf1b666f63930bcf38b39bae21e2a6ae639855cc3d20f7dcecee1e5e93/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/b3edcecf1b666f63930bcf38b39bae21e2a6ae639855cc3d20f7dcecee1e5e93/hostname",
"HostsPath": "/var/lib/docker/containers/b3edcecf1b666f63930bcf38b39bae21e2a6ae639855cc3d20f7dcecee1e5e93/hosts",
"LogPath": "/var/lib/docker/containers/b3edcecf1b666f63930bcf38b39bae21e2a6ae639855cc3d20f7dcecee1e5e93/b3edcecf1b666f63930bcf38b39bae21e2a6ae639855cc3d20f7dcecee1e5e93-json.log",
"Name": "/elated_bohr",
"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,
"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",
"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/b8bddff483e4b62d2b851fd02bf258bc75515ffad44d16db1d16334e135927eb-init/diff:/var/lib/docker/overlay2/8719b5cb2d63dcc4bc474e47dad054ec2464537d3d5355a5e069c0aee6417f6d/diff",
"MergedDir": "/var/lib/docker/overlay2/b8bddff483e4b62d2b851fd02bf258bc75515ffad44d16db1d16334e135927eb/merged",
"UpperDir": "/var/lib/docker/overlay2/b8bddff483e4b62d2b851fd02bf258bc75515ffad44d16db1d16334e135927eb/diff",
"WorkDir": "/var/lib/docker/overlay2/b8bddff483e4b62d2b851fd02bf258bc75515ffad44d16db1d16334e135927eb/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "b3edcecf1b66",
"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",
"while true;do echo hello neo; sleep 1;done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20201204",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "2bc187efad150a253b923df790e3255b0d2aa7b721a0b7f36f54fd5971ae809c",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/2bc187efad15",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "b6a3ea10e0846f09f1198ae306eafdd43526f7fd2a03669e0933b690e77ee21e",
"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": "9a58d67faa2fbf7cef89e52588520739d5299c5d77bcd655c0d5301a80af814a",
"EndpointID": "b6a3ea10e0846f09f1198ae306eafdd43526f7fd2a03669e0933b690e77ee21e",
"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 【常用】
# 命令
docker exec -it [容器ID] bashShell
# ==============【测试】==============
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b3edcecf1b66 centos "/bin/sh -c 'while t…" 16 minutes ago Up 16 minutes elated_bohr
➜ ~ docker exec -it b3edcecf1b66 /bin/bash # 进入容器
[root@b3edcecf1b66 /]#
方式二:docker attach
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b3edcecf1b66 centos "/bin/sh -c 'while t…" 16 minutes ago Up 16 minutes elated_bohr
➜ ~ docker attach b3edcecf1b66
两种方式的差异
docker exec
:进入容器后开启一个新的终端,可以在里面操作docker attach
:进入容器正在执行的终端,不会启动新的进程
docker cp 文件拷贝
将容器中的文件拷贝到宿主机
# 命令
docker cp [容器ID]:[容器文件路径] [宿主机存储路径]
➜ ~ docker run -it centos /bin/bash # 启动容器
[root@a9535bc3f853 /]# cd /home # 进入 home 目录
[root@a9535bc3f853 home]# touch Test.java # 创建 Test.java 文件
[root@a9535bc3f853 home]# ls
Test.java
[root@a9535bc3f853 home]# exit # 退出容器
exit
➜ ~ docker ps -a # 查看容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a9535bc3f853 centos "/bin/bash" 2 minutes ago Exited (0) 32 seconds ago condescending_fermi
➜ ~ docker cp a9535bc3f853:/home/Test.java /Users/neoyu/Downloads #将容器中的文件拷贝的宿主机
➜ ~ ls /Users/neoyu/Downloads # 查看考本结果
Test.java
拷贝是一个手动过程,后面可以使用
-v
卷技术,可以实现自动同步
总结
最后
以上就是自由夕阳为你收集整理的Docker常用命令的全部内容,希望文章能够帮你解决Docker常用命令所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复