Docker命令

底层原理

在说Docker命令前先简要说一下它的原理

Docker是怎么工作的?

Docker是一个Client-Server结构的系统,Docker的守护进行运行在主机上。通过Socket从客户端访问。

DockerServer接收到Docker-Client的指令,就会执行这个命令

Docker为什么比VM快

  1. Docker有着比虚拟机更少的抽象层
  2. Docker利用的是宿主机的内核,vm需要的是Guest OS

新建一个容器的时候,docker不需要像虚拟机一样重新加载一个操作系统内核,避免引导。虚拟机是加载Guest OS,分钟级别的,而docker是利用宿主机的操作系统,省略了这个复杂的过程,秒级。

帮助命令

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

帮助文档地址:Reference documentation | Docker Documentation

镜像命令

docker images

查看所有本地主机上的镜像

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
d3ic1de@d3ic1de-virtual-machine:~/Desktop/lnmp$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
fbraz3/lnmp latest b1305ced5bb0 8 days ago 917MB
hello-world latest 9c7a54a9a43c 2 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 Format output using a custom template:
'table': Print output in table format
with column headers (default)
'table TEMPLATE': Print output in table format
using the given Go template
'json': Print in JSON format
'TEMPLATE': Print output using the given
Go template.
Refer to https://docs.docker.com/go/formatting/
for more information about formatting output with
templates
--no-trunc Don't truncate output
-q, --quiet Only show image IDs # 只显示镜像的id

搜索镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
d3ic1de@d3ic1de-virtual-machine:~/Desktop/lnmp$ docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 14278 [OK]
mariadb MariaDB Server is a high performing open sou… 5455 [OK]
percona Percona Server is a fork of the MySQL relati… 617 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 832 [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
--no-trunc Don't truncate output
# 通过搜藏来过滤
--filter=STARS=3000 # 搜索出来的镜像就是STARS大于3000的

d3ic1de@d3ic1de-virtual-machine:~/Desktop/lnmp$ docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 14278 [OK]
mariadb MariaDB Server is a high performing open sou… 5455 [OK]

docker pull 下载镜像

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
# 下载镜像 docker pull 镜像名[:tag]
d3ic1de@d3ic1de-virtual-machine:~/Desktop/mysql$ docker pull mysql
Using default tag: latest # 如果不写版本(tag)# ,默认就是 latest
latest: Pulling from library/mysql
e2c03c89dcad: Pull complete # 分层下载,docker image的核心 联合文件系统
68eb43837bf8: Pull complete
796892ddf5ac: Pull complete
6bca45eb31e1: Pull complete
ebb53bc0dcca: Pull complete
2e2c6bdc7a40: Pull complete
6f27b5c76970: Pull complete
438533a24810: Pull complete
e5bdf19985e0: Pull complete
667fa148337b: Pull complete
5baa702110e4: Pull complete
Digest: sha256:232936eb036d444045da2b87a90d48241c60b68b376caf509051cb6cffea6fdc
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址

# 等价
docker pull mysql
docker pull docker.io/library/mysql:latest

# 指定版本下载
d3ic1de@d3ic1de-virtual-machine:~/Desktop/mysql_8.0.33$ docker pull mysql:8.0.33
8.0.33: Pulling from library/mysql #因为最新版也是8.0.33所以也会从这个库下载,当下载别的版本在分层下载时,已有的层就不会重复下载
Digest: sha256:232936eb036d444045da2b87a90d48241c60b68b376caf509051cb6cffea6fdc
Status: Downloaded newer image for mysql:8.0.33
docker.io/library/mysql:8.0.33

docker rmi 删除

1
2
3
docker rmi -f 镜像id  # 删除指定的容器
docker rmi -f 镜像id 镜像id 镜像id 镜像id... # 删除多个容器
docker rmi -f $(docker images -aq) # 删除全部的容器

容器命令

有了镜像才可以创建容器,我就在我的ubuntu上整一个centos镜像来测试(套娃(x)

1
docker pull centos

新建容器并启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
docker run [可选参数] image

# 参数说明
--name="Name" 容器名字 用来区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口 (常用)
-p 容器端口
-P 随机指定容器

#启动并进入容器
d3ic1de@d3ic1de-virtual-machine:~/Desktop/centos$ docker run -it centos /bin/bash
[root@20ec2abf8310 /]# ls # 基础版本,很多命令不完善
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr

# 从容器中退出
[root@20ec2abf8310 /]# exit
exit
d3ic1de@d3ic1de-virtual-machine:~/Desktop/centos$

列出所有运行的容器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# docker ps 命令
-a # 列出当前正在运行的容器+带出历史运行过得容器
-n=? # 显示最近创建的容器
-q # 只显示容器的编号
d3ic1de@d3ic1de-virtual-machine:~/Desktop/centos$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d3ic1de@d3ic1de-virtual-machine:~/Desktop/centos$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20ec2abf8310 centos "/bin/bash" 8 minutes ago Exited (0) About a minute ago upbeat_tu
ae2079b0b404 0667dbb1308e "sh -c 'java $JAVA_O…" 9 hours ago Exited (137) 9 hours ago rmqconsole
533573e1fdac 91fcc442211e "sh mqbroker -c /opt…" 9 hours ago Exited (137) 9 hours ago rmqbroker
3caf5c654159 91fcc442211e "sh mqnamesrv" 9 hours ago Exited (137) 9 hours ago rmqnamesrv
33f46149ea98 fbraz3/lnmp "/docker-entrypoint.…" 47 hours ago Exited (0) 23 hours ago 127.0.0.1:80->80/tcp, 127.0.0.1:3306->3306/tcp lnmp_web_1
92caa0397783 hello-world "/hello" 2 days ago Exited (0) 2 days ago quizzical_euclid

退出容器

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

删除容器

1
2
3
docker rm 容器id                 # 删除指定容器,不能删除正在运行的容器
docker rm -f $(docker ps -aq) # 删除所有容器
docker ps -a -q|xargs docker rm # 删除所有的容器

启动和停止容器

1
2
3
4
docker start 容器id     #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止当前正在运行的容器
docker kill 容器id #强制停止当前容器

其他常用命令

后台启动容器

1
2
3
4
5
6
7
docekr run -d 镜像名
3ic1de@d3ic1de-virtual-machine:~/Desktop$ docker run -d centos

# 问题docker ps 发现 centos 停止了

# 常见的坑,docker 容器使用后台运行,就必须要有一个前台进程,docker发现没有应用就会自动停止
# nginx,容器启动后,发现自己没有提供服务,就会立刻停止,没有程序了

查看日志

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
docker logs

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:37Z") or relative (e.g. "42m"
for 42 minutes)
-n, --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:37Z") or relative (e.g. "42m"
for 42 minutes)

