Day 18 - Docker for DevOps Engineers - Unveiling Docker Compose ๐Ÿš€

Nilkanth MistryNilkanth Mistry
3 min read

Hello DevOps Enthusiasts! Today, we're delving into the magic of Docker Compose, a tool designed to simplify the orchestration of multi-container applications. ๐Ÿณโœจ

๐Ÿ“œ Understanding Docker Compose:

What is Docker Compose?

Docker Compose is a boon for defining and sharing multi-container applications effortlessly. It operates on a YAML file, acting as a blueprint to set up services, links, and configurations in one go. Think of it as your DevOps symphony conductor! ๐ŸŽถ

How to Orchestrate Your Graph Application With Docker Compose

Why YAML?

YAML, or "Yet Another Markup Language," is a human-readable data serialization language frequently used for configuration files. Its simplicity and readability make it a preferred choice.

YAML Tutorial: Everything You Need to Know in 5 Mins | by Nehal Khan |  Level Up Coding

Sample Docker Compose File:

version: '3'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"

๐Ÿš€ Tasks at Hand:

Task-1: Mastering docker-compose.yml

  1. Install Docker Compose: If you haven't installed Docker Compose, you can do it by following the instructions here.

  2. Create a Project Directory: Create a directory for your project and navigate into it.

     mkdir my-docker-project
     cd my-docker-project
    
  3. Create a docker-compose.yml file: Create a docker-compose.yml file in your project directory. This will be the configuration file for Docker Compose.

     version: '3'
     services:
       web:
         image: nginx:latest
         ports:
           - "8080:80"
         environment:
           - ENV_VARIABLE=value
    

    In this example, we define a service named "web" using the Nginx image, expose port 8080 on the host to port 80 on the container, and set an environment variable.

  4. Run Docker Compose: Run the following command to start the services defined in your docker-compose.yml file.

     docker-compose up -d
    

    The -d flag runs the containers in the background

  5. Verify the Containers: Check if the containers are running.

     docker-compose ps
    

  6. Stop and Remove Containers: Stop and remove the containers when done.

     docker-compose down
    

Task-2: Handling Docker Containers Like a Pro

  1. Pull a Pre-existing Image: Choose an image from Docker Hub and pull it.

     docker pull ubuntu:latest
    

  2. Run as a Non-root User: When running a container, use the --user flag to specify a non-root user.

     docker run --user 1000:1000 -it ubuntu:latest /bin/bash
    

    Replace 1000:1000 with the appropriate user and group IDs.

  3. Exploration with Docker Commands:

    • View container processes:

        docker top <container_id>
      
    • Examine exposed ports:

        docker port <container_id>
      
    • View container logs:

        docker logs <container_id>
      
    • Gracefully start, stop, and remove containers:

        docker start <container_id>
        docker stop <container_id>
        docker rm <container_id>
      

Now you have successfully completed both tasks! Adjust the configurations and commands based on your specific needs.

๐Ÿ”„ Running Docker Commands Without Sudo:

sudo usermod -a -G docker $USER
Reboot the machine.

Embark on this Docker journey with confidence! These fundamental concepts pave the way for a deeper understanding of container orchestration.

0
Subscribe to my newsletter

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

Written by

Nilkanth Mistry
Nilkanth Mistry

Embark on a 90-day DevOps journey with me as we tackle challenges, unravel complexities, and conquer the world of seamless software delivery. Join my Hashnode blog series where we'll explore hands-on DevOps scenarios, troubleshooting real-world issues, and mastering the art of efficient deployment. Let's embrace the challenges and elevate our DevOps expertise together! #DevOpsChallenges #HandsOnLearning #ContinuousImprovement