Docker Series Part 3: Docker Compose for Multi-Container Apps

Table of Contents

Docker Series Part 3: Docker Compose for Multi-Container Apps

So far, you’ve learned how to build and run a single-container app with Docker. But real-world applications often need more than one service—like a web app and a database. Managing multiple containers manually can get messy. That’s where Docker Compose comes in!

What is Docker Compose?

Docker Compose is a tool for defining and running multi-container Docker applications. You describe your app’s services, networks, and volumes in a single docker-compose.yml file, and then start everything with a single command.

Example: Flask App with Redis

Suppose you want to run your Flask app alongside a Redis service. Here’s how your docker-compose.yml might look:

version: "3.9"
services:
  web:
    build: .
    ports:
      - "5000:5000"
    environment:
      - REDIS_HOST=redis
  redis:
    image: "redis:alpine"
  • web: Builds your Flask app image from the current directory.
  • redis: Uses the official Redis image.

How to Use Docker Compose

  1. Make sure you have a Dockerfile, requirements.txt, and app.py for your Flask app.
  2. Add the docker-compose.yml file as shown above.
  3. Start your app with:
    docker compose up
    
  4. Both your Flask app and Redis will be running, and your app can connect to Redis using the hostname redis.

Why Use Docker Compose?

  • Simplicity: Manage all your services with one file and one command.
  • Scalability: Easily scale services up or down.
  • Isolation: Each service runs in its own container.

Recap

  • Docker Compose lets you manage multi-container apps easily.
  • Define services in docker-compose.yml.
  • Start everything with docker compose up.

In the next part, we’ll look at environment variables, volumes, and tips for local development. Stay tuned!