docker logs -f -t --tail 条数 容器id #没有日志
# 自己写个shell
d3ic1de@d3ic1de-virtual-machine:~/Desktop$ docker run -d centos /bin/sh -c "while true;do echo 111;sleep 1;done"

d3ic1de@d3ic1de-virtual-machine:~/Desktop$ docker logs -f -t --tail 10 2540f2b9ecf1
2023-07-08T06:32:09.175696908Z 111
2023-07-08T06:32:10.184045587Z 111
2023-07-08T06:32:11.190349012Z 111
2023-07-08T06:32:12.202386500Z 111
2023-07-08T06:32:13.209560305Z 111
2023-07-08T06:32:14.217477073Z 111
2023-07-08T06:32:15.223019838Z 111
2023-07-08T06:32:16.228605450Z 111
2023-07-08T06:32:17.235553572Z 111
2023-07-08T06:32:18.239981668Z 111
2023-07-08T06:32:19.248409996Z 111
2023-07-08T06:32:20.257108808Z 111
2023-07-08T06:32:21.266663642Z 111
2023-07-08T06:32:22.271499978Z 111
2023-07-08T06:32:23.280980439Z 111

查看容器中进程信息

1
2
3
4
5
6
# 命令 docker top 容器id

d3ic1de@d3ic1de-virtual-machine:~/Desktop$ docker top 2540f2b9ecf1
UID PID PPID C STIME
root 3310 3285 0 14:26
root 4297 3310 0 14:40

查看镜像元数据

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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
docker inspect 容器id

Options:
-f, --format string Format output using a custom template:
'json': Print in JSON format
'TEMPLATE': Print output using the given Go template.
Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates
-s, --size Display total file sizes if the type is container
--type string Return JSON for specified type

