Docker 常用命令

帮助命令

1
2
3
docker version #显示详细信息
docker info #显示更详细的信息
docker命令--help #万能命令

官网命令教程

镜像命令

docker images

1
2
3
4
5
6
7
8
9
10
11
12
13
docker images #查询
#运行结果
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 12 months ago 13.3kB
docker images -a #查询全部镜像
#运行结果
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 12 months ago 13.3kB
docker images -q #显示镜像的id
#运行结果
bf756fb1ae65
docker images -aq #显示所有的id
bf756fb1ae65

可选项

Name, shorthand Default Description
--all , -a Show all images (default hides intermediate images)
--digests Show digests
--filter , -f Filter output based on conditions provided
--format Pretty-print images using a Go template
--no-trunc Don’t truncate output
--quiet , -q Only show image IDs

REPOSITORY :镜像的仓库源

TAG : 镜像的标签

IMAGE ID:镜像的id

CREATED:镜像的创建时间

SIZE:镜像的大小

1
2
3
4
5
6
7
8
9
10
11
12
13
docker search mysql #搜索mysql的镜像信息
#运行结果
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10380 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3848 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 758 [OK]
percona Percona Server is a fork of the MySQL relati… 519 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 87
docker search mysql --filter=STARS=3000 #搜索STARS>3000的mysql的镜像信息
#运行结果
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10380 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3848 [OK]

可选项

Name, shorthand Default Description
--filter , -f Filter output based on conditions provided
--format Pretty-print search using a Go template
--limit 25 Max number of search results
--no-trunc Don’t truncate output

都是在dockerhub上搜索

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 mysql # 下载MySQL的镜像
#运行结果--默认下载最新版
Using default tag: latest #tag就是版本号,默认为最新
latest: Pulling from library/mysql
a076a628af6f: Pull complete #分层下载
f6c208f3f991: Pull complete
88a9455a9165: Pull complete
406c9b8427c6: Pull complete
7c88599c0b25: Pull complete
25b5c6debdaf: Pull complete
43a5816f1617: Pull complete
1a8c919e89bf: Pull complete
9f3cf4bd1a07: Pull complete
80539cea118d: Pull complete
201b3cad54ce: Pull complete
944ba37e1c06: Pull complete
Digest: sha256:feada149cb8ff54eade1336da7c1d080c4a1c7ed82b5e320efb5beebed85ae8c
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真实地址
#因此
docker pull mysql
等价于
docker pull docker.io/library/mysql:latest
#下载指定版本的MySQL
docker pull mysql:5.7
#运行结果
5.7: Pulling from library/mysql
a076a628af6f: Already exists
f6c208f3f991: Already exists
88a9455a9165: Already exists
406c9b8427c6: Already exists #与刚刚下载的分层公用了
7c88599c0b25: Already exists
25b5c6debdaf: Already exists
43a5816f1617: Already exists
1831ac1245f4: Pull complete
37677b8c1f79: Pull complete
27e4ac3b0f6e: Pull complete
7227baa8c445: Pull complete
Digest: sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

可选项

Name, shorthand Default Description
--all-tags , -a Download all tagged images in the repository
--disable-content-trust true Skip image verification
--platform API 1.32+ Set platform if server is multi-platform capable
--quiet , -q Suppress verbose output

docker rmi 删除镜像

1
2
3
4
5
6
#查询镜像
docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 a70d36bc331a 16 hours ago 449MB
mysql latest c8562eaf9d81 16 hours ago 546MB
hello-world latest bf756fb1ae65 12 months ago 13.3kB
1
2
3
4
5
#删除ID为 a70d36bc331a的镜像
docker rmi -f a70d36bc331a

#删除所有镜像
docker rmi -f $(docker images -aq)

容器命令

新建容器并启动

用docker下载一个centOS

1
docker pull centos

docker run

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
docker run [可选参数] image
#参数
--name="Name" 容器名字”tomcat01 tomcat02“,用来区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-P 指定容器的端口-p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口
-p 容器端口
容器端口
-p 随机指定端口

