Docker Compose networks

Topic: Containers core

Summary

Compose creates a default network so services resolve by name. Define custom networks for isolation. Use when you need service discovery or isolation.

Intent: How-to

Quick answer

  • Default network lets all services reach each other by service name. Use service_name and port in app config.
  • Define custom networks under networks. Attach services to isolate front from back.
  • Use external true to attach to an existing Docker network.

Prerequisites

Steps

  1. Default network

    Compose up. Services use service name as hostname. Test with docker compose exec app ping db.

  2. Custom network

    Add networks section. Attach services. Only services on same network can communicate.

  3. External network

    Set external true for a network. Attach service to use existing network.

Summary

Use default network for discovery; add custom networks for isolation; attach to external when needed.

Prerequisites

Steps

Step 1: Default network

All services on default network; resolve by service name.

Step 2: Custom network

Define networks; attach services for isolation.

Step 3: External network

Attach to external network when needed.

Verification

  • Services resolve by name; isolation works.

Troubleshooting

Cannot resolve — Same network and correct service name. No isolation — Use different networks.

Next steps

Continue to