mirror of
https://github.com/yeasy/docker_practice.git
synced 2026-03-11 04:14:38 +00:00
Add pitfall notes to COPY and networking docs
This commit is contained in:
@@ -91,3 +91,9 @@ $ docker network rm mynet
|
||||
|
||||
$ docker network prune
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
> **🔥 踩坑实录**
|
||||
>
|
||||
> 一个新手开发者通过 `docker-compose` 部署了两个容器化服务:服务 A 和服务 B。他在服务 A 的代码中尝试用 `localhost:3000` 访问服务 B,结果始终连接超时。这个错误非常隐蔽——在本地单机开发时看不出问题,因为他可能在同一个进程中测试。排查时他错误地认为是防火墙或网络配置问题。实际原因是:每个容器都有独立的网络命名空间,`localhost` 在容器内部只指向容器自己,不是宿主机也不是其他容器。正确的做法是使用 docker-compose 自动创建的服务名作为主机名:`http://service-b:3000`。`docker-compose` 会自动在网络中注册服务名的 DNS,这样容器间通信才能正确解析。改动仅需一行代码,问题随之消失。
|
||||
|
||||
Reference in New Issue
Block a user