Skip to main content

Networking in Compose

Imagine a city blueprint where each building (service) is connected by roads (networks). Without roads, the hospital can’t reach the school, and the power station can’t deliver electricity. In Docker Compose, networks are those roads - they connect services together, enabling communication and collaboration.


Networks Foundations

1. Networking in Compose

  • By default, Compose creates a default network for all services in a project.
  • Services in the same Compose file can communicate using service names as hostnames.
  • Networks can be customized for isolation and control.

2. Types of Networks in Compose

  • Default Network:
    • Automatically created when you run docker-compose up.
    • All services are attached unless specified otherwise.
  • Custom Networks:
    • Defined explicitly in the Compose file.
    • Useful for isolating groups of services.
  • External Networks:
    • Connect services to pre‑existing Docker networks.

3. Service Communication

  • Services resolve each other by service name.
  • Example:
    • web can connect to db using db:3306.
  • No need to hardcode IP addresses.

4. Defining Networks in Compose

version: '3'
services:
  web:
    image: nginx
    networks:
      - appnet
  db:
    image: mysql
    environment:
      - MYSQL_ROOT_PASSWORD=secret
    networks:
      - appnet

networks:
  appnet:

5. Multiple Networks Example

version: '3'
services:
  frontend:
    image: nginx
    networks:
      - publicnet
      - privatenet
  backend:
    image: redis
    networks:
      - privatenet

networks:
  publicnet:
  privatenet:
  • frontend can talk to both backend and external clients.
  • backend is isolated in privatenet.

Things to Remember

  • Compose automatically creates a default network.
  • Service names act as DNS hostnames.
  • Custom networks provide isolation and flexibility.
  • Multiple networks allow fine‑grained control of communication.

Hands‑On Lab

Step 1: Create a Compose File with a Custom Network

version: '3'
services:
  web:
    image: nginx
    ports:
      - "8080:80"
    networks:
      - appnet
  db:
    image: redis
    networks:
      - appnet

networks:
  appnet:

Step 2: Run the Stack

docker-compose up -d

Step 3: Test Communication

docker exec -it web ping db
  • web resolves db by name.

Step 4: Inspect Networks

docker network ls
docker network inspect appnet

Practice Exercise

  1. Create a Compose file with three services:
    • frontend (nginx) exposed on port 8081.
    • backend (node.js app).
    • database (mysql).
  2. Define two networks: publicnet and privatenet.
  3. Attach frontend to both networks, backend and database only to privatenet.
  4. Verify that frontend can talk to backend and database, but external clients only reach frontend.

Visual Learning Model

Networks in Compose
   ├── publicnet
   │     └── frontend (nginx)
   └── privatenet
         ├── frontend (nginx)
         ├── backend (node.js)
         └── database (mysql)

The Hackers Notebook

Networking in Compose connects services together using default or custom networks. Service names act as DNS hostnames, eliminating the need for IP addresses. With multiple networks, developers can isolate services while still allowing controlled communication, making Compose ideal for microservice architectures.


Tips, Tricks, Roadmaps, Resources, Networking, Motivation, Guidance, and Cool Stuff ♥

Updated on Dec 26, 2025