Docker 容器连接

端口映射、容器间通过自定义网络互访、link 的过时用法。

Beginner
Key Points容器之间用自定义 bridge 网络、按容器名互访;对外暴露才用 -p,不要把数据库端口映射到公网。

端口映射

Bash
docker run -d -p 8080:80 nginx            # 宿主 8080 → 容器 80
docker run -d -p 127.0.0.1:8080:80 nginx # 只监听回环,外网访问不到
docker run -d -P nginx                   # 随机映射,docker ps 看实际端口

容器之间怎么通信

On the default bridge, containers can only reach each other by IP, which changes on every recreate. Create a user-defined network and Docker's embedded DNS resolves container names.

Bash
docker network create app-net

docker run -d --name db --network app-net -e MYSQL_ROOT_PASSWORD=secret mysql:8
docker run -d --name api --network app-net -e DB_HOST=db myapi:1.0

# api 容器里直接 ping db / 连 db:3306 即可
docker exec -it api ping -c1 db

三种网络驱动

DriverUseDNS by name
bridgeDefault, single hostOnly on user-defined
hostNo isolation, shares host portsN/A
noneNo networkingNo
overlayMulti-host (Swarm)Yes

link 已经过时

Bash
# 旧写法,仅为兼容保留
docker run -d --name api --link db:db myapi:1.0

--link is one-way and breaks with scaling; it is deprecated. A user-defined network plus container names does the same job better.

从宿主机访问容器

Bash
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web
curl http://127.0.0.1:8080          # 通过映射端口
Do not publish database or Redis ports. If you need temporary access, bind to 127.0.0.1:3306 only.

Friends & Partners