Day 18 Task: Docker for DevOps Engineers

Faizan ShaikhFaizan Shaikh
4 min read

Getting Started with Docker Compose: A DevOps Engineer’s Guide

As a DevOps engineer, mastering Docker is essential for managing multi-container applications. Today, we’ll dive deeper into Docker Compose, a powerful tool that simplifies the process of defining and running multi-container Docker applications.


What is Docker Compose?

Docker Compose is a tool used to define and manage multi-container applications. With Compose, you can define all your app’s services, networks, and volumes in a single YAML file. Once defined, you can bring up or take down the entire application with one command.

Why Docker Compose?

  • Multi-Container Applications: Compose makes it easy to define and run multiple containers for a single application.

  • Simplified Configurations: All services are defined in one place, reducing complexity.

  • Portability: You can share your application with others using the docker-compose.yml file.


Understanding YAML

Before diving into Docker Compose, let’s quickly discuss YAML. YAML stands for "Yet Another Markup Language" or "YAML Ain’t Markup Language" (a playful recursive acronym). It’s widely used in configuration files because it is human-readable and easy to understand.

Here’s a quick look at the anatomy of a docker-compose.yml file.


Task 1: Setting Up a Multi-container Application with Docker Compose

Let's learn how to set up your environment using Docker Compose!

Sample docker-compose.yml file

version: '3.8'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
    volumes:
      - ./html:/usr/share/nginx/html
    environment:
      - NGINX_HOST=localhost
      - NGINX_PORT=80
  database:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: example_password
    volumes:
      - db_data:/var/lib/mysql
volumes:
  db_data:

Breaking it down:

  1. version: Specifies the Compose file version.

  2. services: Defines the containers that make up your application.

    • web: A service using an Nginx container, which serves content on port 8080.

    • database: A MySQL database container with a root password defined using environment variables.

  3. volumes: Used to persist database data between container restarts.

How to Use Docker Compose:

  • To spin up the services, run:

      docker-compose up
    
  • To tear down the services, run:

      docker-compose down
    

Docker Compose simplifies the process of launching and managing multi-container Docker applications.


Task 2: Running Docker Commands Without Sudo & Pulling an Image

Now, let’s pull a pre-existing Docker image and run it locally while practicing how to manage the container.

Step 1: Add User to Docker Group

To run Docker without sudo, you need to add your user to the docker group:

sudo usermod -a -G docker $USER

After running the command, reboot the machine to apply the changes:

sudo reboot

Step 2: Pull and Run an Image

Let’s pull an Nginx image from Docker Hub:

docker pull nginx:latest

Once the image is downloaded, run it on your machine:

docker run -d --name my_nginx -p 8080:80 nginx:latest

This command will run Nginx in detached mode, expose port 80 to the host’s port 8080, and name the container my_nginx.


Managing the Container

Once the container is running, let’s inspect its configuration and manage its lifecycle.

Inspect the Container

To view detailed information about the running container, such as processes, configuration, and exposed ports, use:

docker inspect my_nginx

View Container Logs

To check the logs generated by the container, use:

docker logs my_nginx

Stop and Start the Container

To stop the container, run:

docker stop my_nginx

To start the container again:

docker start my_nginx

Remove the Container

Once you’re done with the container, you can remove it using:

docker rm my_nginx

Conclusion

By now, you should have a solid understanding of Docker Compose and some core Docker commands. Using Docker Compose, you can manage multi-container applications with ease. Additionally, you’ve learned how to pull Docker images, manage containers without sudo, inspect running processes, and more.

Docker Compose is an essential tool for DevOps engineers, simplifying container orchestration in development environments. It allows teams to define, share, and deploy complex applications effortlessly.


By following these tasks, you’ll have an easier time managing multi-container applications in your day-to-day DevOps work. Stay tuned for more Docker deep dives! 😃

0
Subscribe to my newsletter

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

Written by

Faizan Shaikh
Faizan Shaikh