Day 18 🚀Getting Hands-On💻 with Docker Compose for DevOps Engineers🐳
Docker Compose
Docker Compose simplifies running multi-container applications by allowing you to define services, networks, and volumes in a single YAML file. Here’s how to set up and use Docker Compose to manage containerized applications.
Why Docker Compose? 🐳
Multi-Container Applications: Easily manage and define multiple containers in a single application with a single YAML file.
Simplified Configurations: Keep all service configurations centralized in one file, reducing setup complexity.
Portability: Share your application effortlessly with others by providing just the
docker-compose.yml
file.Automated Networking: Compose automatically sets up networks for your containers, allowing seamless communication between services.
Environment-Specific Settings: Use environment variables within the YAML file to manage configurations for different environments (development, staging, production).
Consistency Across Environments: Ensure your application runs the same way in development, testing, and production environments.
Easy Startup and Shutdown: Spin up or tear down entire application stacks with simple
docker-compose up
anddocker-compose down
commands.
What is YAML?
YAML is a human-readable language often used for configuration files like Docker Compose. It supports .yml
or .yaml
file extensions, making it easy to define complex environments in a structured, readable format.
Task 1: Setting Up docker-compose.yml
Here’s a sample docker-compose.yml
file that defines two services, a web server and a database, which interact within a Docker network:
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "8080:80" # Exposing Nginx on host port 8080
volumes:
- ./html:/usr/share/nginx/html # Mount local HTML directory to Nginx
environment:
- NGINX_HOST=localhost
- NGINX_PORT=80
depends_on:
- db # Ensure Nginx starts after MySQL service is up
db:
image: mysql:5.7 # Use MySQL version 5.7
environment:
MYSQL_ROOT_PASSWORD: example_password # Set the MySQL root password
MYSQL_DATABASE: example_db # Create a default database
MYSQL_USER: user # Set a MySQL user
MYSQL_PASSWORD: user_password # Set a password for the MySQL user
volumes:
- db_data:/var/lib/mysql # Mount volume to persist MySQL data
volumes:
db_data:
driver: local # Define volume for MySQL data persistence
Task 1 Breakdown:
Key Features:
MySQL Configuration:
Sets
MYSQL_ROOT_PASSWORD
to define the root password for MySQL.Creates a default database (
example_db
) usingMYSQL_DATABASE
.Defines a user (
user
) with a password (user_password
) usingMYSQL_USER
andMYSQL_PASSWORD
.
Volumes:
Nginx (
./html:/usr/share/nginx/html
): Maps the local./html
directory to the Nginx container's web root. You can place your custom HTML files./html
on the host machine.MySQL (
db_data:/var/lib/mysql
): Ensures MySQL data is persisted even if the container is stopped or removed. This volume is nameddb_data
.
depends_on
:- The
web
service will wait for thedb
service to start before starting itself. However, it’s important to note thatdepends_on
does not guarantee that the database is fully ready for connections. You might still need to add logic to check if MySQL is fully ready (e.g., by adding a retry mechanism or usingwait-for-it
ordockerize
).
- The
Exposing Ports:
- The
web
service (Nginx) is exposed on a host port8080
while listening on container port80
.
- The
Additional Considerations:
- To handle situations where MySQL takes time to initialize, you may need to implement retries or use a waiting script like
wait-for-it
to delay the start of Nginx until MySQL is fully ready.
To spin up the services defined in the YAML file, run:
docker-compose up -d
The -d
flag runs the containers in detached mode. You can stop and remove containers with:
docker-compose down
Task 2: Running Docker Containers as a Non-Root User
To avoid needing sudo
for Docker commands, add your user to the Docker group:
Add user to Docker group:
sudo usermod -aG docker $USER
Reboot your system to apply changes:
sudo reboot
Pull an image and run it:
docker pull nginx:latest docker run -d --name my_nginx -p 8080:80 nginx:latest
Once the image is downloaded, run it on your machine
This command will run Nginx in detached mode, expose port 80 to the host’s port 8080, and name the container my_nginx
.
Task 2 Breakdown:
Inspect the container:
docker inspect my_nginx
View container logs:
docker logs my_nginx
Manage container state: Stop, start, and remove the container as needed:
docker stop my_nginx docker start my_nginx docker rm my_nginx
Additional Notes
Using Docker Compose and understanding docker-compose.yml
will make deploying multi-container applications much simpler, while running Docker as a non-root user enhances security and convenience.
Summary of Docker Commands
Command | Description |
docker pull nginx | Pull the Nginx image from Docker Hub. |
docker run -d --name my-nginx -p 8080:80 nginx | Run the Nginx container as a non-root user. |
docker inspect my-nginx | Inspect the details of the running container. |
docker logs my-nginx | View the logs of the Nginx container. |
docker stop my-nginx | Stop the running Nginx container. |
docker start my-nginx | Start the stopped Nginx container. |
docker rm my-nginx | Remove the Nginx container when done. |
We appreciate❤️ you taking the time to read and connect with us! Your engagement means a lot to us, and we look forward to hearing more from you📝
Subscribe to my newsletter
Read articles from Fauzeya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Fauzeya
Fauzeya
Hi there! I'm Fauzeya 👩💻, a passionate DevOps Engineer with a background in Computer Science Engineering🎓. I’m committed to enhancing security🔒, efficiency⚙️, and effectiveness in software development and deployment processes. With extensive knowledge in cloud computing☁️, containerization📦, and automation🤖, I aim to stay updated with the latest tools and methodologies in the DevOps field. Currently, I’m on a journey to deepen my understanding of DevOps I enjoy sharing my learning experiences and insights through my blog, 📝where I cover topics related to DevOps practices, tutorials, and challenges. I believe in continuous growth and learning and am excited to connect with fellow tech enthusiasts and professionals🤝. Let’s embark on this journey together!🚀