Table of contents
1.
Introduction
2.
What is Docker Compose?
3.
Docker Compose YAML File
3.1.
Structure
3.2.
Services
3.3.
Networks
3.4.
Volumes
4.
Commands
4.1.
Starting Services
4.2.
Stopping Services
4.3.
Viewing Logs
4.4.
Practical Example: WordPress Site
5.
Benefits of Docker Compose
6.
Basic Commands in Docker Compose
7.
Frequently Asked Questions
7.1.
What is Docker Compose used for?
7.2.
What is the difference between docker and Docker Compose?
7.3.
What does Docker Compose up do?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Docker Compose: Example, Benefits and Basic Commands

Author Lekhika
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Docker Compose is a tool that allows you to define and manage multi-container Docker applications. With Compose, you can define an entire environment in a simple docker-compose.yml file, and then bring it to life with a single command. It simplifies the orchestration of Docker containers and is often used in development, testing, and production scenarios.

Docker Compose

What is Docker Compose?

Docker Compose is a tool in the Docker ecosystem that simplifies the process of defining, managing, and orchestrating multi-container Docker applications. It uses a YAML file to specify the services, networks, and volumes required for an application, allowing developers to define complex environments with ease. Docker Compose enables the deployment and scaling of interconnected containers, streamlining the development and deployment of multi-container applications with consistent configurations.

Docker Compose YAML File

Structure

The docker-compose.yml file is the heart of Docker Compose. It's a YAML file that describes the services, networks, and volumes that your application needs. Here's an overview of what a basic file might look like:

version: '3'
services:
  web:
    image: nginx:latest
  db:
    image: mysql:5.7

Services

Services define the containers that will run, what images they use, ports, environment variables, etc. Each service can represent a part of your application, such as a web server or database.

Example:

services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: mypassword

Networks

Networks define the communication rules between containers. You can specify custom networks for isolated communication.

Example:

networks:
  mynetwork:
    driver: bridge

Volumes

Volumes are used to persist data and can be defined within the docker-compose.yml file.

Example:

volumes:
  mydata:
    driver: local

Commands

Starting Services

To start all the services defined in your docker-compose.yml, simply run:

docker-compose up

Stopping Services

To stop all running services:

docker-compose down

Viewing Logs

To view the logs of your running services:

docker-compose logs

Practical Example: WordPress Site

Let's create a simple Docker Compose file to run a WordPress site with a MySQL database.

version: '3'
services:
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: wordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
  wordpress:
    image: wordpress:latest
    ports:
      - "8000:80"
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
    depends_on:
      - db

 

With this file, you can start a WordPress site running on port 8000 with a single command:

docker-compose up

Benefits of Docker Compose

The benefits of Docker Compose are:

  • Simplified Orchestration: Docker Compose simplifies the orchestration of multi-container applications, allowing developers to define and manage complex environments with a single configuration file.
  • Consistent Development Environments: Developers can ensure consistency between development, testing, and production environments, reducing the "it works on my machine" problem.
  • Efficient Collaboration: Sharing a single Docker Compose file makes it easier for teams to collaborate, as everyone works with the same configuration for services, networks, and volumes.
  • Easy Scaling: Docker Compose facilitates scaling services effortlessly. Developers can define the desired scale for each service, making it simple to replicate containers when needed.
  • Rapid Deployment: With Docker Compose, deploying multi-container applications becomes fast and reproducible, enabling efficient and reliable deployment workflows.
  • Resource Optimization: Docker Compose allows users to configure and optimize resource usage, ensuring efficient utilization of CPU, memory, and other resources.

Basic Commands in Docker Compose

The basic commands in Docker Compose are:

  1. docker-compose up: Builds, (re)creates, starts, and attaches to containers for services defined in the docker-compose.yml file.
  2. docker-compose down: Stops and removes containers, networks, volumes, and other services defined in the docker-compose.yml file.
  3. docker-compose ps: Lists containers associated with the services defined in docker-compose.yml along with their status.
  4. docker-compose logs: Displays log output from services defined in the docker-compose.yml file.
  5. docker-compose exec: Executes a command in a running service container.

Frequently Asked Questions

What is Docker Compose used for?

Docker Compose is used to define and manage multi-container Docker applications, making it easier to build, ship, and run applications.

What is the difference between docker and Docker Compose?

Docker is a platform for containerization, while Docker Compose is a tool to define and manage multi-container applications, providing orchestration and simplified configuration.

What does Docker Compose up do?

docker-compose up builds, starts, and attaches to containers for services defined in the docker-compose.yml file, creating a multi-container environment based on the specified configuration.

Conclusion

Docker Compose is a powerful tool that simplifies the orchestration and management of multi-container applications. By defining services, networks, and volumes in a YAML file, developers can create complex environments with ease, both for development and production.

Understanding Docker Compose is essential for modern software development, especially in a world that increasingly relies on containerized applications. The flexibility, ease of use, and efficiency it brings to the Docker ecosystem make it an indispensable tool for developers and operations professionals alike.

Live masterclass