You will create user-defined bridge networks so containers reach each other by name while staying isolated from unrelated containers. By the end you will prove a database is reachable by its app but unreachable from outside its network.

Learning Objectives

  • Create user-defined bridge networks with built-in DNS.
  • Connect containers by name instead of IP.
  • Verify network isolation between groups.
  • Time: ~3 hours · Difficulty: Intermediate · Prereqs: Docker installed.

Architecture Overview

Environment Setup

You will need: Docker and two or three small images (nginx and postgres work well).

Before you begin: the default bridge has no DNS; always use user-defined networks.

Step-by-Step Execution

01
Create a user-defined network

User-defined bridges provide automatic name resolution between attached containers.

docker network create app-net
02
Attach containers to it
docker run -d --name db --network app-net postgres:16 && docker run -d --name web --network app-net nginx:stable
03
Resolve by name from the app
$ docker exec web getent hosts db
172.18.0.2 db

Progress So Far

Testing & Validation

docker run --rm --name probe alpine sh -c "nc -zv db 5432 || echo isolated"

The probe (not on app-net) should print isolated, while web can reach db. That confirms isolation plus intended connectivity.

Troubleshooting
  • Name not resolving: the default bridge lacks DNS; attach to a user-defined network.
  • Containers can all reach each other: they share one network; separate concerns into distinct networks.
  • Port not reachable from host: publish it with -p; network membership alone does not expose it.

Extension Ideas

Key Results

  • Connected containers by name via user-defined DNS.
  • Isolated unrelated containers onto separate networks.
  • Proved both connectivity and isolation with a live probe.
  • Established the networking model used by Compose and K8s.