Docker的常用命令

帮助命令

1
2
3
docker version        #显示Docker的版本信息
docker info #显示Docker的系统信息,包括镜像和容器的数量
docker 命令 --help #万能命令,显示Docker的所有命令
  • 帮助文档的命令:doc文档下,点击reference,里面的Command-line就是命令行,每个命令都可以查到。

镜像命令

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
iyheart@iyheart-virtual-machine:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest e4c58958181a 8 weeks ago 77.8MB
hello-world latest 9c7a54a9a43c 7 months ago 13.3kB

# 解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的id
CREATED 镜像的创建时间
SIZE 镜像的大小

# 可选项
-a, --all #列出所有镜像
--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 #显示镜像的ID

docker search 搜索镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
iyheart@iyheart-virtual-machine:~$ docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 14664 [OK]
mariadb MariaDB Server is a high performing open sou… 5594 [OK]
percona Percona Server is a fork of the MySQL relati… 622 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 906 [OK]
bitnami/mysql Bitnami MySQL Docker Image 104 [OK]


iyheart@iyheart-virtual-machine:~$ docker search --help

可选项:
-f, --filter filter Filter output based on conditions provided
--filter=STARS=3000 #搜索出来的镜像就是STARS大于3000的
#********************************
iyheart@iyheart-virtual-machine:~$ docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 14664 [OK]
mariadb MariaDB Server is a high performing open sou… 5594 [OK]
#********************************

--format string Pretty-print search using a Go template
--limit int Max number of search results
--no-trunc Don't truncate output

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
31
32
33
34
35
36
37
38
39
40
41
# 下载镜像 docker pull 镜像名[:tag]
iyheart@iyheart-virtual-machine:~$ docker pull mysql
Using default tag: latest # 如果不写 tag,默认就是 latest
latest: Pulling from library/mysql
72a69066d2fe: Pull complete # 分层下载,docker iamge的核心 联合文件系统
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709 # 签名 防伪
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址

# 以下两者是等价的
docker pull mysql
docker pull docker.io/library/mysql:latest

# 指定版本下载
iyheart@iyheart-virtual-machine:~$ docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Already exists # 分层下载,使得不同版本能共用某些文件,这样极大的节省了内存空间
93619dbc5b36: Already exists
99da31dd6142: Already exists
626033c43d70: Already exists
37d5d7efb64e: Already exists
ac563158d721: Already exists
d2ba16033dad: Already exists
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

docker1 图片1

docker rmi 删除镜像

1
2
3
iyheart@iyheart-virtual-machine:~$ docker rmi -f 镜像id #删除指定的镜像
iyheart@iyheart-virtual-machine:~$ docker rmi -f 镜像id 镜像id 镜像id # 删除多个镜像
iyheart@iyheart-virtual-machine:~$ docker rmi -f $(docker images -aq) # 删除全部镜像

容器命令

说明:我们有了镜像才可以创建容器,linux,下载一个centos镜像来测试学习

1
docker pull centos

新建容器并启动

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

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

# 测试,启动并进入容器
iyheart@iyheart-virtual-machine:~$ docker run -it centos bin/bash
[root@2e5cc43488e0 /]# ls # 查看容器内的centos,基础版本,很多命令都是不完善的
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@2e5cc43488e0 /]# exit # 退出容器

列出所有在运行的容器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#docker ps 命令
-a # 列出当前正在运行的容器+带出历史运行过的容器
-n=? # 显示最近创建的容器
-q # 只显示容器的编号

iyheart@iyheart-virtual-machine:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
iyheart@iyheart-virtual-machine:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2e5cc43488e0 centos "bin/bash" 2 minutes ago Exited (0) 51 seconds ago sharp_spence
a2633b88f9b5 hello-world "/hello" 16 hours ago Exited (0) 16 hours ago optimistic_leakey
d0d48538cbec hello-world "/hello" 4 days ago Exited (0) 4 days ago beautiful_mclaren


iyheart@iyheart-virtual-machine:~$ docker ps -a -n=1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2e5cc43488e0 centos "bin/bash" 3 minutes ago Exited (0) 2 minutes ago sharp_spence

退出容器

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

删除容器