#进入容器
[root@Shyee ~]# docker run -it centos /bin/bash
[root@c549e8ba2456 /]#
exit #退出容器
[root@c549e8ba2456 /]# exit
exit
[root@Shyee ~]#

#后台启动容器
docker run -d 容器名
#运行结果
[root@Shyee ~]# docker run -d centos
c8d9334aae4794a0d6456207fd5d52deb7328190f3059e6b74d90be64616e3e9
[root@Shyee ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
#启动后会被停掉
#因为docker把容器后台启动后,发现前台没有容器正在运行,认为没有容器,所以就把后台的容器给停掉了

可加载项

Name, shorthand Default Description
--add-host Add a custom host-to-IP mapping (host:ip)
--attach , -a Attach to STDIN, STDOUT or STDERR
--blkio-weight Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--blkio-weight-device Block IO weight (relative device weight)
--cap-add Add Linux capabilities
--cap-drop Drop Linux capabilities
--cgroup-parent Optional parent cgroup for the container
--cgroupns API 1.41+ Cgroup namespace to use (host|private) ‘host’: Run the container in the Docker host’s cgroup namespace ‘private’: Run the container in its own private cgroup namespace ‘’: Use the cgroup namespace as configured by the default-cgroupns-mode option on the daemon (default)
--cidfile Write the container ID to the file
--cpu-count CPU count (Windows only)
--cpu-percent CPU percent (Windows only)
--cpu-period Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota Limit CPU CFS (Completely Fair Scheduler) quota
--cpu-rt-period API 1.25+ Limit CPU real-time period in microseconds
--cpu-rt-runtime API 1.25+ Limit CPU real-time runtime in microseconds
--cpu-shares , -c CPU shares (relative weight)
--cpus API 1.25+ Number of CPUs
--cpuset-cpus CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems MEMs in which to allow execution (0-3, 0,1)
--detach , -d Run container in background and print container ID
--detach-keys Override the key sequence for detaching a container
--device Add a host device to the container
--device-cgroup-rule Add a rule to the cgroup allowed devices list
--device-read-bps Limit read rate (bytes per second) from a device
--device-read-iops Limit read rate (IO per second) from a device
--device-write-bps Limit write rate (bytes per second) to a device
--device-write-iops Limit write rate (IO per second) to a device
--disable-content-trust true Skip image verification
--dns Set custom DNS servers
--dns-opt Set DNS options
--dns-option Set DNS options
--dns-search Set custom DNS search domains
--domainname Container NIS domain name
--entrypoint Overwrite the default ENTRYPOINT of the image
--env , -e Set environment variables
--env-file Read in a file of environment variables
--expose Expose a port or a range of ports
--gpus API 1.40+ GPU devices to add to the container (‘all’ to pass all GPUs)
--group-add Add additional groups to join
--health-cmd Command to run to check health
--health-interval Time between running the check (ms|s|m|h) (default 0s)
--health-retries Consecutive failures needed to report unhealthy
--health-start-period API 1.29+ Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) (default 0s)
--health-timeout Maximum time to allow one check to run (ms|s|m|h) (default 0s)
--help Print usage
--hostname , -h Container host name
--init API 1.25+ Run an init inside the container that forwards signals and reaps processes
--interactive , -i Keep STDIN open even if not attached
--io-maxbandwidth Maximum IO bandwidth limit for the system drive (Windows only)
--io-maxiops Maximum IOps limit for the system drive (Windows only)
--ip IPv4 address (e.g., 172.30.100.104)
--ip6 IPv6 address (e.g., 2001:db8::33)
--ipc IPC mode to use
--isolation Container isolation technology
--kernel-memory Kernel memory limit
--label , -l Set meta data on a container
--label-file Read in a line delimited file of labels
--link Add link to another container
--link-local-ip Container IPv4/IPv6 link-local addresses
--log-driver Logging driver for the container
--log-opt Log driver options
--mac-address Container MAC address (e.g., 92:d0:c6:0a:29:33)
--memory , -m Memory limit
--memory-reservation Memory soft limit
--memory-swap Swap limit equal to memory plus swap: ‘-1’ to enable unlimited swap
--memory-swappiness -1 Tune container memory swappiness (0 to 100)
--mount Attach a filesystem mount to the container
--name Assign a name to the container
--net Connect a container to a network
--net-alias Add network-scoped alias for the container
--network Connect a container to a network
--network-alias Add network-scoped alias for the container
--no-healthcheck Disable any container-specified HEALTHCHECK
--oom-kill-disable Disable OOM Killer
--oom-score-adj Tune host’s OOM preferences (-1000 to 1000)
--pid PID namespace to use
--pids-limit Tune container pids limit (set -1 for unlimited)
--platform API 1.32+ Set platform if server is multi-platform capable
--privileged Give extended privileges to this container
--publish , -p Publish a container’s port(s) to the host
--publish-all , -P Publish all exposed ports to random ports
--pull missing Pull image before running (“always”|”missing”|”never”)
--read-only Mount the container’s root filesystem as read only
--restart no Restart policy to apply when a container exits
--rm Automatically remove the container when it exits
--runtime Runtime to use for this container
--security-opt Security Options
--shm-size Size of /dev/shm
--sig-proxy true Proxy received signals to the process
--stop-signal SIGTERM Signal to stop a container
--stop-timeout API 1.25+ Timeout (in seconds) to stop a container
--storage-opt Storage driver options for the container
--sysctl Sysctl options
--tmpfs Mount a tmpfs directory
--tty , -t Allocate a pseudo-TTY
--ulimit Ulimit options
--user , -u Username or UID (format: <name|uid>[:<group|gid>])
--userns User namespace to use
--uts UTS namespace to use
--volume , -v Bind mount a volume
--volume-driver Optional volume driver for the container
--volumes-from Mount volumes from the specified container(s)
--workdir , -w Working directory inside the container

