Skip to main content

Command Palette

Search for a command to run...

Docker Compose Essentials for DevOps Engineers

#90daysofDevOps Day-18

Published
6 min readView as Markdown
Docker Compose Essentials for DevOps Engineers

Hello, DevOps enthusiasts! Today, we’re diving into Docker Compose, a tool that simplifies managing multi-container applications. By the end of this blog, you’ll know how to set up and run Docker Compose, work with YAML configuration files, and use core Docker commands for managing containers efficiently. Let’s get started!

Why Docker Compose?

When working with complex applications, you might need multiple services, like web servers, databases, and caching layers, to work together. Docker Compose is a tool that enables you to define all these services in a single configuration file (docker-compose.yml) and start them with one command. This makes it especially helpful for applications with microservices architecture or multiple dependencies.

Key Benefits of Docker Compose

  • Centralized Configuration: Manage all services in a single YAML file.

  • One-Command Setup: Spin up or tear down your entire environment with docker-compose up and docker-compose down.

  • Networking and Dependencies: Define how containers communicate and link, automatically managing network configurations.

Understanding YAML

Before we dive into Docker Compose, let’s talk about YAML. YAML, which stands for YAML Ain’t Markup Language (a recursive acronym) or Yet Another Markup Language, is commonly used for configuration files due to its simplicity and readability.

YAML Basics

  • Indentation: YAML relies on indentation to define structure, so proper spacing is essential.

  • File Extension: Use .yml or .yaml as the file extension.

  • Human-Readable: Its structure is designed to be readable, using indentation for nesting instead of brackets or braces.

Here’s a sample YAML structure to illustrate its readability:

version: '3.8'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: rootpass
      MYSQL_DATABASE: myappdb

Task 1: Setting Up Docker Compose with docker-compose.yml

Now, let’s use Docker Compose to define a multi-container application. We’ll create a basic setup with a web server (Nginx) and a database (MySQL), linked together as part of a single application.

Step 1: Create a docker-compose.yml File

version: '3.8'

services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
    environment:
      - ENV=development
      - APP_VERSION=1.0

  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: rootpass
      MYSQL_DATABASE: myappdb
      MYSQL_USER: user
      MYSQL_PASSWORD: password

Explanation:

  • version: Specifies the Docker Compose file version (3.8 is the latest stable).

  • services: Each service represents a container.

    • web service:

      • image: Uses the latest Nginx image.

      • ports: Maps port 80 inside the container to port 8080 on the host.

      • environment: Passes environment variables to the Nginx container.

    • db service:

      • image: Runs MySQL version 5.7.

      • environment: Sets MySQL environment variables for database name, username, and password.

Step 2: Start Docker Compose

To start all services defined in the docker-compose.yml file, use:

docker-compose up -d

This command launches all services in detached mode (running in the background). Use docker ps to verify that both containers are up and running.

Step 3: Access the Web Application

  • Local Development: If you’re running this locally, go to http://localhost:8080 in your browser.

  • AWS EC2: For EC2 instances, replace localhost with your instance’s public IP address: http://your-public-ip:8080.

Step 4: Stop Docker Compose

To stop and remove all services, use:

docker-compose down

This command will stop the running containers and remove them, along with any associated networks.

Task 2: Managing Docker Containers with Key Commands

For this task, we’ll use Docker commands to pull, run, inspect, log, and remove containers. We’ll also cover permissions to run Docker commands without sudo.

Step 1: Pull and Run a Docker Image

Start by pulling a pre-existing Docker image from Docker Hub, such as Nginx.

docker pull nginx
docker run -d --name my-nginx nginx

This command runs an Nginx container in detached mode, named my-nginx.

Step 2: Running Docker Commands without Sudo

If you’re on Linux and tired of typing sudo before every Docker command, here’s how to set up permissions:

  1. Add your user to the Docker group:

     sudo usermod -aG docker $USER
    
  2. Reboot your machine for the changes to take effect:

     sudo reboot
    

Now you should be able to run Docker commands without needing sudo.

Step 3: Inspecting Container Processes and Ports

To view detailed information about a container, such as environment variables, network settings, and volume mounts, use docker inspect.

docker inspect my-nginx

Step 4: Viewing Container Logs

Use the docker logs command to see log output for a container, which is helpful for debugging.

docker logs my-nginx

Step 5: Stopping and Starting the Container

To stop and start the container, use:

docker stop my-nginx
docker start my-nginx

Step 6: Removing the Container

To delete the container when you’re done, use:

docker rm my-nginx

This command removes the stopped container from your local system.

Key Docker Commands Recap

CommandDescription
docker-compose up -dStart services in detached mode
docker-compose downStop and remove services
docker pull <image>Download an image from Docker Hub
docker run -d --name <name> <image>Run a container in detached mode
docker inspect <container>Get detailed information about a container
docker logs <container>View log output from a container
docker stop <container>Stop a running container
docker start <container>Restart a stopped container
docker rm <container>Remove a stopped container

Additional Notes for EC2 Users

If you’re running this setup on an AWS EC2 instance, be mindful of some additional configuration steps:

  1. Allow Inbound Traffic: Open port 8080 in your EC2 instance’s Security Group settings to allow HTTP traffic to reach your web server. You can do this by editing the Security Group attached to your instance and adding an inbound rule for TCP on port 8080, allowing traffic from your IP or from anywhere for testing.

  2. Access via Public IP: Use the public IP of your EC2 instance to access the application instead of localhost. For example, go to http://your-ec2-ip:8080 in your browser.

Conclusion

With Docker Compose and these essential Docker commands, you’re well on your way to managing multi-container applications. Docker Compose provides a convenient way to define and manage services, and understanding key Docker commands will give you more control over your containers.

Key Takeaways

  • Docker Compose: Simplifies multi-container setups with a single YAML file.

  • YAML: A human-readable configuration format ideal for Docker Compose.

  • Core Commands: Commands like docker inspect, docker logs, and docker rm provide essential functionality for managing containers.

  • AWS EC2 Configuration: Remember to adjust firewall rules for external access when running Docker on AWS EC2.

By learning Docker Compose, you can efficiently set up, manage, and deploy complex applications with ease. Keep exploring and experimenting, and you’ll be a Docker pro in no time!

Happy Docker-ing! 🐳