Docker|2.常用操作命令

查看版本信息

1
2
3
docker version
docker info

获取帮助

1
docker --help

镜像命令

列出本地镜像docker images

-a: 列出本地所有镜像(包含中间层镜像)

-q:只显示镜像ID

–digests: 显示镜像的摘要信息

–no-trunc: 显示完整的镜像信息

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest b56d8850aed5 4 days ago 529MB
centos latest 470671670cac 3 weeks ago 237MB
hello-world latest fce289e99eb9 13 months ago 1.84kB
REPOSITORY: 镜像仓库源
TAG:镜像标签
IMAGE ID :镜像ID
CREATED:镜像创建时间
SIZE: 镜像大小
同一仓库源可以有多个TAG,代表这个仓库源的不同个版本,我们使用REPOSITORY:TAG来定义不同的镜像。
如果不指定一个镜像的标签版本,docker将默认使用REPOSITORY:lastest镜像

虽然本地已经修改为阿里云加速镜像站,但是查询还是从https://hub.docker.com/上查找镜像。

–no-trunc: 显示完整的镜像描述

-s: 列出收藏数不小于指定的镜像

–automated: 只列出automated build类型的镜像

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost ~]# docker search tomcat -s 50
Flag --stars has been deprecated, use --filter=stars=3 instead
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
tomcat Apache Tomcat is an open source implementati… 2637 [OK]
tomee Apache TomEE is an all-Apache Java EE certif… 74 [OK]
dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base… 53 [OK]
NAME:镜像名称
DESCRIPTION:镜像描述
STARS:点赞数
OFFICIAL:官方镜像
AUTOMATED:自动更新

拉取镜像dockerpull

下载镜像 docker pull 镜像名称[:TAG],如果不指定TAG,默认为lastest

删除镜像docker rmi

删除镜像: docker rmi 镜像名称/镜像ID

删除单个镜像: docker rmi -f 镜像名称/镜像ID

删除多个镜像: docker rmi -f 镜像名称1:TAG/镜像ID1[空格]镜像名称2:TAG/镜像ID2

删除全部镜像: docker rmi -f $(docker images -qa)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
删除镜像
[root@localhost ~]# docker rmi hello-world
Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container c0c56d69276d is using its referenced image fce289e99eb9
强行删除镜像
[root@localhost ~]# docker rmi -f fce289e99eb9
Untagged: hello-world:latest
Untagged: hello-world@sha256:9572f7cdcee8591948c2963463447a53466950b3fc15a247fcad1917ca215a2f
Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
删除多个镜像
[root@localhost ~]# docker rmi -f hello-world nginx
Untagged: hello-world:latest
Untagged: hello-world@sha256:9572f7cdcee8591948c2963463447a53466950b3fc15a247fcad1917ca215a2f
Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
Untagged: nginx:latest
Untagged: nginx@sha256:ad5552c786f128e389a0263104ae39f3d3c7895579d45ae716f528185b36bc6f
Deleted: sha256:2073e0bcb60ee98548d313ead5eacbfe16d9054f8800a32bedd859922a99a6e1
Deleted: sha256:a3136fbf38691346715cac8360bcdfca0fff812cede416469653670f04e2cab0
Deleted: sha256:99360ffcb2da18fd9ede194efaf5d4b90e7aee99f45737e918113e6833dcf278
Deleted: sha256:488dfecc21b1bc607e09368d2791cb784cf8c4ec5c05d2952b045b3e0f8cc01e

容器命令

新建并启动容器

启动容器必须有一个镜像,如果本地有镜像就从本地启动,如果本地没有则先去镜像站点拉取镜像然后启动,并保存镜像到本地;

docker run 镜像名称

–name=”容器新名字”:为容器指定一个名称;

-d:后台运行容器,并返回容器ID,即启动守护式容器;

-i:已交互模式运行容器,通常与-t同时使用;

-t:为容器重新分配一个伪输入终端,通常与-i同时使用;

-P:随机端口映射;

-p:指定端口映射,有以下四种格式 ip:hostPort:containerPort / ip::containerPort / hostPort:containerPort / containerPort

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest b56d8850aed5 4 days ago 529MB
centos latest 470671670cac 3 weeks ago 237MB
# 启动交互式容器
[root@localhost ~]# docker run -it --name centos20200211 470671670cac
[root@8ec89289309a /]# pwd
/
[root@8ec89289309a /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@8ec89289309a /]#
# 启动守护式容器
docker run -d
# 启动一个mysql容器
docker run -p 3306:3306 --name db1 -v /data/mysql/conf:/etc/mysql/conf.d -v /data/mysql/logs:/logs -v /data/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=111 -d mysql:5.7.29

列出的容器

docker ps

-a: 列出所有的容器,包含已经停止的容器

-l: 显示最新创建的容器

-n: 显示最近n次创建的容器

-q: 静默模式,只显示容器编号

–no-trunc :不截断输出

1
2
3
4
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8ec89289309a 470671670cac "/bin/bash" 3 minutes ago Up 3 minutes centos20200211
8a5da2a84c74 centos "/bin/bash" 3 hours ago Up 3 hours centos0211

