π Day 18 of My #90DaysOfDevOps Challenge: Docker for DevOps Engineers π
After exploring Docker and creating a Dockerfile in previous tasks, today we dive deeper into Docker Compose and learn about working with multiple containers. Docker Compose is an essential tool for DevOps engineers to manage complex multi-container applications with ease.
πΉ What is Docker Compose?
Docker Compose is a tool that simplifies running multi-container Docker applications. Instead of running individual docker run
commands for each container, you define all the services and configurations in a single YAML file, called docker-compose.yml
.
Key Features of Docker Compose
Use a YAML file to define services, networks, and volumes.
Spin up or tear down all services with a single command:
docker-compose up docker-compose down
Easy to link and configure multiple containers.
Supports environment variables for better configuration management.
πΉ What is YAML?
YAML (Yet Another Markup Language or YAML Ainβt Markup Language) is used for writing configuration files. It is simple, human-readable, and commonly used in tools like Kubernetes, Docker Compose, and Ansible.
Basic YAML Syntax Rules
Use spaces for indentation (no tabs).
Key-value pairs are defined with a colon:
key: value
Lists are denoted with a dash:
- item1 - item2
Environment variables can be defined like this:
environment: - VARIABLE_NAME=value
Tasks for Today
πΉ Task 1: Using Docker Compose
Learn how to create and use a docker-compose.yml
file to set up and manage multi-container environments.
Sample docker-compose.yml
File:
Here's an example configuration for a simple web application with Nginx and Redis:
version: "3.8"
services:
web:
image: nginx:latest
ports:
- "8080:80"
redis:
image: redis:latest
ports:
- "6379:6379"
Steps to Use Docker Compose:
Define the services in the
docker-compose.yml
file.Run the following command to start all the services:
docker-compose up -d
To stop and remove all services, run:
docker-compose down
πΉ Task 2: Pull and Run a Pre-existing Docker Image
This task involves running Docker commands without using sudo
and managing containers effectively.
Steps to Complete Task 2:
Pull an Image from Docker Hub:
For example, pull the official Nginx image:docker pull nginx
Run the Container as a Non-root User:
Add the current user to the Docker group:
sudo usermod -aG docker $USER
Reboot the machine to apply the changes.
Start the Container:
docker run -d --name my-nginx -p 8080:80 nginx
Inspect the Container:
View running processes:
docker inspect my-nginx
Check exposed ports:
docker port my-nginx
View Logs:
docker logs my-nginx
Stop and Restart the Container:
docker stop my-nginx docker start my-nginx
Remove the Container:
docker rm my-nginx
πΉ How to Run Docker Commands Without Sudo?
By default, Docker commands require root privileges. To avoid using sudo
for every command:
Add your user to the Docker group:
sudo usermod -aG docker $USER
Reboot your machine to apply changes.
Verify that Docker commands work without
sudo
:docker ps
Key Learnings from Day 18
Docker Compose simplifies the management of multi-container applications.
YAML files are essential for writing configurations in DevOps.
Using Docker commands without
sudo
improves productivity.Managing containers involves inspecting, logging, stopping, restarting, and removing them efficiently.
Conclusion
Day 18 of the #90DaysOfDevOps challenge was all about understanding Docker Compose and managing containers efficiently. Using docker-compose.yml
to set up multi-container applications and working with Docker commands without root access made the tasks easier and more productive.
Docker Compose and YAML are powerful tools that simplify managing complex applications. Learning how to inspect logs, manage processes, and control containers helps make containerization seamless for DevOps engineers.
For more updates, follow me here:
π Hashnode : https://hashnode.com/@patilsiddharth0411
π Medium : https://medium.com/@patilsiddharth0411
π LinkedIn : www.linkedin.com/in/siddharthpatil112000
Special thanks to Train with Shubham for the guidance and support throughout this journey!
#90DaysOfDevOps #Docker #DockerCompose #DevOps #YAML #CloudComputing #LearningTogether #TechCommunity