docker-001

docker 记录

docker 启动容器、进入容器、删除容器等

在生产环境中,为了提高容器的高可用性和安全性,一方面要合理使用资源限制参数来管理容器的资源消耗;另一方面要指定合适的容器重启策略,来自动重启退出的容器。

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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
➜  Downloads docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
19d7ad3d29cb centos "/bin/bash" 14 hours ago Exited (0) 13 hours ago charming_engelbart

# 启动容器
➜ Downloads docker start 19d7ad3d29cb
19d7ad3d29cb

# 进入容器
➜ Downloads docker attach 19d

# 删除容器
➜ Downloads docker rm 0509
Error response from daemon: You cannot remove a running container 0509d2e0e68d29c4b60bf00d7e444cd5f10e000c6db8e0c69e6472b7eb31059c. Stop the container before attempting removal or force remove

# 强制删除容器
➜ Downloads docker rm -f 0509
0509

# 容器导出
big-data docker export -o ./centos_1-19.tar 19d

# 查看容器信息等
➜ big-data docker container inspect 19d
[
{
"Id": "19d7ad3d29cbeaa490759891fd6b24c3b76f72e2e2ab0b4c99b826e69b7bbd39",
"Created": "2021-01-18T12:30:00.994571758Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 1693,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-01-19T02:05:17.487554903Z",
"FinishedAt": "2021-01-18T12:52:17.688931234Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/19d7ad3d29cbeaa490759891fd6b24c3b76f72e2e2ab0b4c99b826e69b7bbd39/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/19d7ad3d29cbeaa490759891fd6b24c3b76f72e2e2ab0b4c99b826e69b7bbd39/hostname",
"HostsPath": "/var/lib/docker/containers/19d7ad3d29cbeaa490759891fd6b24c3b76f72e2e2ab0b4c99b826e69b7bbd39/hosts",
"LogPath": "/var/lib/docker/containers/19d7ad3d29cbeaa490759891fd6b24c3b76f72e2e2ab0b4c99b826e69b7bbd39/19d7ad3d29cbeaa490759891fd6b24c3b76f72e2e2ab0b4c99b826e69b7bbd39-json.log",
"Name": "/charming_engelbart",
"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,
"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/1399c30f19a036d8a981e9c2501b4bfb8f5dc0d4ad17b9f7524aa4e6c4f822e9-init/diff:/var/lib/docker/overlay2/ac2768f1f10cdd6a3a96d526fdba25dd6a50dc8adb9dc9be6e869b2f6d98b726/diff",
"MergedDir": "/var/lib/docker/overlay2/1399c30f19a036d8a981e9c2501b4bfb8f5dc0d4ad17b9f7524aa4e6c4f822e9/merged",
"UpperDir": "/var/lib/docker/overlay2/1399c30f19a036d8a981e9c2501b4bfb8f5dc0d4ad17b9f7524aa4e6c4f822e9/diff",
"WorkDir": "/var/lib/docker/overlay2/1399c30f19a036d8a981e9c2501b4bfb8f5dc0d4ad17b9f7524aa4e6c4f822e9/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "19d7ad3d29cb",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"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": "4730e0d0e8643e8e546ac952ff06337eb6a5e56960659eeb990e032920adcbaf",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/4730e0d0e864",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "65d9cb246361c71aa16c3a4857280110f494fc8d6a68e7dd5a628e83f60c4f19",
"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": "7f0b4f22bbcd47519c542b9b25d362d0c7e882537638dfd0d55a6fc4900e85c3",
"EndpointID": "65d9cb246361c71aa16c3a4857280110f494fc8d6a68e7dd5a628e83f60c4f19",
"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
}
}
}
}
]

# 查看容器内进程
➜ big-data docker top 19d
PID USER TIME COMMAND
1693 root 0:00 /bin/bash

docker run -v参数

格式:-v 本地目录:容器目录 或 -v 容器目录
示例:docker run -it –rm -v $PWD(本地目录):/python(容器目录) python /bin/bash
将本机的当前目录,挂载到镜像中的/python目录
docker run –rm -it -v /home/hyzhou/docker(本地目录):/data(容器目录) ubuntu:14.04 /bin/bash
将本机的/home/hyzhou/docker,挂载到镜像中的/data目录
其他参数:
  1. -d: 后台运行容器
  1. -p: 端口映射 5005:5004  指定主机的5005端口映射到容器的5004端口
  1. -it: 以交互模式运行启动容器