Script Valley
Docker: Complete Course
Docker NetworkingLesson 3.2

How to create custom Docker networks for container communication

custom bridge network, docker network create, container name DNS, network connect, network disconnect, inter-container communication

Why Custom Networks Beat the Default Bridge

Custom Docker bridge network with DNS

On a custom bridge network, Docker provides automatic DNS: containers can reach each other using their --name. This is the foundation of all multi-container setups.

Create a Network and Connect Containers

# Create a custom bridge network
docker network create app-network

# Run a database on that network
docker run -d \
  --name postgres \
  --network app-network \
  -e POSTGRES_PASSWORD=secret \
  postgres:16

# Run the API on the same network
docker run -d \
  --name api \
  --network app-network \
  -e DB_HOST=postgres \
  -p 3000:3000 \
  my-api

The API container can now connect to Postgres using the hostname postgres — Docker's embedded DNS resolves container names to their IPs automatically.

Connect an Existing Container to a Network

docker network connect app-network my-existing-container
docker network disconnect app-network my-existing-container

Inspect and Clean Up

docker network inspect app-network
docker network rm app-network
docker network prune          # Remove all unused networks

A container can belong to multiple networks simultaneously, which lets you create precise network topologies where a frontend container reaches the API but cannot reach the database directly.

Up next

Port mapping and exposing containers to the host

Sign in to track progress