Skip to content

Docker Compose

PotatoScript edited this page Feb 16, 2025 · 1 revision

Docker Compose Cheat Sheet

Overview

Docker Compose is a tool used to define and manage multi-container Docker applications. With docker-compose.yml, you can configure all your application's services (e.g., app, database, cache) in a single YAML file and run them together.


Installation

Install Docker Compose (if not included in Docker Desktop)

  1. Download Docker Compose:
    sudo curl -L "https://github.com/docker/compose/releases/download/v2.16.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  2. Make it executable:
    sudo chmod +x /usr/local/bin/docker-compose
  3. Verify installation:
    docker-compose --version

docker-compose.yml File Structure

A typical docker-compose.yml file has the following structure:

version: '3.8' # Define the Compose file format version

services:
  <service_name>:
    image: <image_name>
    build: <build_context>
    ports:
      - "<host_port>:<container_port>"
    volumes:
      - "<host_path>:<container_path>"
    environment:
      <key>: <value>
    depends_on:
      - <other_service_name>

Docker Compose Commands

Command Description
docker-compose up Build and start containers.
docker-compose up -d Start containers in detached mode (background).
docker-compose down Stop and remove containers, networks, and volumes.
docker-compose build Build or rebuild services.
docker-compose ps List running containers.
docker-compose logs Show logs for containers.
docker-compose stop Stop running containers without removing them.
docker-compose restart Restart services.

Example: Multi-Service Application

docker-compose.yml

This example creates a Node.js application with a MongoDB database:

version: '3.8'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=development
    depends_on:
      - db

  db:
    image: mongo:4.4
    container_name: mongodb
    ports:
      - "27017:27017"
    volumes:
      - mongo_data:/data/db

volumes:
  mongo_data:

Steps to Run

  1. Create a Dockerfile for the app service:

    FROM node:16
    
    WORKDIR /app
    
    COPY package*.json ./
    RUN npm install
    
    COPY . .
    
    EXPOSE 3000
    CMD ["npm", "start"]
  2. Run docker-compose up to start both services:

    docker-compose up
  3. Access the application at http://localhost:3000.


Best Practices for Using Docker Compose

  1. Version Pinning: Always specify the Compose version (e.g., version: '3.8') for compatibility.
  2. Use .env Files: Store environment variables in a .env file for easier management:
    • Example .env file:
      NODE_ENV=production
      DB_HOST=mongodb
    • Reference in docker-compose.yml:
      environment:
        - NODE_ENV=${NODE_ENV}
        - DB_HOST=${DB_HOST}
  3. Leverage Volumes: Use named volumes for persistent data.
  4. Use Networks for Isolation: Specify networks to isolate services when needed.
    networks:
      default:
        name: custom_network
  5. Keep docker-compose.yml Clean: Use separate Compose files for development and production:
    docker-compose -f docker-compose.dev.yml up

Advanced Example: WordPress with MySQL

docker-compose.yml

version: '3.8'

services:
  wordpress:
    image: wordpress:latest
    ports:
      - "8080:80"
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: user
      WORDPRESS_DB_PASSWORD: password
      WORDPRESS_DB_NAME: wordpress
    depends_on:
      - db

  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
      MYSQL_DATABASE: wordpress
      MYSQL_USER: user
      MYSQL_PASSWORD: password
    volumes:
      - db_data:/var/lib/mysql

volumes:
  db_data:

Steps to Run

  1. Save the above configuration as docker-compose.yml.
  2. Run the setup:
    docker-compose up
  3. Access WordPress at http://localhost:8080.

Debugging with Docker Compose

  • Check logs of a specific service:
    docker-compose logs <service_name>
  • Execute a command inside a running container:
    docker-compose exec <service_name> <command>
    Example:
    docker-compose exec app bash

Clone this wiki locally