0Pricing
Docker & Kubernetes for Developers · Lesson

Multi-Container Apps with Compose

Orchestrate multiple Docker containers, such as a web app and a database, using Docker Compose for local development.

Multi-Container Apps with Compose is a free Docker & Kubernetes for Developers lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Docker & Kubernetes for Developers learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Multi-Container Apps?

In real-world applications, you rarely have just one component. Imagine a web application: you need a web server (like Nginx), a backend application (like Python Flask), and a database (like Redis or PostgreSQL).

Each of these components has its own dependencies and configuration. Managing them individually and ensuring they communicate correctly can quickly become complex.

Meet Docker Compose

Docker Compose is a tool designed to simplify the process of defining and running multi-container Docker applications.

  • You define your entire application's services (containers) in a single YAML file.
  • Then, with one simple command, Compose creates and starts all the services for you.
  • It's incredibly useful for local development and testing environments.

The Compose YAML File

The heart of Docker Compose is the docker-compose.yml file. This YAML file describes your application's services, networks, and volumes.

It typically starts with a version key, followed by the main services section where you define each container that makes up your application.

Defining Services

Under the services section, you list each component of your application. Each service corresponds to a Docker container.

Key properties for defining a service include:

  • image: Specifies the Docker image to use (e.g., nginx:latest).
  • build: Provides the path to a directory containing a Dockerfile to build a custom image.
  • container_name: An optional, fixed name for your container, making it easier to reference.

Exposing & Configuring Services

You often need to expose your services to the host machine or configure them with environment variables.

  • ports: Maps ports from the host to the container (e.g., "8080:80" maps host port 8080 to container port 80).
  • environment: Passes environment variables into the container (e.g., DB_HOST: "database").

These settings are crucial for communication and customization.

Connecting Your Services

When you use Docker Compose, it automatically creates a default network for your application. All services defined in your docker-compose.yml file join this network.

This allows services to communicate with each other using their service names as hostnames. For example, a web app service can connect to a database service named redis by simply using redis as the hostname.

Example: Python + Redis

Let's build a simple multi-container application: a Python Flask web app that counts how many times it has been visited, storing the count in a Redis database.

This setup clearly shows how a 'web' service can interact with a 'database' service within a single Compose application.

Web App Code & Dockerfile

Here's our simple Python Flask application (app.py) and its Dockerfile. The Flask app connects to the redis service, and the Dockerfile packages it all up.

# app.py
import time
import redis
from flask import Flask

app = Flask(__name__)
# Connects to 'redis' service in Compose network
cache = redis.Redis(host='redis', port=6379)

def get_hit_count():
    retries = 5
    while True:
        try:
            return cache.incr('hits')
        except redis.exceptions.ConnectionError as exc:
            if retries == 0:
                raise exc
            retries -= 1
            time.sleep(0.5)

@app.route('/')
def hello():
    count = get_hit_count()
    return 'Hello from CoddyKit! I have been seen {} times.\n'.format(count)

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

# requirements.txt
# flask
# redis

# Dockerfile
FROM python:3.9-slim-buster
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python", "app.py"]

Your First Compose File

This docker-compose.yml ties everything together. It defines two services: web (our Python app, built from the current directory) and redis (using the official Redis image).

Try running this example!

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

Managing Your Compose App

To start your multi-container application in detached mode (in the background), navigate to the directory containing your docker-compose.yml and supporting files, then run:

docker compose up -d

To see the running services and their status:

docker compose ps

To stop and remove the containers, networks, and volumes created by Compose:

docker compose down

Compose Configuration Check

Consider the following docker-compose.yml snippet:

version: '3.8'
services:
  app:
    build: .
    ports:
      - "8080:80"
  database:
    image: "postgres:13"

Which statement is TRUE about this configuration?

Recap: Docker Compose

You've learned how Docker Compose simplifies managing multi-container applications for local development!

  • It uses a docker-compose.yml file to define all your services.
  • Services can communicate with each other using their service names on a default network.
  • Commands like docker compose up and docker compose down manage your entire application stack with ease.

Docker Compose is an essential tool for local development setups involving multiple Docker containers.

Frequently asked questions

Is the “Multi-Container Apps with Compose” lesson free?

Yes — the full text of “Multi-Container Apps with Compose” is free to read here on the web, and the Docker & Kubernetes for Developers course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Docker & Kubernetes for Developers course, upgrade to CoddyKit PRO.

What will I learn in “Multi-Container Apps with Compose”?

Orchestrate multiple Docker containers, such as a web app and a database, using Docker Compose for local development. You practise Docker & Kubernetes for Developers with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Docker & Kubernetes for Developers?

No prior experience is required. Docker & Kubernetes for Developers on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Multi-Container Apps with Compose” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Docker & Kubernetes for Developers lesson?

Yes. Every Docker & Kubernetes for Developers lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Crafting Dockerfiles for Images
  2. Building Custom Docker Images
  3. Multi-Container Apps with Compose
  4. Persisting Data with Docker Volumes
← Back to Docker & Kubernetes for Developers