退出容器

  • exit 容器停止退出

  • Ctrl+P+Q 容器不停止退出

开两个终端做个操作

1
2
3
4
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8ec89289309a 470671670cac "/bin/bash" 6 minutes ago Up 6 minutes centos20200211
8a5da2a84c74 centos "/bin/bash" 3 hours ago Up 3 hours centos0211
1
2
3
[root@8ec89289309a /]# exit
exit
[root@localhost ~]#
1
2
3
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8a5da2a84c74 centos "/bin/bash" 3 hours ago Up 3 hours centos0211

启动容器

docker start 容器id/容器名称

1
2
3
4
5
6
7
8
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ba4a8278426e 470671670cac "/bin/bash" 12 minutes ago Up 12 minutes centos20200211
f34465900de2 centos "/bin/bash" 28 minutes ago Exited (127) 27 minutes ago stoic_ride
8a5da2a84c74 centos "/bin/bash" 3 hours ago Up 3 hours centos0211
899f21d7be4f 470671670cac "/bin/bash" 4 hours ago Exited (0) 3 hours ago dazzling_hypatia
8821a07e9822 470671670cac "/bin/bash" 4 hours ago Exited (130) 4 hours ago infallible_jones
c0c56d69276d fce289e99eb9 "/hello" 31 hours ago Exited (0) 31 hours ago interesting_dirac
1
2
3
4
5
6
7
[root@localhost ~]# docker start 899f21d7be4f
899f21d7be4f
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ba4a8278426e 470671670cac "/bin/bash" 12 minutes ago Up 12 minutes centos20200211
8a5da2a84c74 centos "/bin/bash" 3 hours ago Up 3 hours centos0211
899f21d7be4f 470671670cac "/bin/bash" 4 hours ago Up 5 seconds dazzling_hypatia

重启容器

docker restart 容器id/容器名称

1
2
3
4
5
6
7
[root@localhost ~]# docker restart 899f21d7be4f
899f21d7be4f
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ba4a8278426e 470671670cac "/bin/bash" 14 minutes ago Up 14 minutes centos20200211
8a5da2a84c74 centos "/bin/bash" 4 hours ago Up 4 hours centos0211
899f21d7be4f 470671670cac "/bin/bash" 4 hours ago Up 1 second dazzling_hypatia

停止容器

docker stop 容器id/容器名称

1
2
3
4
5
6
7
8
9
10
11
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ba4a8278426e 470671670cac "/bin/bash" 16 minutes ago Up 16 minutes centos20200211
8a5da2a84c74 centos "/bin/bash" 4 hours ago Up 4 hours centos0211
899f21d7be4f 470671670cac "/bin/bash" 4 hours ago Up About a minute dazzling_hypatia
[root@localhost ~]# docker stop 899f21d7be4f
899f21d7be4f
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ba4a8278426e 470671670cac "/bin/bash" 16 minutes ago Up 16 minutes centos20200211
8a5da2a84c74 centos "/bin/bash" 4 hours ago Up 4 hours centos0211

强行停止容器

docker kill 容器id/容器名称

1
2
3
4
5
6
7
8
9
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ba4a8278426e 470671670cac "/bin/bash" 16 minutes ago Up 16 minutes centos20200211
8a5da2a84c74 centos "/bin/bash" 4 hours ago Up 4 hours centos0211
[root@localhost ~]# docker kill 8a5da2a84c74
8a5da2a84c74
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ba4a8278426e 470671670cac "/bin/bash" 18 minutes ago Up 18 minutes centos20200211

删除容器

删除已经停止的单个容器 docker rm 容器id/容器名称

删除正在运行的单个容器 docker rm -f 容器id/容器名称

一次性删除多个容器 docker rm -f $(docker ps -a -q) / docker ps -a -q |xargs docker rm

1
2
3
4
5
6
7
8
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ba4a8278426e 470671670cac "/bin/bash" 43 minutes ago Up 43 minutes centos20200211
# 不加-f,只会删除已经停止退出的容器
[root@localhost ~]# docker rm -f $(docker ps -a -q)
ba4a8278426e
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

查看容器日志

docker logs 容器id

-t 加入时间戳

-t 跟随最新的日志打印

