关键理解:
默认 bridge 网络:容器 IP 是 172.17.0.x,宿主机无法直接访问容器端口,必须用
-p 8080:80做端口映射自定义 bridge 网络:容器间可以通信,但外界访问仍需端口映射
为什么刚才的 my-net 容器不能从外部访问?
因为它们没有
-p参数,宿主机不知道如何转发流量
第 22 步操作:测试端口映射 + 自定义网络
1. 运行一个 Nginx 容器在自定义网络上,并映射端口
docker run -d --name nginx-custom --network my-net -p 8080:80 nginx
2. 在另一个容器内访问 Nginx(通过容器名)
docker run --rm --network my-net alpine wget -qO- http://nginx-custom
3. 从宿主机访问(通过映射的端口)
curl http://localhost:8080
4. 清理测试容器
docker stop nginx-custom ping-box-1 ping-box-2 ping-box-3 ping-box-4
docker rm nginx-custom ping-box-1 ping-box-2 ping-box-3 ping-box-4
root@docker-test:~# docker run -d --name nginx-custom --network my-net -p 8080:80 nginx
25f8d27fc321f7ccac0a10cdc09b8b0da1ab38979f8ab59d8eecee0947c1aab3
root@docker-test:~# docker network ls
NETWORK ID NAME DRIVER SCOPE
19dfe34b0f0a app-network bridge local
4a4b5163ad07 bridge bridge local
f536a0a68643 host host local
644572449ffb my-net bridge local
41df356c148c none null local
root@docker-test:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
25f8d27fc321 nginx "/docker-entrypoint.…" 26 seconds ago Up 25 seconds 0.0.0.0:8080->80/tcp nginx-custom
de7720518172 alpine "sleep infinity" 13 minutes ago Up 13 minutes ping-box-4
292eafedaeba alpine "sleep infinity" 13 minutes ago Up 13 minutes ping-box-3
230658b18cb4 alpine "sleep infinity" 14 minutes ago Up 14 minutes ping-box-2
053f6d5f720d alpine "sleep infinity" 14 minutes ago Up 14 minutes ping-box-1
root@docker-test:~# docker run --rm --network my-net alpine wget -qO- http://nginx-custom
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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, nginx is successfully installed and working.
Further configuration is required for the web server, reverse proxy,
API gateway, load balancer, content cache, or other features.</p>
<p>For online documentation and support please refer to
<a href="https://nginx.org/">nginx.org</a>.<br/>
To engage with the community please visit
<a href="https://community.nginx.org/">community.nginx.org</a>.<br/>
For enterprise grade support, professional services, additional
security features and capabilities please refer to
<a href="https://f5.com/nginx">f5.com/nginx</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
root@docker-test:~# curl http://localhost:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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, nginx is successfully installed and working.
Further configuration is required for the web server, reverse proxy,
API gateway, load balancer, content cache, or other features.</p>
<p>For online documentation and support please refer to
<a href="https://nginx.org/">nginx.org</a>.<br/>
To engage with the community please visit
<a href="https://community.nginx.org/">community.nginx.org</a>.<br/>
For enterprise grade support, professional services, additional
security features and capabilities please refer to
<a href="https://f5.com/nginx">f5.com/nginx</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
网络模式总结
| 场景 | 网络方案 |
|---|---|
| 单容器,需要外界访问 | 默认 bridge + -p |
| 多容器互相通信(Compose) | 创建自定义网络,容器名做 DNS |
| 高性能、不想 NAT | 使用 --network host(慎用,端口冲突风险) |
| 完全隔离 | --network none |
| 跨宿主机通信 | overlay 网络(Swarm/K8s 中学习) |
