Ch. 13 · Docker

Docker Networks and Container DNS

Docker Networks and Container DNS. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readintermediateupdated Oct 3, 2026

Containers on a suitable user-defined network can reach each other by configured service names. Localhost inside a container refers to that container.

Before you start

You should understand the difference between an image, a running container and the host. Record where a file, process or network endpoint actually lives before diagnosing a problem. Commands illustrate local experiments; adapt image names and paths to a disposable development environment.

The practical goal is to reason through this situation: An application connects to the database service name, not its own localhost. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.

Step-by-step walkthrough

Step 1: Locate network boundaries

Localhost within an application container names that container.

Step 2: Use service identity

Connect to the database’s service name on a suitable shared network.

Step 3: Check listening and membership

DNS resolution alone does not prove the target accepts connections.

Worked scenario

An application connects to the database service name, not its own localhost.

An application attempting localhost:5432 searches inside itself rather than another container. On the intended user-defined network, the database service name supplies discovery. A database listening only on an unsuitable interface or containers attached to different networks can still prevent access.

Common mistake

Hard-coding transient container IPs breaks when instances are recreated.

Verify the behavior

Verify resolution, network membership, listening address and port before changing credentials.

Interview exercise

Troubleshoot a failed connection.

Answer and reasoning

Check network membership, service name, listening address and port before changing credentials.

Continue learning

Compare the scenario with the Docker interview questions and test your understanding with the Docker MCQs. For terminology and implementation details, consult the reference material.

More in Docker

read ✓Docker · mid

Docker BuildKit and Cache Mounts

Speed up image builds with BuildKit cache mounts, multi-stage builds and dependency-first layer ordering.

~2 min readread →
read ✓Docker · mid

Docker Compose Profiles

Start only the services you need with Compose profiles, and keep the default set small for focused local development.

~2 min readread →
esc