πŸš€ Day 18 of My #90DaysOfDevOps Challenge: Docker for DevOps Engineers πŸš€

πŸš€ Day 18 of My #90DaysOfDevOps Challenge: Docker for DevOps Engineers πŸš€

Β·

4 min read

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

  1. Use spaces for indentation (no tabs).

  2. Key-value pairs are defined with a colon:

     key: value
    
  3. Lists are denoted with a dash:

     - item1
     - item2
    
  4. 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:

  1. Define the services in the docker-compose.yml file.

  2. Run the following command to start all the services:

     docker-compose up -d
    
  3. 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:

  1. Pull an Image from Docker Hub:
    For example, pull the official Nginx image:

     docker pull nginx
    
  2. 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.

  3. Start the Container:

     docker run -d --name my-nginx -p 8080:80 nginx
    
  4. Inspect the Container:

    • View running processes:

        docker inspect my-nginx
      
    • Check exposed ports:

        docker port my-nginx
      
  5. View Logs:

     docker logs my-nginx
    
  6. Stop and Restart the Container:

     docker stop my-nginx
     docker start my-nginx
    
  7. 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:

  1. Add your user to the Docker group:

     sudo usermod -aG docker $USER
    
  2. Reboot your machine to apply changes.

  3. Verify that Docker commands work without sudo:

     docker ps
    

Key Learnings from Day 18

  1. Docker Compose simplifies the management of multi-container applications.

  2. YAML files are essential for writing configurations in DevOps.

  3. Using Docker commands without sudo improves productivity.

  4. 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

Β