Docker for DevOps Engineers #Day-18

Nikunj VaishnavNikunj Vaishnav
3 min read

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

Today, we delved into Docker Compose, a powerful tool for managing multi-container Docker applications. By learning how to set up and configure services using a docker-compose.yml file, you can streamline the process of managing interconnected containers. Additionally, we explored how to pull and run Docker images from public repositories, giving you hands-on experience with container management. With these skills, you're well on your way to mastering Docker and enhancing your DevOps toolkit. Keep practicing, and stay tuned for more exciting topics in our 90-day journey! 😃

Connect and Follow:

LinkedIn | Twitter | GitHub

Like👍 | Share📲 | Comment💭

2
Subscribe to my newsletter

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

Written by

Nikunj Vaishnav
Nikunj Vaishnav

👋 Hi there! I'm Nikunj Vaishnav, a passionate QA engineer Cloud, and DevOps. I thrive on exploring new technologies and sharing my journey through code. From designing cloud infrastructures to ensuring software quality, I'm deeply involved in CI/CD pipelines, automated testing, and containerization with Docker. I'm always eager to grow in the ever-evolving fields of Software Testing, Cloud and DevOps. My goal is to simplify complex concepts, offer practical tips on automation and testing, and inspire others in the tech community. Let's connect, learn, and build high-quality software together! 📝 Check out my blog for tutorials and insights on cloud infrastructure, QA best practices, and DevOps. Feel free to reach out – I’m always open to discussions, collaborations, and feedback!