# 测试
d3ic1de@d3ic1de-virtual-machine:~/Desktop$ docker inspect 33f46149ea98
[
{
"Id": "33f46149ea988dad0035ef7d471ac677490fc232d041f0d1803315ba47cb54cb",
"Created": "2023-07-05T16:41:40.599620735Z",
"Path": "/docker-entrypoint.sh",
"Args": [],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 0,
"Error": "",
"StartedAt": "2023-07-05T16:41:42.025974427Z",
"FinishedAt": "2023-07-06T16:31:09.831610891Z"
},
"Image": "sha256:b1305ced5bb0efd45ef3d37231ee304369d68f3348417a936b977ad0b635a005",
"ResolvConfPath": "/var/lib/docker/containers/33f46149ea988dad0035ef7d471ac677490fc232d041f0d1803315ba47cb54cb/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/33f46149ea988dad0035ef7d471ac677490fc232d041f0d1803315ba47cb54cb/hostname",
"HostsPath": "/var/lib/docker/containers/33f46149ea988dad0035ef7d471ac677490fc232d041f0d1803315ba47cb54cb/hosts",
"LogPath": "/var/lib/docker/containers/33f46149ea988dad0035ef7d471ac677490fc232d041f0d1803315ba47cb54cb/33f46149ea988dad0035ef7d471ac677490fc232d041f0d1803315ba47cb54cb-json.log",
"Name": "/lnmp_web_1",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "docker-default",
"ExecIDs": null,
"HostConfig": {
"Binds": [
"/home/d3ic1de/Desktop/lnmp:/app/public:rw"
],
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"3306/tcp": [
{
"HostIp": "127.0.0.1",
"HostPort": "3306"
}
],
"80/tcp": [
{
"HostIp": "127.0.0.1",
"HostPort": "80"
}
]
},
"RestartPolicy": {
"Name": "",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": [],
"ConsoleSize": [
0,
0
],
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "private",
"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",
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": null,
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": null,
"DeviceCgroupRules": null,
"DeviceRequests": null,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": null,
"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/af3e66825c1b954f8d4f7617cebb1fcf1c10c991df29ddc4e3d4c5cdd07a9d28-init/diff:/var/lib/docker/overlay2/24a5a1ab90146cbed5589e243f65f796848ed16ddbe9bbbe868d6094614485ae/diff:/var/lib/docker/overlay2/2c1c4fbe35ea4ce9d983ea120ea59c81c094b00108541918891703e4e570d110/diff:/var/lib/docker/overlay2/b8dac61c0604607c92b634fb9393b9aa676de803dc6847a6ab52a3c23d3fc666/diff:/var/lib/docker/overlay2/a61933463a0cac7eec97d3bf52331d1d11185eb649061439eeb1fbbbb652e914/diff:/var/lib/docker/overlay2/b104a579067dafad0665dfbd4eb792f5375df094c114a11c99d799b33f307d58/diff:/var/lib/docker/overlay2/c9ac23e243df39d6ea5e2c0db797af5078c01ae378c14a5dc1fd582849c263c2/diff:/var/lib/docker/overlay2/2593d207d5ee0bd40203f56cd3658053efdd1cd4458b69035eb014b6b80d672e/diff:/var/lib/docker/overlay2/01d70802708a2698bdfbdce086c2a123430c2157d21e20009962709d31e6aedd/diff:/var/lib/docker/overlay2/49fc21885e09cf2a539c3a17fe75f6f7da76f8308995b6c6af7b6af80ec1a46a/diff:/var/lib/docker/overlay2/32dcc439ab20279acfb4b3a1fdefad0ce37134dbe0087bffc7f388090ea09774/diff:/var/lib/docker/overlay2/77c3496fc76d955f8d1c051fda81991ec2897c749ea4719ea4fddd6838117cc0/diff:/var/lib/docker/overlay2/1f2a3e382e04c4d7f0b5d907091487ddb83d8809f45a41ad453e21e3b9ad2749/diff:/var/lib/docker/overlay2/5cc8976f800d759c072cbf85ee4772eefc369f19c678f6be0a0ab0a9e43c6d1f/diff:/var/lib/docker/overlay2/2236d265460ef9436afd3772a772e75a6ca68c4f42f95c949f541394649b80b6/diff:/var/lib/docker/overlay2/a0ef1be9cc4d62dc71b3217643bd906ee7b2e2fd4c7d9faaffb10eb2cc427f84/diff:/var/lib/docker/overlay2/1c58f1c281218c81b158aecec2a6916734590eb693f0b13cc6fbad4ed270e90e/diff:/var/lib/docker/overlay2/08d9bd91e7518c74fce61c3c415790cf0f8879cfdc4ec267505ebd3e5f565e56/diff:/var/lib/docker/overlay2/1348ae04abbf6a379ef3513e7e80983642b2f6a8e2b3e3297f72e54d741b0be5/diff:/var/lib/docker/overlay2/98dd357d854916a64f0bde7d1df2a2d821b428c43d219a1ee926d7a976120ede/diff:/var/lib/docker/overlay2/2c60de8e7fcfb7dc12151eaa9a6403cfad5a8ab5b90dea493bcabf812ee37fbc/diff",
"MergedDir": "/var/lib/docker/overlay2/af3e66825c1b954f8d4f7617cebb1fcf1c10c991df29ddc4e3d4c5cdd07a9d28/merged",
"UpperDir": "/var/lib/docker/overlay2/af3e66825c1b954f8d4f7617cebb1fcf1c10c991df29ddc4e3d4c5cdd07a9d28/diff",
"WorkDir": "/var/lib/docker/overlay2/af3e66825c1b954f8d4f7617cebb1fcf1c10c991df29ddc4e3d4c5cdd07a9d28/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "bind",
"Source": "/home/d3ic1de/Desktop/lnmp",
"Destination": "/app/public",
"Mode": "rw",
"RW": true,
"Propagation": "rprivate"
}
],
"Config": {
"Hostname": "33f46149ea98",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"3306/tcp": {},
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": null,
"Image": "fbraz3/lnmp",
"Volumes": {
"/app/public": {}
},
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"com.docker.compose.config-hash": "4fffe6d4eb4ca1ef849c69fe9e9f1c9f40524731116ae330e2af822d1d17c853",
"com.docker.compose.container-number": "1",
"com.docker.compose.oneoff": "False",
"com.docker.compose.project": "lnmp",
"com.docker.compose.project.config_files": "docker-compose.yml",
"com.docker.compose.project.working_dir": "/home/d3ic1de/Desktop/lnmp",
"com.docker.compose.service": "web",
"com.docker.compose.version": "1.29.2",
"org.opencontainers.image.ref.name": "ubuntu",
"org.opencontainers.image.version": "22.04"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "ace42eae9a6986cb1ef738bf5ba7569f675306a60945d0a07273c6a1b29aa0a7",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"3306/tcp": [
{
"HostIp": "127.0.0.1",
"HostPort": "3306"
}
],
"80/tcp": [
{
"HostIp": "127.0.0.1",
"HostPort": "80"
}
]
},
"SandboxKey": "/var/run/docker/netns/ace42eae9a69",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "21c954855f3136add678ddd8a2330f54f03613cb99a6b4aeb2bd79b602936836",
"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": "590e637f848220fd6191d29a6ac5642718f0536b9b13ed7c92d0c53d1bba79b1",
"EndpointID": "21c954855f3136add678ddd8a2330f54f03613cb99a6b4aeb2bd79b602936836",
"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
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
# 命令
docker exec -it 容器id bashShell

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
--env-file list Read in a file of 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

