Introduction
Docker Compose is a powerful tool that simplifies the deployment of multi-container applications. It allows developers to define application services, networks, and volumes using a YAML file, enabling seamless orchestration and scaling of containerized applications.
In this blog, we will explore Docker Compose by deploying a sample voting application. This guide is complemented by a video tutorial available on the Mastering Cloud YouTube channel.
What is Docker Compose?
Docker Compose is a command-line tool that enables you to define and manage multi-container Docker applications. It uses a YAML configuration file (docker-compose.yml
) to describe the services required to run an application, making it easy to:
Define multi-container applications
Manage service dependencies
Scale services efficiently
Handle networking and persistent storage
Prerequisites
Before getting started, ensure you have the following:
Docker installed on your machine
Basic understanding of Docker and containerized applications
Docker Compose installed (included with Docker Desktop)
Installing Docker Compose
The following command will download the 1.29.2
release and save the executable file at /usr/local/bin/docker-compose
, which will make this software globally accessible as docker-compose
:
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Next, set the correct permissions so that the docker-compose
command is executable:
sudo chmod +x /usr/local/bin/docker-compose
To verify that the installation was successful, you can run:
docker-compose --version
Getting Started
Step 1: Clone the Repository
To follow along, clone the example voting application repository:
git clone https://github.com/dockersamples/example-voting-app.git
Step 2: Explore the docker-compose.yml
File
The application consists of multiple services defined in the docker-compose.yml
file:
vote: A Python web app for voting
result: A Node.js web app displaying the voting results
worker: A background service to process votes
redis: A Redis service for caching votes
db: A PostgreSQL database to store votes
seed: A service that seeds the database
Below is the key structure of the docker-compose.yml
file:
services:
vote:
build:
context: ./vote
target: dev
depends_on:
redis:
condition: service_healthy
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 15s
timeout: 5s
retries: 3
start_period: 10s
volumes:
- ./vote:/usr/local/app
ports:
- "5000:80"
networks:
- front-tier
- back-tier
result:
build: ./result
entrypoint: nodemon --inspect=0.0.0.0 server.js
depends_on:
db:
condition: service_healthy
volumes:
- ./result:/usr/local/app
ports:
- "5001:80"
- "127.0.0.1:9229:9229"
networks:
- front-tier
- back-tier
worker:
build:
context: ./worker
depends_on:
redis:
condition: service_healthy
db:
condition: service_healthy
networks:
- back-tier
redis:
image: redis:alpine
volumes:
- "./healthchecks:/healthchecks"
healthcheck:
test: /healthchecks/redis.sh
interval: "5s"
networks:
- back-tier
db:
image: postgres:15-alpine
environment:
POSTGRES_USER: "NA"
POSTGRES_PASSWORD: "NA"
volumes:
- "db-data:/var/lib/postgresql/data"
- "./healthchecks:/healthchecks"
healthcheck:
test: /healthchecks/postgres.sh
interval: "5s"
networks:
- back-tier
seed:
build: ./seed-data
profiles: ["seed"]
depends_on:
vote:
condition: service_healthy
networks:
- front-tier
restart: "no"
volumes:
db-data:
networks:
front-tier:
back-tier:
Step 3: Running the Application
To deploy the application using Docker Compose, navigate to the project directory and run:
docker-compose up -d
This command starts all services in detached mode
.
Step 4: Accessing the Application
Voting App: Open
http://localhost:8080
Results App: Open
http://localhost:8081
Step 5: Stopping the Application
To stop the application, run:
docker-compose down
Conclusion
Docker Compose simplifies multi-container application management, making it easy to define, deploy, and scale services. By following this tutorial, you have successfully deployed a multi-container voting application using Docker Compose. Experiment with different configurations and extend the application to match your use cases.
If you found this tutorial helpful, leave a comment on the YouTube video or reach out via GitHub. Happy coding! ๐