This lab demonstrates a practical Docker Compose example with a multi-container application. You'll explore a complete docker-compose-sample.yml file that defines three interconnected services: a web server, an application, and a database.
- Docker and Docker Compose installed
- Basic understanding of Docker concepts
- Completion of the previous lab (001-intro)
This docker-compose-sample.yml file defines a multi-container application with three services: web, app, and db. Below is a detailed explanation of each section:
- Image: Uses the official
nginx:latestimage. - Container Name: The container is named
web-container. - Ports: Maps port
8080on the host to port80in the container. - Environment Variables:
NGINX_HOST: Set tolocalhost.NGINX_PORT: Set to80.
- Volumes: Mounts the
./web-datadirectory on the host to/usr/share/nginx/htmlin the container, allowing you to serve custom static files. - Networks: Connects to the
webnetnetwork. - Restart Policy: Always restarts the container if it stops.
- Build: Builds the image from a Dockerfile located in the
./appdirectory. - Container Name: The container is named
app-container. - Ports: Maps port
3000on the host to port3000in the container. - Environment Variables:
NODE_ENV: Set toproduction.
- Dependencies: Depends on the
dbservice, ensuring the database starts before the app. - Networks: Connects to both
webnetanddbnetnetworks. - Restart Policy: Restarts the container on failure.
- Image: Uses the official
postgres:latestimage. - Container Name: The container is named
db-container. - Environment Variables:
POSTGRES_USER: Set touser.POSTGRES_PASSWORD: Set topassword.POSTGRES_DB: Set tomydb.
- Volumes: Mounts a named volume
db-datato/var/lib/postgresql/datain the container, persisting database data. - Networks: Connects to the
dbnetnetwork. - Restart Policy: Restarts the container unless stopped manually.
db-data: A named volume used by thedbservice to persist PostgreSQL data.
webnet: A custom network used by thewebandappservices to communicate.dbnet: A custom network used by theappanddbservices to communicate.
This Compose file demonstrates how to define services, manage dependencies, and configure networks and volumes for a multi-container application.
- Examine the Sample File: Review the
docker-compose-sample.ymlfile to understand the service definitions - Analyze Service Dependencies: Study how the
appservice depends on thedbservice - Understand Networking: See how services communicate through custom networks
- Review Volume Management: Learn how data persistence is handled with named volumes
- Multi-service Architecture: How to structure applications with multiple containers
- Service Dependencies: Using
depends_onto control startup order - Network Isolation: Creating separate networks for different tiers
- Data Persistence: Using volumes to maintain database state
- Port Mapping: Exposing services to the host system
After completing this lab, you'll understand:
- How to structure a real-world multi-container application
- The relationship between web servers, applications, and databases
- Best practices for service configuration in Docker Compose
