Interview's MateDocker
Interview's Mate

Docker

Compose · Networking · Security · Ops

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Docker topics
Lesson 85/ 160
05. Docker Compose & Multi-Container Apps

Networking in Compose (Default Networks, Custom Networks, Aliases)

Use the default project network plus custom networks and aliases in Compose.

BeginnerDefault Network · Custom Networks3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Keep ordinary services on the default project network until a real boundary is needed, then expose only the intended crossing service.

Think of it like…

The default network is a private project campus road, custom networks are fenced zones, and a proxy has passes to both zones.

Where you’ve seen it

In practice, networks: { edge: {}, data: { internal: true } }; services: { api: { networks: [edge, data] }, db: { networks: [data] } } is the concrete move for this job: use the default project network plus custom networks and aliases in Compose. The release is not accepted until docker compose up -d and docker network inspect $(docker compose config --format json | jq -r .networks.data.name) for endpoint names confirms the result.

Words you’ll hear

Default Network
The implementation control in networks: { edge: {}, data: { internal: true } }; services: { api: { networks: [edge, data] }, db: { networks: [data] } } for use the default project network plus custom networks and aliases in Compose.
Custom Networks
The result to observe with docker compose up -d and docker network inspect $(docker compose config --format json | jq -r .networks.data.name) for endpoint names, rather than infer from a successful command alone.
Aliases
The production boundary exposed when giving every service the edge network merely to make debugging easy, or declaring external: true and expecting docker compose down to delete it.

Visual diagram

Loading interactive diagram

Theory & deep dive

Default Network control path

The concrete mechanism is networks: { edge: {}, data: { internal: true } }; services: { api: { networks: [edge, data] }, db: { networks: [data] } }. The observable result is docker compose up -d and docker network inspect $(docker compose config --format json | jq -r .networks.data.name) for endpoint names; if those disagree, inspect the runtime evidence before changing unrelated settings.

Use the default project network plus custom networks and aliases in Compose. Keep Default Network, Custom Networks and Aliases in the same deployment record so the intent and the observed state cannot drift apart.

Concrete Default Network setup

networks: { edge: {}, data: { internal: true } }; services: { api: { networks: [edge, data] }, db: { networks: [data] } }

Key takeaway: Prove Custom Networks with docker compose up -d and docker network inspect $(docker compose config --format json | jq -r .networks.data.name) for endpoint names, not with command success alone.

Aliases failure boundary

A networks: declaration at the top is just a definition; a service must list it. Ports publish onto the host and do not replace the internal network route, while external: true makes teardown intentionally leave the network intact. The narrow failure to design for is giving every service the edge network merely to make debugging easy, or declaring external: true and expecting docker compose down to delete it.

Accept this compromise explicitly: One default network is simple; explicit network tiers reduce compromise paths but require a gateway and more troubleshooting context. The controlled check-docker compose up -d and docker network inspect $(docker compose config --format json | jq -r .networks.data.name) for endpoint names-also identifies the point where a different design is safer.

Deliberate failure test

Exercise this failure safely: giving every service the edge network merely to make debugging easy, or declaring external: true and expecting docker compose down to delete it.

Key takeaway: One default network is simple; explicit network tiers reduce compromise paths but require a gateway and more troubleshooting context.

How it works, step by step

Step 1 of 4
Design

1. State the boundary

Write down why the workload needs this: A networks: declaration at the top is just a definition; a service must list it. Ports publish onto the host and do not replace the internal network route, while external: true makes teardown intentionally leave the network intact.

Keep ordinary services on the default project network until a real boundary is needed, then expose only the intended crossing service.
Key numbers at this step
Intent
Default Network

When to use it & trade-offs

What it is

Compose creates a project-scoped default bridge where service names resolve. Additional named or custom networks can attach frontends and backends to different trust zones, and a network alias adds a DNS role without changing the service name.

Why it matters: A networks: declaration at the top is just a definition; a service must list it. Ports publish onto the host and do not replace the internal network route, while external: true makes teardown intentionally leave the network intact.

When to reach for it

  • The fit is strongest for this job: use the default project network plus custom networks and aliases in Compose. Pair it with docker compose up -d and docker network inspect $(docker compose config --format json | jq -r .networks.data.name) for endpoint names in the runbook.
  • Production Default Network changes should make Custom Networks observable through networks: { edge: {}, data: { internal: true } }; services: { api: { networks: [edge, data] }, db: { networks: [data] } }, rather than trusting the runtime default.
  • Keep a rollback example for the case where giving every service the edge network merely to make debugging easy, or declaring external: true and expecting docker compose down to delete it.

