运行一个web应用
在docker容器中运行一个Python Flask应用
1 | $ docker run -d -P training/webapp python app.py |
- -d:让容器在后台运行。
- -P:将容器内部使用的网络端口映射到我们使用的主机上。可以指定如 -p 5000:5000
查看容器状态,Docker开放了5000 端口(默认 Python Flask 端口)映射到主机端口32769 上
1
2
3$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
069a387381ee training/webapp "python app.py" About a minute ago Up About a minute 0.0.0.0:32769->5000/tcp elastic_austin1
2$ docker run -d -p 5000:5000 training/webapp python app.py
cdbdd07a3c088237c15630216fd98e6e7cab74c6f96d80445814b0641e258e221
2
3
4$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cdbdd07a3c08 training/webapp "python app.py" 7 seconds ago Up 6 seconds 0.0.0.0:5000->5000/tcp cocky_nightingale
069a387381ee training/webapp "python app.py" 5 minutes ago Up 5 minutes 0.0.0.0:32769->5000/tcp elastic_austin查看容器端口
1
2
3
4
5$ docker port cdbdd07a3c08
5000/tcp -> 0.0.0.0:5000
$ docker port cocky_nightingale
5000/tcp -> 0.0.0.0:5000浏览器访问和查看后台日志
1
2
3
4$ docker logs -f elastic_austin
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
172.17.0.1 - - [25/Apr/2017 09:53:34] "GET / HTTP/1.1" 200 -
172.17.0.1 - - [25/Apr/2017 09:53:35] "GET /favicon.ico HTTP/1.1" 404 -查看WEB应用程序容器中的进程
1
2
3$ docker top elastic_austin
PID USER TIME COMMAND
5629 root 0:06 python app.py检查WEB应用程序
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$ docker inspect elastic_austin
[
{
"Id": "069a387381ee5b218de850023e6fc9160b82402a06087f196049c56186c1b658",
"Created": "2017-04-25T04:27:56.868358836Z",
"Path": "python",
"Args": [
"app.py"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 5629,
"ExitCode": 0,
"Error": "",
"StartedAt": "2017-04-25T04:27:58.496773504Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:6fae60ef344644649a39240b94d73b8ba9c67f898ede85cf8e947a887b3e6557",
"ResolvConfPath": "/var/lib/docker/containers/069a387381ee5b218de850023e6fc9160b82402a06087f196049c56186c1b658/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/069a387381ee5b218de850023e6fc9160b82402a06087f196049c56186c1b658/hostname",
"HostsPath": "/var/lib/docker/containers/069a387381ee5b218de850023e6fc9160b82402a06087f196049c56186c1b658/hosts",
"LogPath": "/var/lib/docker/containers/069a387381ee5b218de850023e6fc9160b82402a06087f196049c56186c1b658/069a387381ee5b218de850023e6fc9160b82402a06087f196049c56186c1b658-json.log",
"Name": "/elastic_austin",
"RestartCount": 0,
"Driver": "aufs",
"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,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": true,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"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": [],
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": -1,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0
},
"GraphDriver": {
"Name": "aufs",
"Data": null
},
"Mounts": [],
"Config": {
"Hostname": "069a387381ee",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"5000/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"python",
"app.py"
],
"Image": "training/webapp",
"Volumes": null,
"WorkingDir": "/opt/webapp",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "b3e41e0db8241c4343d293e0ad9042d115781366c95af4c9485afefd4a95c14f",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"5000/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "32769"
}
]
},
"SandboxKey": "/var/run/docker/netns/b3e41e0db824",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "2c1b0818134f62205c3413aa58a40a8d8d1bfa49c22392d4759a7d1cf59f0f90",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "45dfed77714f8c6e748e8d910dd6979b40214339b948b26b212291c728441b40",
"EndpointID": "2c1b0818134f62205c3413aa58a40a8d8d1bfa49c22392d4759a7d1cf59f0f90",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03"
}
}
}
}
]停止(stop)和重启
1
2$ docker start elastic_austin
elastic_austin移除WEB应用容器【先停止】
1
2
3
4
5
6
7
8$ docker rm elastic_austin
Error response from daemon: You cannot remove a running container 069a387381ee5b218de850023e6fc9160b82402a06087f196049c56186c1b658. Stop the container before attempting removal or use -f
$ docker stop elastic_austin
elastic_austin
$ docker rm elastic_austin
elastic_austin