π Day 18: Mastering Docker Compose for DevOps Engineers! π

Table of contents

π Introduction: Moving Beyond Dockerfiles
You've already conquered creating Dockerfiles and pushing images to repositories. Now, let's dive deeper and explore Docker Composeβa powerful tool for managing multi-container applications. Ready? Let's go! π
π οΈ What is Docker Compose?
Docker Compose is a handy tool that helps you define and run multi-container Docker applications. It uses a YAML file (docker-compose.yml
) to configure services, networks, and volumes. With a single command, you can spin up or tear down your entire environment. π
Why Docker Compose?
𧩠Manages complex applications with ease.
π Automates multi-container setups.
β‘ Simplifies environment management with
docker-compose up
anddocker-compose down
.
π What is YAML?
YAML (Yet Another Markup Language) is a human-readable data format commonly used for configuration files. Itβs simple, clear, and easy to understand.
β¨ Key Features of YAML:
Uses indentation to represent data structure.
Highly readable for humans.
File extensions:
.yml
or.yaml
.
π Tasks with Step-by-Step Answers!
βοΈ Task 1: Setting Up with docker-compose.yml
Letβs create a basic docker-compose.yml
file for a simple web application and a database service:
version: "3.8"
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html
db:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_DATABASE: sampledb
MYSQL_USER: chintamani
MYSQL_PASSWORD: chintamani123@
π Explanation:
web
service runs an NGINX container, accessible on port 8080.
π³ Task 2: Running a Pre-Existing Docker Image
Pull a Docker Image:
docker pull nginx:latest
π Pulls the official nginx HTTP server image
Run as a Non-Root User:
Add your user to the Docker group:
usermod -aG docker $USER
Reboot to apply changes:
sudo reboot
Run the container:
docker run -d --name mynginx -p 8081:80 nginx:latest
Inspect the Container:
docker inspect mynginx
π View details like environment variables, ports, and resource usage.
View Logs:
docker logs mynginx
π Logs show real-time output and help troubleshoot.
Stop and Start the Container:
Stop:
docker stop mynginx
Start:
docker start mynginx
Remove the Container:
docker rm mynginx
ποΈ Cleans up resources when no longer needed.
π« Run Docker Commands Without sudo
To avoid typing sudo
for every Docker command:
Add your user to the Docker group:
sudo usermod -aG docker $USER
Reboot your machine:
sudo reboot
β Now, Docker commands run without
sudo
!
π Conclusion
Youβve now mastered Docker Compose and explored essential Docker commands! π This knowledge is fundamental in real-world DevOps projects involving multi-container environments. Keep practicing, and youβll become a Docker pro in no time! π
π Stay tuned for more challenges and happy coding! π
Subscribe to my newsletter
Read articles from Chintamani Tare directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Chintamani Tare
Chintamani Tare
π¨βπ» Chintamani Tare | DevOps Enthusiast & Linux Advocate π I'm a passionate DevOps engineer with a solid foundation in Linux system administration. With a deep interest in automation, cloud technologies, and CI/CD pipelines, I love simplifying complex tasks and building scalable infrastructure. Whether it's scripting in Bash, managing servers with Ansible, or deploying applications with Docker and Kubernetes, I'm always eager to explore the latest tools and practices in the DevOps space.