Docker Network Introduction

Doc no.
KKZ-TN-055
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.

shell
docker network ls
# NETWORK ID     NAME      DRIVER    SCOPE
# 334be69b47a8   bridge    bridge    local

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.

shell
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 busybox1

02User-defined Bridge Networks#

To create a bridge network:

shell
docker network create my_bridge --driver bridge

To attach containers to the network:

shell
docker network connect my_bridge busybox1
docker network connect my_bridge busybox2

This time automatic service discovery will work:

shell
# OK
docker exec -it busybox2 ping busybox1
Figure 1 — Both bridges connect busybox1 and busybox2, but only a user-defined bridge runs Docker’s embedded DNS server (127.0.0.11), so only there does ping busybox1 resolve.

03Networking 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:

yaml
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_default is created.
  • A container is created using web's configuration. It joins the network myapp_default under the name web.
  • A container is created using db's configuration. It joins the network myapp_default under the name db.

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}:8001 for example postgres://localhost:8001.
Figure 2 — Inside myapp_default, web reaches db by service name on the container port 5432. The host goes through the published HOST_PORT 8001, and a container reaches the host via host.docker.internal.

04Summary#

  • Container 之间的连接通过自定义的 docker network 最方便, 支持自动服务发现, 不需要向宿主机映射端口(通过 CONTAINER_PORT 访问)
  • 宿主机想访问 Container 的服务, Container 必须开启端口映射, 宿主机通过 localhost:HOST_PORT 访问
  • Container 想访问宿主机的服务, 宿主机的 IP 为 host.docker.internal

05Reference#

Comments