Home/Engineer/Docker / Containers/Understanding Docker Network…
Docker / ContainersIntermediateubuntumacoswindowsdockernetworkbridge

Understanding Docker Networks and Practical Configuration

Explains the differences between bridge, host, and overlay networks, how containers communicate with each other, and how to expose them externally.

Contents
  1. Docker Network Drivers
  2. Basic Network Commands
  3. Custom Bridge Networks (Recommended)
  4. Compose Network Configuration
  5. Port Mapping
  6. Debugging

Docker Network Drivers

DriverDescriptionUse case
bridgeDefault; isolated virtual network from the hostContainer communication on a single host
hostUses the host network directlyWhen performance is the top priority
noneCompletely disables networkingWhen full isolation is required
overlayMulti-host networkDocker Swarm

Basic Network Commands

Bash
docker network ls
docker network inspect bridge
docker network create mynet
docker network create \
  --driver bridge \
  --subnet 172.20.0.0/16 \
  --gateway 172.20.0.1 \
  mynet

docker network connect mynet container1
docker network disconnect mynet container1

Bash
docker network create app-net

docker run -d --name db      --network app-net postgres:16
docker run -d --name backend --network app-net \
  -e DB_HOST=db \
  myapp:latest

Compose Network Configuration

YAML
services:
  nginx:
    image: nginx:alpine
    ports:
      - "80:80"
    networks:
      - frontend

  app:
    build: .
    networks:
      - frontend
      - backend

  db:
    image: postgres:16
    networks:
      - backend

networks:
  frontend:
  backend:
    internal: true

Port Mapping

Bash
docker run -p 8080:80 nginx
docker run -p 127.0.0.1:8080:80 nginx # localhost
docker port container1

Debugging

Bash
docker exec -it myapp sh
# nslookup db
# curl http://db:5432

docker inspect --format '{{.NetworkSettings.IPAddress}}' mycontainer
#docker#network#bridge#compose#Containers
Editorial note

AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us

Topic-related official docs (auto-linked · not per-claim sources)Docker documentation ↗

Questions & answers

Ask a question about this guide. We'll review and reply.