Day 18: Understanding Docker Compose

Ketan HirayKetan Hiray
2 min read

🐋Docker Compose

Docker Compose is a tool for managing complex applications with multiple containers. It uses a YAML file to specify services, networks, volumes, and more. We can build, run, and scale the entire application with a single command.

Docker Compose helps develop, test, and deploy multi-component applications or microservices. For instance, it can be used to create a web application with a web server, a database, and a cache, and to manage communication and dependencies between components.

📑What is YAML?

YAML stands for "Yet Another Markup Language" or "YAML Ain’t Markup Language" (a recursive acronym). It is a data serialization language designed to be easy to read and write for humans, commonly used for configuration files and data storage/transmission in applications.

YAML has a simple syntax that uses indentation, colons, dashes, and brackets to represent the structure and values of data. YAML supports scalars (such as strings, numbers, and booleans), lists (or sequences), and maps (or dictionaries). YAML also allows you to define custom data types and aliases for reusing data.

☑️Tasks

#️⃣Task 1: Understanding docker-compose.yml

To effectively use Docker Compose, start by learning how to work with the docker-compose.yml file. This file allows us to:

  • Set up the environment: Define the configuration for your services.

  • Configure services: Specify how each service will run and interact with others.

  • Use environment variables: Easily manage dynamic configurations

version: '3.3'
services:
  web:
    image: nginx:latest
    ports:
      - "8181:80"
  database:
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD:root

#️⃣Task 2: Running Docker Commands

1️⃣Pull a Docker Image:

docker pull nginx:latest

2️⃣Run the Container as a Non-Root User:

sudo usermod -a -G docker $USER
sudo reboot
docker run -d --name my_nginx nginx:latest

3️⃣Inspect the Container:

docker inspect my_nginx

4️⃣View Container Logs:

docker logs my_nginx

5️⃣Stop and Start the Container:

docker stop my_nginx
docker start my_nginx

6️⃣Remove the Container:

docker stop <container_id/name>
docker rm <container_id/name>

💥How to Run Docker Commands Without Sudo?

 sudo usermod -a -G docker $USER

🟢Reboot the machine.

sudo reboot

🎯Conclusion

Mastering Docker Compose and understanding YAML configuration files are crucial for modern DevOps. Docker Compose simplifies the management of multi-container applications, allowing you to define, configure, and control your services with ease. By completing hands-on tasks like setting up docker-compose.yml, pulling and running Docker images, and managing containers without root access, we can efficiently deploy and maintain containerized applications.

0
Subscribe to my newsletter

Read articles from Ketan Hiray directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ketan Hiray
Ketan Hiray

💼 Senior Software Engineer 🚀 Enthusiast in DevOps Engineering