Docker Network Introduction
- Doc no.
- KKZ-TN-059
- Date
- 2025-03-29
- GMT day
- 2025:088
- Est. read
- 3 min
Summarize common Docker Network operations.
01Default Bridge Network#
After a fresh Docker installation, you can find a default bridge network up and running.
docker network ls
Docker uses a software-based bridge network that allows containers connected to the same bridge network to communicate while isolating them from other containers not running in the same bridge network.
docker run -dit --name busybox1 busybox
docker run -dit --name busybox2 busybox
docker inspect busybox1 | jq -r ' [0].NetworkSettings.IPAddress'
docker inspect busybox2 | jq -r '.[0].NetworkSettings.IPAddress'
# OK
docker exec -it busybox2 ping 172.17.0.3
# Error
docker exec -it busybox2 ping busybox102User-defined Bridge Networks#
To create a bridge network:
docker network create my_bridge --driver bridgeTo attach containers to the network:
docker network connect my_bridge busybox1
docker network connect my_bridge busybox2This time automatic service discovery will work:
# OK
docker exec -it busybox2 ping busybox103Networking in Compose#
By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by the service's name.
For example, suppose your app is in a directory called myapp, and your compose.yaml looks like this:
services:
web:
build: .
ports:
- "8000:8000"
db:
image: postgres
ports:
- "8001:5432"When you run docker compose up, the following happens:
- A network called
myapp_defaultis created. - A container is created using
web's configuration. It joins the networkmyapp_defaultunder the nameweb. - A container is created using
db's configuration. It joins the networkmyapp_defaultunder the namedb.
Each container can now look up the service name web or db and get back the appropriate container's IP address. For example, web's application code could connect to the URL postgres://db:5432 and start using the Postgres database.
- Within the web container, your connection string to db would be
postgres://db:5432 - From the host machine, the connection string would be
postgres://{DOCKER_IP}:8001for examplepostgres://localhost:8001.
04Summary#
- Container 之间的连接通过自定义的 docker network 最方便, 支持自动服务发现, 不需要向宿主机映射端口(通过
CONTAINER_PORT访问) - 宿主机想访问 Container 的服务, Container 必须开启端口映射, 宿主机通过
localhost:HOST_PORT访问 - Container 想访问宿主机的服务, 宿主机的 IP 为
host.docker.internal