docker常用命令

相关命令可以在官方文档查询:https://docs.docker.com/reference/

1、帮助命令

1
docker version # 显示docker的版本信息
1
docker info # 显示docker系统信息,包括镜像和容器的数量
1
docker --help # 帮助命令

2、镜像命令

查看镜像

1
2
3
4
docker images --help # 查看与镜像操作有关的帮助信息
docker images # 查看本地主机上的所有镜像
REPOSITORY(镜像仓库源) TAG(镜像的标签) IMAGE ID(镜像id) CREATED(镜像创建时间) SIZE(镜像的大小)
hello-world latest d1165f221234 6 weeks ago 13.3kB

查找镜像

1
2
3
docker search --help # 搜索镜像帮助信息
docker search # 搜索镜像
docker search mysql --filter=STARS=3000 # 过滤出STARS大于等于3000的mysql镜像

拉取镜像

1
2
3
docker pull # 下载镜像
docker pull mysql等价于docker pull docker.io/library/mysql:latest # 如果不写tag,默认就是latest
docker pull mysql:5.7 # 下载指定版本的mysql

删除镜像

1
2
docker rmi <IMAGE ID> # 删除指定镜像
docker rmi -f $(docker images -aq) # 删除全部镜像

3、容器命令

新建容器并启动

1
2
3
4
5
6
7
docker run <可选参数> image
可选参数说明如下:
--name # 设置容器名字
-d # 后台方式运行
-i -t # 使用交互方式进行,进入容器查看内容
-p # 指定容器的端口号(小写),有三种常用形式:①-p 主机端口:容器端口。②-p 容器端口。③-p ip:主机端口:容器端口。
-P # 随机指定端口(大写)

查看容器

1
2
3
4
docker ps # 查看正在运行的容器
docker ps -a # 查看所有容器
docker ps -a -n # 显示最近运行的容器的个数
docker ps -a -q # 只显示所有容器ID

退出容器

1
2
exit # 停止运行容器并退出
Ctrl+P+Q # 容器不停止退出

删除容器

1
2
docker rm <CONTAINER ID> # 删除指定容器
docker rm -f $(docker ps -aq) # 删除全部容器

启动和停止容器

1
2
3
4
docker start <CONTAINER ID> # 启动指定容器
docker restart <CONTAINER ID> # 重启指定容器
docker stop <CONTAINER ID> # 停止指定容器
docker kill <CONTAINER ID> # 强制停止指定容器

4、其它命令

查看日志

1
2
docker logs --help # 日志帮助命令
docker logs -t -f --tail <显示的日志条数> <CONTAINER ID> # 查看容器日志

查看容器进程信息

1
docker top <CONTAINER ID>

查看镜像的元数据

1
2
docker inspect --help # 查看元数据帮助命令
docker inspect <CONTAINER ID> # 查看指定容器的元数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
root@wocode-PC:/# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
82c3cb1a696a centos "/bin/sh -c 'while t…" 4 minutes ago Up 4 minutes intelligent_mclean
root@wocode-PC:/# docker inspect 82c3cb1a696a
[
{
"Id": "82c3cb1a696a75e98dc689e43ff273813c991fc15a767d7e268e40e9a399b4f7",
"Created": "2021-04-20T07:23:29.159620919Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo helloworld;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 21744,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-04-20T07:23:29.881676751Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/82c3cb1a696a75e98dc689e43ff273813c991fc15a767d7e268e40e9a399b4f7/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/82c3cb1a696a75e98dc689e43ff273813c991fc15a767d7e268e40e9a399b4f7/hostname",
"HostsPath": "/var/lib/docker/containers/82c3cb1a696a75e98dc689e43ff273813c991fc15a767d7e268e40e9a399b4f7/hosts",
"LogPath": "/var/lib/docker/containers/82c3cb1a696a75e98dc689e43ff273813c991fc15a767d7e268e40e9a399b4f7/82c3cb1a696a75e98dc689e43ff273813c991fc15a767d7e268e40e9a399b4f7-json.log",
"Name": "/intelligent_mclean",
"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,
"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/922966af51a52133caa8092c0f5125dbe22d7b64236258252e3107f9354ea983-init/diff:/var/lib/docker/overlay2/75b63e85e26f4c38b10cabee93c99e8a7001284b44947dc4624dc42b5a016673/diff",
"MergedDir": "/var/lib/docker/overlay2/922966af51a52133caa8092c0f5125dbe22d7b64236258252e3107f9354ea983/merged",
"UpperDir": "/var/lib/docker/overlay2/922966af51a52133caa8092c0f5125dbe22d7b64236258252e3107f9354ea983/diff",
"WorkDir": "/var/lib/docker/overlay2/922966af51a52133caa8092c0f5125dbe22d7b64236258252e3107f9354ea983/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "82c3cb1a696a",
"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 helloworld;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": "2911d2f9408dd540fb524c22408225456a9c279eb95f7812a13b1c472d543a57",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/2911d2f9408d",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "06892c4efe14c07b7e407b8acfa5c3461931cb841db5b9b8af74ce1ff5c8c982",
"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": "2a38abc2fb1f6abcac56f0377cc35d99857603db8b0bda0fa3e71b7a22b864b2",
"EndpointID": "06892c4efe14c07b7e407b8acfa5c3461931cb841db5b9b8af74ce1ff5c8c982",
"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
}
}
}
}
]

进入正在运行的容器

1
2
docker exec -i -t <CONTAINER ID> /bin/bash # 以交互方式进入正在运行的容器,并且会开启一个新的终端以便进行操作
docker attach <CONTAINER ID> # 进入容器正在运行的终端,不会启动新的进程

从容器中拷贝文件到主机

1
docker cp <CONTAINER ID>:容器内路径 主机路径

5、小结