docker ps

1
2
3
docker ps #查看正在运行的容器
docker ps -a #查看运行过的容器
docker ps -a -n=? #显示最近?个容器

退出

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
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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:37) or relative (e.g. 42m for 42 minutes)
--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:37) or relative (e.g. 42m for 42 minutes)
#运行一个容器,让他不停打印
[root@Shyee ~]# docker run -d centos /bin/sh -c "while true;do echo Shyee;sleep 1;done"
936f798488f4afb85055dea3d75c40703b827823467af54f0ee20834f2298760
[root@Shyee ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
936f798488f4 centos "/bin/sh -c 'while t…" 9 seconds ago Up 8 seconds laughing_feistel
[root@Shyee ~]# docker logs -tf --tail 10 936f798488f4
2021-01-20T04:17:47.958314992Z Shyee
2021-01-20T04:17:48.960104271Z Shyee
2021-01-20T04:17:49.962248118Z Shyee
2021-01-20T04:17:50.964089661Z Shyee
2021-01-20T04:17:51.965808705Z Shyee
2021-01-20T04:17:52.967578590Z Shyee
2021-01-20T04:17:53.969388856Z Shyee
2021-01-20T04:17:54.971137786Z Shyee
2021-01-20T04:17:55.972977481Z Shyee
2021-01-20T04:17:56.974827278Z Shyee
2021-01-20T04:17:57.976766489Z Shyee
#一直会刷新

查看docker容器内部信息

1
2
3
4
5
docker top
[root@Shyee ~]# docker top a87e6c731659
UID PID PPID C STIME TTY TIME CMD
root 101771 101755 1 12:22 ? 00:00:00 /bin/sh -c while true;do echo Shyee;sleep 1;done
root 101826 101771 0 12:22 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1

查看容器的信息

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
docker inspect
[root@Shyee ~]# docker inspect a87e6c731659
[
{
"Id": "a87e6c731659ecff036fac2cede73e586466f15bbc026f5a1f3d106bc990a38a",
"Created": "2021-01-20T04:22:02.208432632Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo Shyee;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 101771,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-01-20T04:22:02.751586945Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/a87e6c731659ecff036fac2cede73e586466f15bbc026f5a1f3d106bc990a38a/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/a87e6c731659ecff036fac2cede73e586466f15bbc026f5a1f3d106bc990a38a/hostname",
"HostsPath": "/var/lib/docker/containers/a87e6c731659ecff036fac2cede73e586466f15bbc026f5a1f3d106bc990a38a/hosts",
"LogPath": "/var/lib/docker/containers/a87e6c731659ecff036fac2cede73e586466f15bbc026f5a1f3d106bc990a38a/a87e6c731659ecff036fac2cede73e586466f15bbc026f5a1f3d106bc990a38a-json.log",
"Name": "/dazzling_matsumoto",
"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/24893cfd3f87c08a9366dac239c9fd5b02fea33a192fcc3f0c243a671c4da2c8-init/diff:/var/lib/docker/overlay2/f660b0f563e2db7f1e4e7c42e598560f1c25d2e10d534e781bfb2ccd45e9b38b/diff",
"MergedDir": "/var/lib/docker/overlay2/24893cfd3f87c08a9366dac239c9fd5b02fea33a192fcc3f0c243a671c4da2c8/merged",
"UpperDir": "/var/lib/docker/overlay2/24893cfd3f87c08a9366dac239c9fd5b02fea33a192fcc3f0c243a671c4da2c8/diff",
"WorkDir": "/var/lib/docker/overlay2/24893cfd3f87c08a9366dac239c9fd5b02fea33a192fcc3f0c243a671c4da2c8/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "a87e6c731659",
"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 Shyee;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": "b1519a918616f880d5139e041386bce0c0a2e49c4330750c5f4ca8da34cae7e9",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/b1519a918616",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "1b101194ef0bb0cb2990db0acfa26a50e9dd20d10a0f633993ab1ef7a8b4ff98",
"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": "a414e1e71fc0c80a2484ca0c656819a0532ccc902b70b9123a07dab25baaf04a",
"EndpointID": "1b101194ef0bb0cb2990db0acfa26a50e9dd20d10a0f633993ab1ef7a8b4ff98",
"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
docker exec -it 容器ID

[root@Shyee ~]# docker exec -it a87e6c731659 /bin/bash
[root@a87e6c731659 /]#
[root@a87e6c731659 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@a87e6c731659 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 04:22 ? 00:00:00 /bin/sh -c while true;do echo Shyee;sleep 1;done
root 396 0 0 04:28 pts/0 00:00:00 /bin/bash
root 464 1 0 04:29 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root 465 396 0 04:29 pts/0 00:00:00 ps -ef

#另一种方法
docker attach 容器ID
[root@Shyee ~]# docker attach a87e6c731659

拷贝

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
#从容器内拷到容器外
docker cp
[root@Shyee ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f 6 weeks ago 209MB
#进入容器
[root@Shyee ~]# docker run -it centos /bin/bash
[root@112cbae27efa /]# cd /home
[root@112cbae27efa home]# ls
#创建测试文件
[root@112cbae27efa home]# touch a.txt
[root@112cbae27efa home]# ls
a.txt
#退出容器
[root@112cbae27efa home]# exit
exit
[root@Shyee local]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@Shyee local]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
112cbae27efa centos "/bin/bash" 4 minutes ago Exited (0) 14 seconds ago laughing_leakey
#将测试文件拷贝出来
[root@Shyee local]# docker cp 112cbae27efa:/home/a.txt /usr/local
[root@Shyee local]# ls
aegis a.txt bin blog etc games include jdk-15.0.1 lib lib64 libexec nginx nginx-1.19.6 sbin share src

docker 部署nginx

查找并下载容器

1
2
docker search nginx
docker pull 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
#-d 后台启动
#将25565映射到容器内的80
[root@Shyee local]# docker run -d --name nginx01 -p 25565:80 nginx
6cf85750f6e82fbeaffd84e90c28cdcbbc91e70a2771ff1ea81cba9f35281c33
[root@Shyee local]# curl localhost:25565
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

进入nginx

1
2
3
4
5
6
[root@Shyee local]# docker exec -it nginx01 /bin/bash
root@6cf85750f6e8:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@6cf85750f6e8:/# cd etc/nginx
root@6cf85750f6e8:/etc/nginx# ls
conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf

dockers部署tomcat

官方方式

1
2
$ docker run -it --rm tomcat:9.0
#--rm 适用于测试,用完即关
1
2
3
4
5
6
7
8
9
[root@Shyee local]# docker run -d -p 19132:8080 --name tomcat01 tomcat
#测试报404
#进入tomcat01
[root@Shyee local]# docker exec -it tomcat01 /bin/bash
root@b1085cdbfb07:/usr/local/tomcat# ls
BUILDING.txt CONTRIBUTING.md LICENSE NOTICE README.md RELEASE-NOTES RUNNING.txt bin conf lib logs native-jni-lib temp webapps webapps.dist work
root@b1085cdbfb07:/usr/local/tomcat# cd webapps
root@b1085cdbfb07:/usr/local/tomcat/webapps# ls
#webapp是空的

webapps原有的文件都在 webapps.dist 中,所以把webapps.dist中的文件拷到webapps里。

1
2
3
4
5
root@b1085cdbfb07:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@b1085cdbfb07:/usr/local/tomcat# cd webapps
root@b1085cdbfb07:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager
# 测试通过

部署es+kibana

es暴露的端口很多!

es十分的耗内存

es的数据一般需要放置到安全目录!挂载

官方文档

1
$ docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.10.1

卡死了。

限制内存

1
$ docker run -d --name elasticsearch02 -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node"  -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.10.1
1
2
3
4
5
6
7
[root@Shyee ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
22dcdeaf2f79 elasticsearch:7.10.1 "/tini -- /usr/local…" 7 seconds ago Up 6 seconds 0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp elasticsearch02
[root@iZ2zeeg68odufzgdsoyyujZ ~]# docker stats

CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
22dcdeaf2f79 elasticsearch02 5.87% 358.7MiB / 1.774GiB 19.74% 1.1kB / 0B 162MB / 0B 31

检查

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@Shyee ~]# curl localhost:9200
{
"name" : "22dcdeaf2f79",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "_YJ5sKDETeqelK3n1v2U_w",
"version" : {
"number" : "7.10.1",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "1c34507e66d7db1211f66f3513706fdf548736aa",
"build_date" : "2020-12-05T01:00:33.671820Z",
"build_snapshot" : false,
"lucene_version" : "8.7.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}

可视化

portainer

1
docker run -d -p 8088:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer

从浏览器链接8088端口进入

选择local

![image-20210221204835330](D:\bloglocal\Docker 常用命令.assets\image-20210221204835330.png)

会进入到这种页面

![image-20210221205209445](D:\bloglocal\Docker 常用命令.assets\image-20210221205209445.png)

commit镜像

1
2
docker commit #提交容器为一个新的副本
docker commit -m="提交的描述信息" -a="作者" 容器id 目标镜像名

测试

1
2
3
4
5
6
7
8
9
10
11
#启动一个默认tomcat,
docker run -it -p 8080:8080 tomcat
docker exec -it 5fea3e1cfa5a /bin/bash
root@5fea3e1cfa5a:/usr/local/tomcat# cd webapps
root@5fea3e1cfa5a:/usr/local/tomcat/webapps# ls
#发现webapps中没有东西(都在webapps.dist中)
#将其拷贝到webapps中
root@5fea3e1cfa5a:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@5fea3e1cfa5a:/usr/local/tomcat# cd webapps
root@5fea3e1cfa5a:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager

进行提交

1
2
3
4
5
[root@Shyee ~]# docker commit -a ="Shyee" -m="init tomcat done" 5fea3e1cfa5a tomcat02:1.0
sha256:75830e7057278f119161e5e0745c90fc9d58ea475476d64d50d522186ce58df4
[root@Shyee ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat02 1.0 75830e705727 6 seconds ago 654MB