通过上文我们已经基本了解了docker的结构(仓库,镜像,容器)以及跑docker应用了;本文将通过介绍一个web应用:向你展示如何进行主机与web容器之间的通信,这是web开发者常用的;第二,贯穿上文中内容, 且为我们后续讲解网络提供基础。
一个web 应用运行和访问
运行一个 web 应用
我们只需要找一个webapp的镜像即可,无需自己再写个程序啥的。接下来让我们尝试使用 docker 构建一个 web 应用程序。可以用docker search webapp查找
这里找了一个training/webapp
,它是将在docker容器中运行一个 Python Flask web应用。
root@VM-16-2-ubuntu:/etc/docker# docker pull training/webapp
Using default tag: latest
latest: Pulling from training/webapp
[DEPRECATION NOTICE] Docker Image Format v1, and Docker Image manifest version 2, schema 1 support will be removed in an upcoming release. Suggest the author of docker.io/training/webapp:latest to upgrade the image to the OCI Format, or Docker Image manifest v2, schema 2. More information at https://docs.docker.com/go/deprecated-image-specs/
e190868d63f8: Downloading
909cd34c6fd7: Download complete
0b9bfabab7c1: Download complete
a3ed95caeb02: Downloading
10bbbc0fc0ff: Downloading
fca59b508e9f: Downloading
e7ae2541b15b: Downloading
9dd97ef58ce9: Downloading
a4c1b0cb7af7: Downloading
latest: Pulling from training/webapp
[DEPRECATION NOTICE] Docker Image Format v1, and Docker Image manifest version 2, schema 1 support will be removed in an upcoming release. Suggest the author of docker.io/training/webapp:latest to upgrade the image to the OCI Format, or Docker Image manifest v2, schema 2. More information at https://docs.docker.com/go/deprecated-image-specs/
e190868d63f8: Pull complete
909cd34c6fd7: Pull complete
0b9bfabab7c1: Pull complete
a3ed95caeb02: Pull complete
10bbbc0fc0ff: Pull complete
fca59b508e9f: Pull complete
e7ae2541b15b: Pull complete
9dd97ef58ce9: Pull complete
a4c1b0cb7af7: Pull complete
Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
Status: Downloaded newer image for training/webapp:latest
docker.io/training/webapp:latest
root@VM-16-2-ubuntu:/etc/docker# docker images | grep webapp
training/webapp latest 6fae60ef3446 9 years ago 349MB
启动这个webapp
root@VM-16-2-ubuntu:/etc/docker# docker run -d -P training/webapp python app.py
52e082e91619ea7fe2e5fab639c77d4d482e44cb58921617c3063b19e0e7a79b
root@VM-16-2-ubuntu:/etc/docker# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
52e082e91619 training/webapp "python app.py" 9 seconds ago Up 7 seconds 0.0.0.0:32768->5000/tcp, :::32768->5000/tcp modest_payne
访问webapp
我们注意看上述PORTS部分为0.0.0.0:32769->5000/tcp
Docker 开放了 5000 端口(默认 Python Flask 端口)映射到主机端口 32768 上。
- 从主机上看,它应该暴露了端口32768
所以我们来验证下:
root@VM-16-2-ubuntu:/etc/docker# netstat -ant |grep 32768
tcp 0 0 0.0.0.0:32768 0.0.0.0:* LISTEN
tcp6 0 0 :::32768 :::* LISTEN
root@VM-16-2-ubuntu:/etc/docker# curl localhost:32768
Hello world!
- 从容器内部看,它应该有一个端口5000
我们再进容器验证下:
root@VM-16-2-ubuntu:/etc/docker# docker exec -it 52e082e91619 sh
# netstat -ant | grep 5000
tcp 0 0 0.0.0.0:5000 0.0.0.0:* LISTEN
设置自定义映射端口
通过 -p 参数可以设置映射的端口
root@VM-16-2-ubuntu:/home/lighthouse# docker run -d -p 5001:5000 training/webapp python app.py
6a84afe284705ab58ba4667ea1d81dc0b79dfec0e32bd3555dd31a6fc8f47403
root@VM-16-2-ubuntu:/home/lighthouse# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6a84afe28470 training/webapp "python app.py" 11 seconds ago Up 10 seconds 0.0.0.0:5001->5000/tcp, :::5001->5000/tcp friendly_mendeleev
52e082e91619 training/webapp "python app.py" 4 hours ago Up 4 hours 0.0.0.0:32768->5000/tcp, :::32768->5000/tcp modest_payne
看到跑了两个内部端口都是5000的web实例,新的容器内部的 5000 端口映射到我们本地主机的 5001 端口上。
其他常用功能
查看web应用网络端口
通过 docker ps 命令可以查看到容器的端口映射,docker 还提供了另一个快捷方式 docker port,使用 docker port 可以查看指定 (ID 或者名字)容器的某个确定端口映射到宿主机的端口号。
root@VM-16-2-ubuntu:/home/lighthouse# docker port 6a84afe28470
5000/tcp -> 0.0.0.0:5001
5000/tcp -> [::]:5001
root@VM-16-2-ubuntu:/home/lighthouse# docker port friendly_mendeleev
5000/tcp -> 0.0.0.0:5001
5000/tcp -> [::]:5001
查看应用的日志
docker logs [ID或者名字] 可以查看容器内部的标准输出。-f参数表示持续输出日志
root@VM-16-2-ubuntu:/home/lighthouse# docker logs -f 6a84afe28470
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
查看应用程序容器的进程
docker top
root@VM-16-2-ubuntu:/home/lighthouse# docker top 6a84afe28470
UID PID PPID C STIME TTY TIME CMD
root 1237566 1237543 0 15:36 ? 00:00:00 python app.py
检查应用程序
使用 docker inspect 来查看 Docker 的底层信息。它会返回一个 JSON 文件记录着 Docker 容器的配置和状态信息
root@VM-16-2-ubuntu:/home/lighthouse# docker inspect 6a84afe28470
[
{
"Id": "6a84afe284705ab58ba4667ea1d81dc0b79dfec0e32bd3555dd31a6fc8f47403",
"Created": "2024-09-25T07:36:04.84056488Z",
"Path": "python",
"Args": [
"app.py"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 1237566,
"ExitCode": 0,
"Error": "",
"StartedAt": "2024-09-25T07:36:05.226876582Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:6fae60ef344644649a39240b94d73b8ba9c67f898ede85cf8e947a887b3e6557",
"ResolvConfPath": "/var/lib/docker/containers/6a84afe284705ab58ba4667ea1d81dc0b79dfec0e32bd3555dd31a6fc8f47403/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/6a84afe284705ab58ba4667ea1d81dc0b79dfec0e32bd3555dd31a6fc8f47403/hostname",
"HostsPath": "/var/lib/docker/containers/6a84afe284705ab58ba4667ea1d81dc0b79dfec0e32bd3555dd31a6fc8f47403/hosts",
"LogPath": "/var/lib/docker/containers/6a84afe284705ab58ba4667ea1d81dc0b79dfec0e32bd3555dd31a6fc8f47403/6a84afe284705ab58ba4667ea1d81dc0b79dfec0e32bd3555dd31a6fc8f47403-json.log",
"Name": "/friendly_mendeleev",
"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": {
"5000/tcp": [
{
"HostIp": "",
"HostPort": "5001"
}
]
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"ConsoleSize": [
22,
205
],
"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/d2576d793b09121959a91fddc18e320aa2119e4294cce829ed79cf1cbd89bc3f-init/diff:/var/lib/docker/overlay2/fe66cf8642de55d1d5d5d902b1e78a9f8d31476559f52a844bc140063e7092b2/diff:/var/lib/docker/overlay2/eac679abb2fd6cf2118f0d1faa01bfb09861aa473f59fc9f52ce56d79bd67849/diff:/var/lib/docker/overlay2/50c6f3af9824a55852a7056292ca48b1cdd217a4d09659d8a1046a4154cae94b/diff:/var/lib/docker/overlay2/8512382651ea9079ab8bd217be66f98d52548dde028d80f61d8427c4d78ec9eb/diff:/var/lib/docker/overlay2/4ded61d8bdc15a967fcb1784d5dae977dc201d72d71a27bd4dc5cbe775f1f947/diff:/var/lib/docker/overlay2/07db5830024f3fcd2b9e690ae2a9da03e41ddbf87d2c866eda50f15ff12161f8/diff:/var/lib/docker/overlay2/b2229399efaf652aa944d8f3c72ce8a30442043fd7d3927c61dd2d77d9cac18e/diff:/var/lib/docker/overlay2/5d8d1a9aa0fdc5716bf3e2a13a6abfe63592668338a9cf4173cb26d6d5fa91e9/diff:/var/lib/docker/overlay2/2de862c54d0ea06f37f0383f5114b55962464c2431176b33c6c7f9b1ce25265f/diff:/var/lib/docker/overlay2/4b49e6c2207be6a4e05bd535bda8628f7123a2c734022be7b766f892ece71e8d/diff:/var/lib/docker/overlay2/99b8ed3ad45eb2164ae24e64529a17ba77f8d4b899ff399b4a280e70cc1a191c/diff:/var/lib/docker/overlay2/48f4fce7e56a82dbb8feb3aba464fcb3adbab4be9d74ddaf78c9c7ea37527e9e/diff:/var/lib/docker/overlay2/d54ff6dd77f7ce2ad674c4aa2160fcc02b66431aa65c06eba1bd3fc47fdb5f12/diff",
"MergedDir": "/var/lib/docker/overlay2/d2576d793b09121959a91fddc18e320aa2119e4294cce829ed79cf1cbd89bc3f/merged",
"UpperDir": "/var/lib/docker/overlay2/d2576d793b09121959a91fddc18e320aa2119e4294cce829ed79cf1cbd89bc3f/diff",
"WorkDir": "/var/lib/docker/overlay2/d2576d793b09121959a91fddc18e320aa2119e4294cce829ed79cf1cbd89bc3f/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "6a84afe28470",
"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": "633cb8961970fcf194a2705cc1e659cc48e0484e6ac5bc70d6691c324af3ca45",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"5000/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "5001"
},
{
"HostIp": "::",
"HostPort": "5001"
}
]
},
"SandboxKey": "/var/run/docker/netns/633cb8961970",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "dc0f9ead1c1eec705868eaa3316883a3fe44d31be9eb6b7e810c15874cc20b6b",
"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": "e7cb0b1c9f4043e23fa9f2a75d58b4bff052ab142be3bc0e5fe961abec6e09f1",
"EndpointID": "dc0f9ead1c1eec705868eaa3316883a3fe44d31be9eb6b7e810c15874cc20b6b",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
}
]
停止应用容器
root@VM-16-2-ubuntu:/home/lighthouse# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6a84afe28470 training/webapp "python app.py" 8 minutes ago Up 8 minutes 0.0.0.0:5001->5000/tcp, :::5001->5000/tcp friendly_mendeleev
52e082e91619 training/webapp "python app.py" 4 hours ago Up 4 hours 0.0.0.0:32768->5000/tcp, :::32768->5000/tcp modest_payne
root@VM-16-2-ubuntu:/home/lighthouse#
root@VM-16-2-ubuntu:/home/lighthouse#
root@VM-16-2-ubuntu:/home/lighthouse#
root@VM-16-2-ubuntu:/home/lighthouse# docker stop 6a84afe28470
6a84afe28470
root@VM-16-2-ubuntu:/home/lighthouse# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
52e082e91619 training/webapp "python app.py" 4 hours ago Up 4 hours 0.0.0.0:32768->5000/tcp, :::32768->5000/tcp modest_payne
重启应用容器
root@VM-16-2-ubuntu:/home/lighthouse# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6a84afe28470 training/webapp "python app.py" 9 minutes ago Exited (137) 54 seconds ago friendly_mendeleev
52e082e91619 training/webapp "python app.py" 4 hours ago Up 4 hours 0.0.0.0:32768->5000/tcp, :::32768->5000/tcp modest_payne
603b6ae9191b ubuntu:v1.0.2 "sh" 40 hours ago Exited (0) 40 hours ago youthful_driscoll
3a429f4c76a5 b1e9cef3f297 "/bin/bash" 40 hours ago Exited (130) 40 hours ago gracious_shaw
4b7423cdafdf b1e9cef3f297 "/bin/bash" 40 hours ago Exited (0) 40 hours ago flamboyant_proskuriakova
root@VM-16-2-ubuntu:/home/lighthouse# docker restart 6a84afe28470
6a84afe28470
root@VM-16-2-ubuntu:/home/lighthouse# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6a84afe28470 training/webapp "python app.py" 10 minutes ago Up 3 seconds 0.0.0.0:5001->5000/tcp, :::5001->5000/tcp friendly_mendeleev
52e082e91619 training/webapp "python app.py" 4 hours ago Up 4 hours 0.0.0.0:32768->5000/tcp, :::32768->5000/tcp modest_payne
移除应用容器
root@VM-16-2-ubuntu:/home/lighthouse# docker ps -a |grep friendly
6a84afe28470 training/webapp "python app.py" 11 minutes ago Up About a minute 0.0.0.0:5001->5000/tcp, :::5001->5000/tcp friendly_mendeleev
root@VM-16-2-ubuntu:/home/lighthouse# docker rm -f 6a84afe28470
6a84afe28470
root@VM-16-2-ubuntu:/home/lighthouse# docker ps -a |grep friendly