DEV Community

9romit
9romit

Posted on Originally published at 9romit.com

Docker Compose V2: Install It Right and Avoid the V1 Traps

The first time I adopted Docker Compose, the difference between docker-compose and docker compose broke roughly half my scripts. Every doc spells it differently, so it is hard to tell which one is current. This guide covers V2 specifically, from install through day-to-day operation.

Docker Compose V2 ships as a Docker CLI plugin and runs as docker compose. The older docker-compose (V1, hyphenated) reached end of support in July 2023 and is no longer recommended. On Ubuntu you install it via apt; on Windows it comes bundled with Docker Desktop.

  • The difference between Compose V1 and V2
  • Installing on Ubuntu
  • Installing on Windows with Docker Desktop
  • Writing a compose.yaml
  • Running it, plus a command reference
  • Common errors and how to fix them
  • FAQ
  • Related reading

The difference between Compose V1 and V2

V1 was a standalone binary written in Python, using the hyphenated docker-compose command. V2 was rewritten in Go and integrated as a Docker CLI plugin, so the command became docker compose with a space.

In practice this is not just a syntax change — execution speed and the supported feature set differ. Features like profiles, watch mode, and healthcheck-based dependency conditions only work properly in V2. When you find an older blog post using docker-compose, treat it as reference only and use docker compose in your own work.

V1 (legacy) V2 (current standard)
Command form docker-compose docker compose
Implementation language Python Go
Install method Separate binary Docker CLI plugin
version key required Yes No (ignored if present)
Support status Ended July 2023 Current standard

Installing on Ubuntu

On Ubuntu the standard approach is to install the docker-compose-plugin package alongside Docker Engine. Most recent distributions document it this way.

sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Enter fullscreen mode Exit fullscreen mode

After installing, confirm the version from the terminal.

docker compose version
Enter fullscreen mode Exit fullscreen mode

It prints something like this:

Docker Compose version v5.3.1
Enter fullscreen mode Exit fullscreen mode

This is where a lot of people stall. Most of the "I installed with apt but docker compose does not work" cases come down to installing only the legacy docker-compose package instead of docker-compose-plugin. Check the package name carefully.

Installing on Windows with Docker Desktop

Windows needs no separate step. Installing Docker Desktop includes Compose V2, and you can run docker compose right away in PowerShell or a WSL2 terminal.

Something you only notice after using it a while: on Windows, how volume mount paths are written depends on whether you are on the WSL2 backend or the Hyper-V backend. If you work in WSL2, keeping project files inside the Linux filesystem (for example under /home/yourname) performs better. Putting them on a Windows drive (/mnt/c) makes file I/O noticeably slower.

Writing a compose.yaml

As of V2, the version key at the top of the file is no longer needed. A practical example runs a web server together with a database.

services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
    depends_on:
      db:
        condition: service_healthy
    restart: always

  db:
    image: postgres:16
    environment:
      POSTGRES_USER: appuser
      POSTGRES_PASSWORD: <your-password>
      POSTGRES_DB: appdb
    volumes:
      - db_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U appuser"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: always

volumes:
  db_data:
Enter fullscreen mode Exit fullscreen mode

Here is the key point: if you attach condition: service_healthy to depends_on, the web container waits for the database to be genuinely ready. Without it, the web container frequently tries to connect before the db container has finished starting and fails. Beginners often stop at a bare depends_on; experienced users always pair it with a healthcheck condition.

Running it, plus a command reference

The basics and the commands you will use most:

# Start in the background
docker compose up -d

# Check status
docker compose ps

# Follow the logs
docker compose logs -f web

# Restart a single service
docker compose restart web

# Stop everything and remove containers
docker compose down

# Remove volumes as well
docker compose down -v
Enter fullscreen mode Exit fullscreen mode

docker compose ps gives you a table of services, their state, and their published ports.

NAME      IMAGE          COMMAND                  SERVICE   STATUS         PORTS
web       nginx:latest   "/docker-entrypoint.…"   web       Up 2 minutes   0.0.0.0:8080->80/tcp
db        postgres:16    "docker-entrypoint.s…"   db        Up 2 minutes (healthy)
Enter fullscreen mode Exit fullscreen mode

Checklist

  • Is the filename correct? (docker-compose.yml is still recognized, but new projects should use compose.yaml)
  • Check for port conflicts before starting
  • Keep environment variables in a separate .env file
  • Set an explicit restart policy in production
  • Confirm you have a backup before running the volume-removal flag (-v)

Common errors and how to fix them

The most frequent ones first.

Error response from daemon: driver failed programming external connectivity on endpoint web: Bind for 0.0.0.0:8080 failed: port is already allocated
Enter fullscreen mode Exit fullscreen mode

This happens when another process is already using the port. It shows up often when you re-run up without fully cleaning up the previous containers with docker compose down.

service "web" depends on undefined service "db": invalid compose project
Enter fullscreen mode Exit fullscreen mode

This one is almost always an indentation error. YAML is whitespace-based, so a stray tab character breaks parsing outright. Turning on the "show whitespace and tabs" option in your editor catches these fast.

A practical tip: validate compose.yaml with docker compose config before running it. It prints the fully parsed configuration, so typos and missing variables get caught ahead of time.

FAQ

Can I use docker-compose and docker compose together? Most distributions alias them automatically now, but for a new project it is easier to maintain if you standardize on docker compose from the start.

Should I use compose.yaml or docker-compose.yml? V2 recognizes both, but the official docs name compose.yaml for new files.

How do I separate environments (dev/staging/prod)? Use override files. Layering them with docker compose -f compose.yaml -f compose.prod.yaml up -d is the most common approach in practice.

Run docker compose config on the project you are working on right now. If the output differs from what you expected, start investigating there.

Related reading

This post is for informational purposes. Any consequences of applying it are the reader's responsibility.

Top comments (0)