Docker Compose & Networking
Overview
Docker Compose is a tool for defining and running multi‑container Docker applications. This tutorial covers writing Compose files, networking containers, managing data with volumes, and orchestrating complex setups with a single command.
1. Docker Compose Fundamentals
Compose uses a YAML file to define services, networks, and volumes.
- Services: Each container is a service.
- Networks: Enable communication between containers.
- Volumes: Persistent data storage.
2. Writing a docker-compose.yml
Key fields
build: Path to Dockerfile.image: Use an existing image.ports: Port mapping.environment: Environment variables.depends_on: Startup order (but not wait for readiness).volumes: Mount volumes.
3. Defining Services
Each service can be configured with its own Dockerfile, environment, and dependencies. You can also scale services horizontally.
4. Networking between Containers
Compose creates a default network for your services. Containers can communicate using service names as hostnames.
This isolates services: only the API can access the database, not the frontend (if it were separate).
5. Managing Data with Volumes
Volumes persist data beyond the container lifecycle.
- Named volumes: Managed by Docker.
- Bind mounts: Mount a host directory.
- tmpfs: In‑memory (for temporary data).
Quiz
Question 1
What is the purpose of Docker Compose?
- To manage a single container
- To define and run multi‑container applications
- To build Docker images
- To manage Kubernetes clusters
Show answer
Question 2
How do containers in a Compose network discover each other?
- By IP address
- By service name (hostname)
- By container ID
- By manually configuring DNS
Show answer
Question 3
Which type of volume is best for sharing code during development with hot‑reload?
- Named volume
- Bind mount
- tmpfs
- Anonymous volume
Show answer
Exercises
Exercise 1
Write a docker-compose.yml for a simple app with a Node.js API and a MongoDB database.
Sample answer
Exercise 2
Add a Redis cache service to the above Compose file and update the API to use it.
Sample answer
Homework
Homework 1
Create a Docker Compose setup for your capstone project (frontend + backend + database). Include environment variables, networking, and volumes. Test it locally.
Sample outline
- Services: React frontend, Node.js API, PostgreSQL DB, Redis cache.
- Networks: Internal network for backend + DB, external for frontend.
- Volumes: DB data volume, bind mounts for frontend/backend code (for development).
- Environment: Use .env file for secrets.
Mini‑Project
Full‑Stack Application with Compose
Build a complete multi‑container application with Docker Compose:
- React frontend (build and serve with nginx)
- Node.js backend (Express API)
- PostgreSQL database
- Redis cache
- All services defined in a single docker-compose.yml
- Use environment variables for configuration
- Include health checks
Sample outline
- docker-compose.yml: All services with build contexts.
- .env: DB credentials, API keys.
- Frontend: Build React, then serve with nginx.
- Backend: Node.js with Express, connects to DB and Redis.
- Health checks: For each service to ensure readiness.
Tutorial Summary
You learned how to use Docker Compose to define and run multi‑container applications. You set up networking between containers, managed data with volumes, and used environment variables. You can now orchestrate complex services with a single command.
Key takeaway: Docker Compose is the standard tool for local development of multi‑service applications. It mirrors production setups, making it easier to test and debug.