When to avoid or be careful

  • Do not accept command success while the system can still fail by giving every service the edge network merely to make debugging easy, or declaring external: true and expecting docker compose down to delete it.
  • Avoid erasing Aliases protection merely to dodge this cost: “One default network is simple; explicit network tiers reduce compromise paths but require a gateway and more troubleshooting context”.
  • Do not replace the evidence path-docker compose up -d and docker network inspect $(docker compose config --format json | jq -r .networks.data.name) for endpoint names-with a screenshot or a mutable tag.

networks: { edge: {}, data: { internal: true } }; services: { api: { networks: [edge, data] }, db: { networks: [data] } }: practical choices

Explicit Default Network
Best for: Use the default project network plus custom networks and aliases in Compose, with a reviewed runbook.

Strengths: Compose creates a project-scoped default bridge where service names resolve. Additional named or custom networks can attach frontends and backends to different trust zones, and a network alias adds a DNS role without changing the service name.

Watch out: One default network is simple; explicit network tiers reduce compromise paths but require a gateway and more troubleshooting context.

Ambient default
Best for: Short-lived Custom Networks experiments with low blast radius.

Strengths: It can begin with networks: { edge: {}, data: { internal: true } }; services: { api: { networks: [edge, data] }, db: { networks: [data] } } and fewer surrounding controls.

Watch out: It silently accepts giving every service the edge network merely to make debugging easy, or declaring external: true and expecting docker compose down to delete it, which makes the result hard to reproduce after promotion.

Managed Aliases
Best for: Teams that centralize Aliases policy across many workloads.

Strengths: A networks: declaration at the top is just a definition; a service must list it. Ports publish onto the host and do not replace the internal network route, while external: true makes teardown intentionally leave the network intact.

Watch out: The platform adds an ownership and debugging layer; retain docker compose up -d and docker network inspect $(docker compose config --format json | jq -r .networks.data.name) for endpoint names as the direct check.

Interview signals & numbers to know

When this topic comes up

  • networks: { edge: {}, data: { internal: true } }; services: { api: { networks: [edge, data] }, db: { networks: [data] } }
  • docker compose up -d and docker network inspect $(docker compose config --format json | jq -r .networks.data.name) for endpoint names
  • Failure signature: giving every service the edge network merely to make debugging easy, or declaring external: true and expecting docker compose down to delete it
  • Trade-off: One default network is simple; explicit network tiers reduce compromise paths but require a gateway and more troubleshooting context.

Benchmarks & rules of thumb

1 default network -
per Compose project when no custom attachment is declared
2 intentional networks -
edge and internal data in the example
0 public db ports -
needed for API-to-database traffic

Common mistakes & follow-ups

Common interview mistakes

  • Treating a successful networks: { edge: {}, data: { internal: true } }; services: { api: { networks: [edge, data] }, db: { networks: [data] } } as proof and skipping docker compose up -d and docker network inspect $(docker compose config --format json | jq -r .networks.data.name) for endpoint names.
  • Giving every service the edge network merely to make debugging easy, or declaring external: true and expecting docker compose down to delete it
  • Optimizing Default Network without accepting that one default network is simple; explicit network tiers reduce compromise paths but require a gateway and more troubleshooting context.

Likely interviewer follow-ups

  • Why does networks: { edge: {}, data: { internal: true } }; services: { api: { networks: [edge, data] }, db: { networks: [data] } } address Default Network here rather than through application code?
  • How would you distinguish healthy Custom Networks from the failure “giving every service the edge network merely to make debugging easy, or declaring external: true and expecting docker compose down to delete it” using docker compose up -d and docker network inspect $(docker compose config --format json | jq -r .networks.data.name) for endpoint names?
  • When would this trade-off-“One default network is simple; explicit network tiers reduce compromise paths but require a gateway and more troubleshooting context”-justify a different Aliases design?
Interview tip: Give one concrete operation-networks: { edge: {}, data: { internal: true } }; services: { api: { networks: [edge, data] }, db: { networks: [data] } }-and one proof-docker compose up -d and docker network inspect $(docker compose config --format json | jq -r .networks.data.name) for endpoint names. Then name the boundary: giving every service the edge network merely to make debugging easy, or declaring external: true and expecting docker compose down to delete it.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Networking in Compose (Default Networks, Custom Networks, Aliases) with generative AI interview Q&A and hands-on practice.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Service Dependencies (depends_on) & Health-Check Conditions (service_healthy)
Next topic
Managing Named Volumes & Bind Mounts in Compose