Skip to main content

How to Use Docker Compose

Purpose

Learn how to define and manage multiple Docker containers using a simple YAML configuration file with Docker Compose.


1. Install Docker Compose

If you don't already have Docker Compose installed, install it:

bash
sudo apt update
sudo apt install docker-compose-plugin -y

2. Create a Docker Compose File

Create a directory for your project and add a docker-compose.yml file inside it:

bash
mkdir my-project
cd my-project
nano docker-compose.yml

Example docker-compose.yml:

yaml
version: '3'

services:
  web:
    image: nginx
    ports:
      - "8080:80"

3. Start the Application

Use Docker Compose to bring up your containers:

bash
docker compose up -d

The -d option runs it in the background (detached mode).


4. Stop the Application

When you want to stop and remove all containers defined in the Compose file:

bash
docker compose down

✅ Summary

  • Compose simplifies multi-container deployments with a single file.
  • Use volumes inside docker-compose.yml to persist your data.
  • Keep your Compose files under version control (like Git) for easy recovery and updates.