Skip to main content

Command Palette

Search for a command to run...

Day 18 Task: Docker for DevOps Engineers

Published
3 min readView as Markdown
Day 18 Task: Docker for DevOps Engineers
P

Greetings! 👋 I'm Priyadarshi Ranjan, a dedicated DevOps Engineer embarking on an enriching journey. Join me as I delve into the dynamic realms of cloud computing and DevOps through insightful blogs and updates. 🛠️ My focus? Harnessing AWS services, optimizing CI/CD pipelines, and mastering infrastructure as code. Whether you're peers, interns, or curious learners, let's thrive together in the vibrant DevOps ecosystem. 🌐 Connect with me for engaging discussions, shared insights, and mutual growth opportunities. Let's embrace the learning curve and excel in the dynamic realm of AWS and DevOps technology!

Introduction

Welcome to Day 18 of our 90-day DevOps journey! So far, we've created Dockerfiles and pushed them to repositories. Today, we're diving deeper into Docker by exploring Docker Compose. Let's understand what Docker Compose is and how it can make managing multi-container applications a breeze. 😃

Docker Compose

Docker Compose is a tool designed to help define and manage multi-container Docker applications. With Compose, you can create a YAML file to define your services and, with a single command, spin everything up or tear it all down. This simplifies the process of managing interconnected containers.

What is YAML?

YAML, which stands for "YAML Ain’t Markup Language" (or "Yet Another Markup Language"), is a data serialization language that's human-readable and commonly used for configuration files. YAML files are easy to understand and use a .yml or .yaml extension.

Task 1: Setting Up with Docker Compose

Your first task is to learn how to use the docker-compose.yml file to set up your environment, configure services, and link different containers. You'll also learn how to use environment variables in the docker-compose.yml file.

Sample docker-compose.yml File

Here's a simple example to get you started:

version: '3'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
  db:
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: example

In this example, we define two services: web and db. The web service uses the latest Nginx image and maps port 8080 on the host to port 80 on the container. The db service uses the latest MySQL image and sets an environment variable for the MySQL root password.

Task 2: Running a Pre-existing Docker Image

For the second task, you'll pull a Docker image from a public repository (like Docker Hub) and run it on your local machine. Follow these steps:

  1. Pull the Docker Image:

     docker pull nginx:latest
    
  2. Run the Container as a Non-root User: First, give your user permission to Docker:

     sudo usermod -aG docker $USER
    

    Reboot your machine to apply the changes:

     sudo reboot
    
  3. Run the Container:

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

     docker inspect mynginx
    
  5. View Container Logs:

     docker logs mynginx
    
  6. Stop and Start the Container:

     docker stop mynginx
     docker start mynginx
    
  7. Remove the Container:

     docker rm mynginx
    

Conclusion

Docker Compose is a powerful tool that simplifies the management of multi-container Docker applications. By using a simple YAML file, you can define your entire stack and manage it with ease. The skills you've learned today will help you in real-world scenarios where applications often consist of multiple interconnected services.

Real-life Example

Imagine you're working on a web application that consists of a front-end service, a back-end service, and a database. With Docker Compose, you can define all these services in a single docker-compose.yml file and manage them as a cohesive unit. This approach saves time and reduces complexity, making your development process more efficient.

Connect and Follow Me on Socials

LINKDIN | GITHUB |TWITTER

More from this blog

Priyadarshi Ranjan

71 posts

As a DevOps engineer, I leverage automation and continuous integration to streamline development workflows, ensuring robust and scalable deployments.