Introduction to Docker Compose

Tutorial 1 of 5

Introduction

In this tutorial, we will introduce Docker Compose, a tool that simplifies the deployment and management of multi-container Docker applications. Docker Compose allows you to define and run multi-container Docker applications with just a single command.

By the end of this tutorial, you will:

  • Understand the basics of Docker Compose.
  • Learn how to define and run multi-container Docker applications.
  • Be able to use Docker Compose in your projects.

Prerequisites

Before you start, you should have:

  • Basic understanding of Docker.
  • Docker installed on your machine.

Step-by-Step Guide

Understanding Docker Compose

Docker Compose is a tool that allows you to manage and deploy multi-container Docker applications. It uses YAML files to configure application services and with a single command, you can create and start all services from your configuration.

Creating a Docker Compose file

The first step in using Docker Compose is to create a docker-compose.yml file. This YAML file defines the services, networks, and volumes for your application.

Here's an example docker-compose.yml file:

version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
  redis:
    image: "redis:alpine"

In this example, we have two services: web and redis. The web service builds from the current directory and maps port 5000 on the host to port 5000 on the container. The redis service uses the official Redis image from Docker Hub.

Running Docker Compose

After creating the docker-compose.yml file, you can start your application with the docker-compose up command. This command will start and run your entire app.

Code Examples

Let's look at a practical example of using Docker Compose.

Suppose we have a simple Flask application that uses Redis as a message broker. Our project directory might look like this:

/
|-- app.py
|-- Dockerfile
|-- docker-compose.yml

The app.py file is a simple Flask application:

from flask import Flask
from redis import Redis

app = Flask(__name__)
redis = Redis(host='redis', port=6379)

@app.route('/')
def hello():
    redis.incr('hits')
    return 'Hello World! I have been seen %s times.' % redis.get('hits')

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

The Dockerfile for the web service might look like this:

FROM python:3.7-alpine
WORKDIR /app
ADD . /app
RUN pip install flask redis
CMD ["python", "app.py"]

And the docker-compose.yml file:

version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
  redis:
    image: "redis:alpine"

To start the application, you would run docker-compose up from your project directory. You should see output indicating that both the web and redis services have been started. If you navigate to http://localhost:5000 in your web browser, you'll see the text "Hello World! I have been seen n times.", with n incrementing each time you refresh the page.

Summary

In this tutorial, we've covered the basics of Docker Compose. We've learned how to define and run multi-container applications with a single command.

To continue learning about Docker Compose, you can:

  • Try to define more complex multi-container applications.
  • Learn more about the Docker Compose file reference and the different options available.
  • Explore other Docker Compose commands like docker-compose down, docker-compose pause, and docker-compose restart.

Practice Exercises

  1. Create a Docker Compose file for a multi-container application that includes a web server, a database, and a worker service.
  2. Modify the Flask application from the example above to use a PostgreSQL database instead of Redis. Update the Docker Compose file accordingly.
  3. Learn how to use environment variables in Docker Compose files and update the Flask application to use an environment variable to configure the database connection string.

Remember, the best way to learn Docker Compose is by doing. Don't hesitate to experiment and try new things. Happy learning!