SyntaxStudy
Sign Up
Docker Docker Network Types and Bridge Networking
Docker Beginner 1 min read

Docker Network Types and Bridge Networking

Docker creates three default networks at installation: bridge (the default for standalone containers), host (container shares the host network namespace), and none (no networking). Additional networks can be created with docker network create. The bridge driver is the most commonly used for single-host deployments. When containers are connected to the same user-defined bridge network, they can communicate using container names as hostnames. This automatic DNS resolution is one of the key advantages of user-defined bridges over the default bridge network (which only provides IP-based communication). Always create dedicated bridge networks for application stacks rather than using the default bridge. Port mapping (-p host_port:container_port) makes container ports accessible from the host and external networks. Docker modifies iptables rules to forward traffic. Publishing to 0.0.0.0 (the default) makes the port accessible on all host interfaces; binding to 127.0.0.1:3000:3000 restricts access to the loopback interface, useful when Nginx handles external traffic.
Example
# List Docker networks
docker network ls

# Create a user-defined bridge network
docker network create --driver bridge app-network

# Run containers on the same network (DNS by container name)
docker run -d \
  --name db \
  --network app-network \
  -e POSTGRES_PASSWORD=secret \
  postgres:16-alpine

docker run -d \
  --name api \
  --network app-network \
  -e DATABASE_URL=postgres://postgres:secret@db:5432/postgres \
  -p 3000:3000 \
  myapp:latest

# api can reach db by hostname "db"
docker exec api ping db -c 3
docker exec api nslookup db

# Inspect the network
docker network inspect app-network

# Connect a running container to an additional network
docker network connect app-network existing-container

# Disconnect and remove
docker network disconnect app-network existing-container
docker network prune -f