Automating Multi-Container Applications with Docker Compose

Overview

Docker Compose is an official tool designed to simplify the management and orchestration of multi-container Docker applications. It allows developers to define a stack of services in a single configuration file and control them with minimal commands.

Core Concepts

  • Service: An individual container instance running a specific image (e.g., a database, a web API, or a cache).
  • Project: A collection of related services that constitute a complete application environment.

Workflow

  1. Define your application services and infrastructure requirements in a YAML definition file.
  2. Execute a single command to create and start all services.

Installation

Download the binary from a reliable mirror source and move it to your system path:

sudo curl -L "https://mirrors.aliyun.com/docker-toolbox/linux/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Configuration Example: Flask App

The following example defines a Python web application and a Redis cache. It maps ports, mounts source code for live development, and persists logs.

version: "3.9"
services:
  app:
    container_name: flask_web
    build: ./app
    ports:
      - "5000:5000"
    volumes:
      - ./app:/usr/src/app
      - storage_volume:/usr/src/app/logs
    depends_on:
      - cache

  cache:
    image: redis:alpine

volumes:
  storage_volume:

To launch this stack, run the following in the terminal:

docker-compose up --build

Case Study: WordPress Deployment

You can deploy a WordPress site with a MySQL backend using a single manifest. This configuration ensures data persistence for both the database and the website files.

version: "3.9"

volumes:
  db_content: {}
  site_content: {}

services:
  database:
    image: mysql:5.7
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: rootpass
      MYSQL_DATABASE: wp_db
      MYSQL_USER: wp_user
      MYSQL_PASSWORD: wp_pass
    volumes:
      - db_content:/var/lib/mysql

  cms:
    image: wordpress:latest
    restart: unless-stopped
    ports:
      - "8080:80"
    depends_on:
      - database
    environment:
      WORDPRESS_DB_HOST: database:3306
      WORDPRESS_DB_USER: wp_user
      WORDPRESS_DB_PASSWORD: wp_pass
      WORDPRESS_DB_NAME: wp_db
    volumes:
      - site_content:/var/www/html

Operations

  • Start in background: docker-compose up -d
  • Stop and remove containers (keeps volumes): docker-compose down
  • Stop and remove everything (including volumes): docker-compose down -v

Standard Project Structure

When deploying a custom application (e.g., a Java Spring Boot JAR):

  1. Package the application (e.g., target/app.jar).
  2. Create a Dockerfile to containerize the artifact.
  3. Define the service, networking, and dependencies in docker-compose.yml.

Tags: docker Docker Compose Container Orchestration devops YAML

Posted on Sat, 16 May 2026 00:41:35 +0000 by theorok