1
2
3
docker rm 容器id            # 删除指定的容器,不能删除正在运行的容器,如果强制删除 rm -f
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
# 命令 docker run -d 镜像名:
iyheart@iyheart-virtual-machine:~$ docker run -d centos
#问题docker ps,发现 centos停止了

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

image-20231203124549822

查看日志

1
2
3
4
5
6
7
8
9
10
11
12
13
docker logs -f -t --tail 容器

# 如果没有日志,可以自己编写一段shell脚本
iyheart@iyheart-virtual-machine:~$ docker run -d centos /bin/sh -c "while true;do echo abc;sleep 1;done"

#iyheart@iyheart-virtual-machine:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b8c7b5f31707 centos "/bin/sh -c 'while t…" 8 seconds ago Up 6 seconds kind_dewdney

# 显示日志
-tf # 显示日志
--tail number # 要显示日志条数
iyheart@iyheart-virtual-machine:~$ docker logs -tf --tail 10 b8c7b5f31707

查看容器中进程信息 ps

1
2
3
4
# 命令 docker top 容器id
iyheart@iyheart-virtual-machine:~$ docker top 45193e60990d
UID PID PPID C STIME TTY TIME CMD
root 4511 4471 0 19:28 pts/0 00:00:00 /bin/bash

查看镜像源数据

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
# 命令 docker inspect 容器id

# 可选项
-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