–tail 数字显示最后的多少条

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@localhost ~]# docker logs -t -f --tail 20 4f30ee04eac7
2020-02-11T17:00:51.830836896Z bash: ll: command not found
2020-02-11T17:01:56.146309331Z [root@4f30ee04eac7 /]# ss
2020-02-11T17:01:56.279428980Z Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
2020-02-11T17:02:08.128296224Z [root@4f30ee04eac7 /]# ps
2020-02-11T17:02:08.134374708Z bash: $'\345\271ps': command not found
2020-02-11T17:02:13.669104746Z [root@4f30ee04eac7 /]ps -ef
2020-02-11T17:02:13.685931632Z UID PID PPID C STIME TTY TIME CMD
2020-02-11T17:02:13.687150066Z root 1 0 0 16:59 pts/0 00:00:00 /bin/bash
2020-02-11T17:02:13.687185878Z root 18 1 0 17:02 pts/0 00:00:00 ps -ef
[root@4f30ee04eac7 /]#
[root@4f30ee04eac7 /]# ps -ef
2020-02-11T17:05:14.601317299Z UID PID PPID C STIME TTY TIME CMD
2020-02-11T17:05:14.601362985Z root 1 0 0 16:59 pts/0 00:00:00 /bin/bash
2020-02-11T17:05:14.601375403Z root 19 1 0 17:05 pts/0 00:00:00 ps -ef
2020-02-11T17:05:15.511402720Z [root@4f30ee04eac7 /]# pwd
2020-02-11T17:05:15.511432928Z /
2020-02-11T17:05:16.155997418Z [root@4f30ee04eac7 /]# ls
2020-02-11T17:05:16.160923769Z bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
2020-02-11T17:05:18.225781961Z [root@4f30ee04eac7 /]# cd
2020-02-11T17:05:20.175387626Z [root@4f30ee04eac7 ~]# id
2020-02-11T17:05:20.180529056Z uid=0(root) gid=0(root) groups=0(root)

查看容器内进程

docker top 容器id

1
2
3
[root@localhost ~]# docker top 4f30ee04eac7
UID PID PPID C STIME TTY TIME CMD
root 4648 4631 0 00:59 pts/0 00:00:00 /bin/bash

进入容器

docker exec -it 容器ID bash

docker attach 容器ID

attach 直接进入容器启动命令的终端,不会启动新的进程

exec 是在容器中打开新的终端,并且可以启动新的进程

1
2
3
4
5
6
7
8
9
[root@localhost ~]# docker run -it centos
[root@cd7a272e8786 /]#
# 按Ctrl+P+Q退出容器
[root@localhost ~]#
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cd7a272e8786 centos "/bin/bash" 12 seconds ago Up 11 seconds heuristic_perlman
[root@localhost ~]# docker attach cd7a272e8786
[root@cd7a272e8786 /]#
1
2
3
4
[root@localhost ~]# docker exec -it -t cd7a272e8786 ls -l /tmp
total 8
-rwx------ 1 root root 671 Jan 13 21:49 ks-script-_srt3u3c
-rwx------ 1 root root 1379 Jan 13 21:49 ks-script-gpqu_kuo

查看容器内部细节

docker inspect 容器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
[root@localhost ~]# docker inspect 4f30ee04eac7
[
{
"Id": "4f30ee04eac790df1edc4d1ae87df1096cb4aac78f8b5a741602068f5a3ad1ba",
"Created": "2020-02-11T16:59:07.103035515Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 4648,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-02-11T16:59:07.625242514Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:470671670cac686c7cf0081e0b37da2e9f4f768ddc5f6a26102ccd1c6954c1ee",
"ResolvConfPath": "/var/lib/docker/containers/4f30ee04eac790df1edc4d1ae87df1096cb4aac78f8b5a741602068f5a3ad1ba/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/4f30ee04eac790df1edc4d1ae87df1096cb4aac78f8b5a741602068f5a3ad1ba/hostname",
"HostsPath": "/var/lib/docker/containers/4f30ee04eac790df1edc4d1ae87df1096cb4aac78f8b5a741602068f5a3ad1ba/hosts",
"LogPath": "/var/lib/docker/containers/4f30ee04eac790df1edc4d1ae87df1096cb4aac78f8b5a741602068f5a3ad1ba/4f30ee04eac790df1edc4d1ae87df1096cb4aac78f8b5a741602068f5a3ad1ba-json.log",
"Name": "/centos20200211",
"RestartCount": 0,
"Driver": "devicemapper",
"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": {
"DeviceId": "32",
"DeviceName": "docker-253:0-102243954-b0ecd426ae759df49cab32c752911e1dcd35a84afc31955ba49a4699db11092d",
"DeviceSize": "10737418240"
},
"Name": "devicemapper"
},
"Mounts": [],
"Config": {
"Hostname": "4f30ee04eac7",
"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": "470671670cac",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20200114",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS",
"org.opencontainers.image.created": "2020-01-14 00:00:00-08:00",
"org.opencontainers.image.licenses": "GPL-2.0-only",
"org.opencontainers.image.title": "CentOS Base Image",
"org.opencontainers.image.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "6b6ff8333ff7e94732a09dddc37744728b0febc6c1bf06bfaaf54bf791e576d3",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/6b6ff8333ff7",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "66edac9cd524223f0a57e4c58d5080114826f6a90db64f1e734f6f6f5371627e",
"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": "cd648ca5b88f8ec9e946884ed18c21fe1f2bbf214a5e3bd0094bdd61cc36f41c",
"EndpointID": "66edac9cd524223f0a57e4c58d5080114826f6a90db64f1e734f6f6f5371627e",
"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
}
}
}
}
]
独学无友,孤陋寡闻;学习记录,欢迎拍砖!