Skip to content
intermediate Phase 13 · Docker Compose Advanced

Custom Networks

Create custom bridge and overlay networks for service isolation.

45m
0 problems
Topic Progress 0%

Default Network and Service Discovery

Default Network and Service Discovery

Every Compose project gets one bridge network (named <project>_default), and all services attach to it. Containers resolve each other by service name through the embedded DNS at 127.0.0.11:

docker network inspect apptest_default
# -> Services: web, api, db  (name -> container IP mapping)

That mapping is why DATABASE_URL=postgres://db:5432 works and why you must never hard-code container IPs: they change on every recreate, while the DNS entry follows. Container-to-container traffic on the same bridge flows directly — no host port round trip is involved.

Custom Bridge and Overlay Networks

Custom Bridge and Overlay Networks

Split traffic into explicit networks:

services:
  web:  { networks: [front] }
  api:  { networks: [front, back] }
  db:   { networks: [back] }
networks:
  front: {}
  back:  { internal: true }

Now web cannot reach db even by IP — back is internal, so its members also lose outbound internet. Bridge networks are single-host; overlay spans hosts but requires Swarm (services created by docker stack deploy or docker service create). Add aliases for extra DNS names, ipam to pin a subnet, and external: true to join a pre-existing network.

Pitfalls and Interview Angles

Pitfalls and Interview Angles

Service on two networks. api above is reachable from both sides; anything that must stay private should never join front.

Cross-project traffic. Networks are project-scoped, so projectA_web cannot dial projectB_api by default — declare the shared network as external: true in both files and attach the services explicitly.

Legacy links. Deprecated aliases that also create dependencies; service-name DNS replaces them entirely.

Interview: draw two networks and say which container can ping which, explain why internal: true is a security control, and state what overlay adds (and what it costs — Swarm).

Quiz

1. How do containers on the same Compose network reach each other?

Question 1 options

2. What does internal: true do to a network?

Question 2 options

3. When do you need the overlay driver?

Question 3 options

Flashcards

Question

Project default network

Answer

One bridge per project; all services join it and resolve each other by name.

Question

Embedded DNS

Answer

127.0.0.11 inside containers mapping service names to live container IPs.

Question

internal: true

Answer

Network with no outbound internet — isolation for data tiers.

Question

external network

Answer

Pre-existing network both projects declare to share one flat DNS domain.

Question

bridge vs overlay

Answer

Bridge = single host; overlay = multi-host, Swarm required.

Revision Notes

Key Takeaways

  • 1. Service-name DNS, not IPs, is the contract between containers
  • 2. Custom networks express blast-radius boundaries
  • 3. Overlay is a Swarm feature, not a Compose flag you flip casually

Interview Tips

  • • Draw reachability across two networks and name the DNS mechanism
  • • Explain how two Compose projects would share a database service

Cheat Sheet

one bridge per project; DNS via 127.0.0.11 maps service→IP; networks front/back with internal:true for db; external:true shares across projects; overlay needs Swarm.

Continue Learning