1
mac@MacdeMacBook-Pro ~ % 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" 容器名字 用来区分容器
-it 使用交互的方式运行 进入容器查看内容
-d 后台方式运行
-p
-p ip:主机端口:容器端口
-p 主机端口:容器端口
-p 容器端口
容器端口
-P 随机指定端口

# 测试,启动并进入容器
mac@MacdeMacBook-Pro ~ % docker run -it centos /bin/bash
[root@084a638abcc7 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var

#从容器中返回主机
[root@084a638abcc7 /]# exit
exit

### 列出所有的运行的容器
1
2
3
4
5
6
7
8
9
10
11
# docker ps 命令
列出当前正在运行的容器
-a #列出当前正在运行的容器和历史运行过的容器
-n=? #显示最近创建的容器
-q #只显示容器的编号
mac@MacdeMacBook-Pro ~ % docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
084a638abcc7 centos "/bin/bash" 4 minutes ago Exited (0) About a minute ago hardcore_panini
0b4e3e7c0b35 hello-world "/hello" 43 minutes ago Exited (0) 43 minutes ago boring_tereshkova
mac@MacdeMacBook-Pro ~ % docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
### 退出容器
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
#命令 docker -d 镜像名
mac@MacdeMacBook-Pro ~ % docker run -d centos

### 查看日志
1
2
3
4
docker logs  -ft --tail
#显示日志
-tf #显示日志
--tail number #要显示的日志条数
### 查看容器中的进程信息
1
2
3
4
5
6
docker top 容器Id
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9a5f11734277 centos "/bin/bash" 11 seconds ago Up 10 seconds agitated_bartik
mac@MacdeMacBook-Pro ~ % docker top 9a5f11734277
UID PID PPID C STIME TTY TIME CMD
root 3403 3375 0 20:45 ? 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
# 命令 docker inspect 容器Id
mac@MacdeMacBook-Pro ~ % docker inspect 9a5f11734277
[
{
"Id": "9a5f11734277b852ff8863949b81bfed74c9f3e8d4a74d3be2b1d4dffeae0e1a",
"Created": "2021-05-22T20:45:40.317199885Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 3403,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-05-22T20:45:40.706704339Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/9a5f11734277b852ff8863949b81bfed74c9f3e8d4a74d3be2b1d4dffeae0e1a/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/9a5f11734277b852ff8863949b81bfed74c9f3e8d4a74d3be2b1d4dffeae0e1a/hostname",
"HostsPath": "/var/lib/docker/containers/9a5f11734277b852ff8863949b81bfed74c9f3e8d4a74d3be2b1d4dffeae0e1a/hosts",
"LogPath": "/var/lib/docker/containers/9a5f11734277b852ff8863949b81bfed74c9f3e8d4a74d3be2b1d4dffeae0e1a/9a5f11734277b852ff8863949b81bfed74c9f3e8d4a74d3be2b1d4dffeae0e1a-json.log",
"Name": "/agitated_bartik",
"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,
"CgroupnsMode": "host",
"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/77cac635569126a99c0af94eeda336c61ebf5c62885647a6a29bd2cef679886b-init/diff:/var/lib/docker/overlay2/f065e606298dfdf6bb02a024818a4e4c062fe0b1c343e040293edc3bb04de86f/diff",
"MergedDir": "/var/lib/docker/overlay2/77cac635569126a99c0af94eeda336c61ebf5c62885647a6a29bd2cef679886b/merged",
"UpperDir": "/var/lib/docker/overlay2/77cac635569126a99c0af94eeda336c61ebf5c62885647a6a29bd2cef679886b/diff",
"WorkDir": "/var/lib/docker/overlay2/77cac635569126a99c0af94eeda336c61ebf5c62885647a6a29bd2cef679886b/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "9a5f11734277",
"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": "eedb30699a235b7f08f3a3680ea2da8eeeb07333e6028fcf70b5c637215c4c62",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/eedb30699a23",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "7cb260cd88c48365ac016cea45c0fc7ee0111f94606e41628d18f2a3da1a6851",
"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": "d24fab5689b36900802fec22ff27f250bf9cd480a498953715d4e6200e707081",
"EndpointID": "7cb260cd88c48365ac016cea45c0fc7ee0111f94606e41628d18f2a3da1a6851",
"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
docker exec -it 9a5f11734277 /bin/bash  #进入容器侯开启一个新的终端
docker attach 9a5f11734277 #进入容器正在使用的终端,不会启动新的进程
### 从容器内拷贝文件到主机上
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
docker cp 容器id:容器内路径 目的主机路径

#进入docker容器内部
mac@MacdeMacBook-Pro ~ % docker exec -it 9a5f11734277 /bin/bash
[root@9a5f11734277 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@9a5f11734277 /]# cd /home/
[root@9a5f11734277 home]# ls
#创建文件
[root@9a5f11734277 home]# touch test.java
[root@9a5f11734277 home]# ls
test.java
[root@9a5f11734277 home]# exit
exit
#拷贝容器到主机上
mac@MacdeMacBook-Pro ~ % docker cp 9a5f11734277:/home/test.java /Users/mac
mac@MacdeMacBook-Pro ~ % ls /Users/mac
Applications Downloads Postman getting-started
BuildTools Library Projects logs
Desktop Movies Public temp
DevEcoStudioProjects Music Share test.java
Documents Pictures dump.rdb