iyheart@iyheart-virtual-machine:~$ docker inspect b8c7b5f31707
[
{
"Id": "b8c7b5f317075244f4361437ea4ce0396823960e31974c3ed40a4b811f39068c",
"Created": "2023-12-03T05:02:48.706377904Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo abc;sleep 1;done"
],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 137,
"Error": "",
"StartedAt": "2023-12-03T05:02:49.616046085Z",
"FinishedAt": "2023-12-03T05:06:04.965945912Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/b8c7b5f317075244f4361437ea4ce0396823960e31974c3ed40a4b811f39068c/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/b8c7b5f317075244f4361437ea4ce0396823960e31974c3ed40a4b811f39068c/hostname",
"HostsPath": "/var/lib/docker/containers/b8c7b5f317075244f4361437ea4ce0396823960e31974c3ed40a4b811f39068c/hosts",
"LogPath": "/var/lib/docker/containers/b8c7b5f317075244f4361437ea4ce0396823960e31974c3ed40a4b811f39068c/b8c7b5f317075244f4361437ea4ce0396823960e31974c3ed40a4b811f39068c-json.log",
"Name": "/kind_dewdney",
"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,
"ConsoleSize": [
24,
80
],
"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": [],
"BlkioDeviceReadBps": [],
"BlkioDeviceWriteBps": [],
"BlkioDeviceReadIOps": [],
"BlkioDeviceWriteIOps": [],
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"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",
"/sys/devices/virtual/powercap"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/cd4df83ee866f8f211fff6bab2a3dfe6fe57d6096754d6390df91c997d87420b-init/diff:/var/lib/docker/overlay2/17ed3a01cf050df63ae92a06a1d3c2e10573ac15ce450b7590affda6a7e88080/diff",
"MergedDir": "/var/lib/docker/overlay2/cd4df83ee866f8f211fff6bab2a3dfe6fe57d6096754d6390df91c997d87420b/merged",
"UpperDir": "/var/lib/docker/overlay2/cd4df83ee866f8f211fff6bab2a3dfe6fe57d6096754d6390df91c997d87420b/diff",
"WorkDir": "/var/lib/docker/overlay2/cd4df83ee866f8f211fff6bab2a3dfe6fe57d6096754d6390df91c997d87420b/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "b8c7b5f31707",
"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 abc;sleep 1;done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20210915",
"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": "3dc39d8c49ab26e8d33429d9df18f123f5f253de1b0351bb1f74e05e8ee22342",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/3dc39d8c49ab",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "af2411cafe6c9ea5ab439c16bb0e87a9c69b3fb049352b998fbdc0565f790ff0",
"EndpointID": "",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"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
# 我们通常容器都是使用后台方式运行的,需要进入容器,修改一些配置

# 命令
docker exec -it 容器id bashshell

# 测试
iyheart@iyheart-virtual-machine:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
45193e60990d centos "/bin/bash" 10 hours ago Up 10 hours modest_hypatia
iyheart@iyheart-virtual-machine:~$ docker exec -it 45193e60990d
"docker exec" requires at least 2 arguments.
See 'docker exec --help'.

Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Execute a command in a running container
iyheart@iyheart-virtual-machine:~$ docker exec -it 45193e60990d /bin/bash
[root@45193e60990d /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 11:28 pts/0 00:00:00 /bin/bash
root 15 0 0 14:36 pts/1 00:00:00 /bin/bash
root 29 15 0 14:36 pts/1 00:00:00 ps -ef


# 方式二
dokcer attach 容器id

# 测试
iyheart@iyheart-virtual-machine:~$ docker attach 45193e60990d
正在执行当前的代码....

# 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
33
34
35
36
37
# 命令 docker cp 容器id:容器内路径 目的主机路径

# 查看当前主机目录下
iyheart@iyheart-virtual-machine:/home$ ls
555.java iyheart
iyheart@iyheart-virtual-machine:/home$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f7128d9a8bd9 centos "/bin/bash" 4 minutes ago Exited (0) 13 seconds ago condescending_bhaskara

# 进入docker容器内部
iyheart@iyheart-virtual-machine:/home$ docker attach f7128d9a8bd9
[root@f7128d9a8bd9 /]# cd /home
[root@f7128d9a8bd9 home]# ls
# 在容器内新建一个文件
[root@f7128d9a8bd9 home]# touch test.java
[root@f7128d9a8bd9 home]# ls
test.java
[root@f7128d9a8bd9 home]# exit
exit
iyheart@iyheart-virtual-machine:/home$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f7128d9a8bd9 centos "/bin/bash" 4 minutes ago Exited (0) 13 seconds ago condescending_bhaskara
# 将这文件拷贝出来到主机上

iyheart@iyheart-virtual-machine:/home$ docker cp f7128d9a8bd9:/home/test.java /home
Successfully copied 1.54kB to /home
open /home/test.java: permission denied

# 注意这里出现报错,报错的原因是因为主机终端权限不足
# 这里只需要在docker命令前面添加sudo即可

iyheart@iyheart-virtual-machine:/home$ sudo docker cp f7128d9a8bd9:/home/test.java /home
Successfully copied 1.54kB to /home
iyheart@iyheart-virtual-machine:/home$ ls
555.java iyheart test.java

# 拷贝是一个手动的过程,未来我们使用 -v 卷的技术,可以实现连通

作业练习

docker部署Nignx

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
# 启动docker
systemctl start docker

# 查看docker是否能正常运行
docker version
或者也可以使用指令 docker info

# 查看nignx
可以使用指令docker search nignx
也可以在docker hub上搜索nignx(推荐)(有帮助文档和指定版本)

# 拉取nignx镜像
docker pull nignx

# 运行nignx并测试
# 运行
iyheart@iyheart-virtual-machine:~$ docker run -d --name nginx01 -p3344:80 nginx
# -d 后台运行
# --name 给容器命名
# -p 宿主机端口:容器内部端口
######### (尚未解决)测试如何用公网访问Nginx 问题:不知道是哪个ip:80


# 进入nginx所在容器
iyheart@iyheart-virtual-machine:~$ docker exec -it nginx01 /bin/bash

# 查看nginx所在配置
root@af25ad564c6a:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@af25ad564c6a:/# cd /etc/nginx
root@af25ad564c6a:/etc/nginx# ls
conf.d mime.types nginx.conf uwsgi_params
fastcgi_params modules scgi_params

image-20231204185046935

image-20231204185121865

image-20231204185145077

image-20231204185200515

image-20231204190057224

  • iyheart@iyheart-virtual-machine:~$ docker run -d —name nginx01 -p3344:80 nginx指令的意思:让外网能访问到docker容器内部的nginx

image-20231204190248753

  • 思考问题:我们每改动nginx配置文件,都需要进入容器内部,这十分麻烦。要是可以在容器外部提供一个映射路径,达到在容器修改文件名,容器内部就可以自动修改? -v 数据卷

docker部署Tomcat

docker部署ES+Kibana