# 测试
d3ic1de@d3ic1de-virtual-machine:~/Desktop$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2540f2b9ecf1 centos "/bin/sh -c 'while t…" 44 minutes ago Up 44 minutes affectionate_kare
d3ic1de@d3ic1de-virtual-machine:~/Desktop$ docker exec -it 2540f2b9ecf1 /bin/bash
[root@2540f2b9ecf1 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@2540f2b9ecf1 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 06:26 ? 00:00:04 /bin/sh -c while true;do echo 111;sleep 1;done
root 2706 0 0 07:11 pts/0 00:00:00 /bin/bash
root 3007 1 0 07:16 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root 3008 2706 0 07:16 pts/0 00:00:00 ps -ef

# 方式二
docker attach 容器id

Options:
--detach-keys string Override the key sequence for detaching a container
--no-stdin Do not attach STDIN
--sig-proxy Proxy all received signals to the process (default true)

d3ic1de@d3ic1de-virtual-machine:~/Desktop$ docker attach 2540f2b9ecf1
正在执行当前的代码

# docker exec # 进入容器后开启一个新的终端,可以在里面操作(常用)
# docker attach # 进入容器正在执行的终端,不会启动新的进程

从容器内拷贝文件到主机上

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
docker cp 容器id:容器内路径  目的的主机路径

# 查看当前主机目录下
d3ic1de@d3ic1de-virtual-machine:/home$ ls
d3ic1de
d3ic1de@d3ic1de-virtual-machine:/home$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2851d76877c5 centos "/bin/bash" 25 seconds ago Up 23 seconds affectionate_beaver
# 进入容器内部
d3ic1de@d3ic1de-virtual-machine:/home$ docker attach 2851d76877c5
[root@2851d76877c5 /]# cd /home
[root@2851d76877c5 home]# ls
# 在容器内新建文件
[root@2851d76877c5 home]# touch test.py
[root@2851d76877c5 home]# ls
test.py
[root@2851d76877c5 home]# exit
exit
d3ic1de@d3ic1de-virtual-machine:/home$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d3ic1de@d3ic1de-virtual-machine:/home$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2851d76877c5 centos "/bin/bash" 26 minutes ago Exited (0) 17 seconds ago affectionate_beaver
# 将容器内的文件拷贝到主机上
d3ic1de@d3ic1de-virtual-machine:/home$ sudo docker cp 2851d76877c5:/home/test.py /home
[sudo] password for d3ic1de:
Successfully copied 1.54kB to /home
d3ic1de@d3ic1de-virtual-machine:/home$ ls
d3ic1de test.py
d3ic1de@d3ic1de-virtual-machine:/home$

# 拷贝是一个手动过程,使用 -v 卷的技术,可以自动实现

命令汇总

Use the Docker command line | Docker Documentation

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
attach	    # 将本地标准输入、输出和错误流附加到正在运行的容器
build # 从 Docker 文件构建映像
builder # 管理生成
checkpoint # 管理检查点
commit # 根据容器的更改创建新映像
config # 管理群配置
container # 管理容器
context # 管理上下文
cp # 在容器和本地文件系统之间复制文件/文件夹
create # 创建新容器
diff # 检查对容器文件系统上的文件或目录的更改
events # 从服务器获取实时事件
exec # 在正在运行的容器中执行命令
export # 将容器的文件系统导出为 tar 归档
history # 显示图像的历史记录
image # 管理图像
images # 列表图像
import # 从压缩包导入内容以创建文件系统映像
info # 显示系统范围的信息
inspect # 返回有关 Docker 对象的低级信息
kill # 终止一个或多个正在运行的容器
load # 从 tar 存档或 STDIN 加载图像
login # 登录到注册表
logout # 从注册表注销
logs # 获取容器的日志
manifest # 管理 Docker 映像清单和清单列表
network # 管理网络
node # 管理群节点
pause # 暂停一个或多个容器中的所有进程
plugin # 管理插件
port # 列出容器的端口映射或特定映射
ps # 列出容器
pull # 从注册表下载映像
push # 将映像上传到注册表
rename # 重命名容器
restart # 重新启动一个或多个容器
rm # 删除一个或多个容器
rmi # 删除一个或多个图像
run # 从映像创建并运行新容器
save # 将一张或多张图像保存到 tar 存档(默认情况式传输到 STDOUT)
search # 在 Docker Hub 中搜索映像
secret # 管理群机密
service # 管理群服务
stack # 管理群堆栈
start # 启动一个或多个已停止的容器
stats # 显示容器资源使用情况统计信息的实时流
stop # 停止一个或多个正在运行的容器
swarm # 管理群
system # 管理docker
tag # 创建引用SOURCE_IMAGE的标记TARGET_IMAGE
top # 显示容器正在运行的进程
trust # 管理 Docker 映像上的信任
unpause # 取消暂停一个或多个容器中的所有进程
update # 更新一个或多个容器的配置
version # 显示 Docker 版本信息
volume # 管理卷
wait # 阻止,直到一个或多个容器停止,然后打印其退出代码