archive2022

Moving from docker-compose to docker compose (Compose V2)

Compose V2 is now generally available. How to install it on a Linux server and a Raspberry Pi, and the small differences that can break your scripts.

2 min read#ops#docker#docker-compose

On 26 April 2022 Docker announced general availability of Compose V2. The command is now docker compose, with a space, as a plugin of the Docker CLI. The old Python tool docker-compose (V1) is deprecated. (Docker blog)

Docker Desktop already has V2. The Pi I reinstalled with the 64-bit OS in February got the plugin with Docker, but the Linux server and the older Pis still ran V1. I moved three small stacks, and a few things broke on the way.

Install

If Docker came from Docker’s own apt repository (the get.docker.com script also uses it), the plugin is one package:

sudo apt update
sudo apt install docker-compose-plugin
docker compose version

This works for amd64, arm64 and armhf. If you installed docker.io from the distribution repository, the plugin package may not exist there. Then either move to Docker’s repository, or download the binary from the Compose GitHub releases to ~/.docker/cli-plugins/docker-compose and make it executable.

I kept V1 installed until all stacks ran fine with V2, then removed it with sudo apt remove docker-compose (or pip uninstall docker-compose, depending on how it was installed).

What broke

Container names. V1 named containers project_service_1, V2 uses project-service-1. My backup script called docker exec myapp_db_1 and failed the first night. Now it uses the service name through compose, which does not depend on the naming:

docker compose exec db psql -U app

container_name: in the file is the other option, if a fixed name is really needed.

The .env file. V2 reads it from the project directory. Run commands from the folder with the compose file, or use -f together with --project-directory.

Builds. docker compose build uses BuildKit by default. The output looks different, and some old Dockerfiles that relied on legacy builder behavior may fail. In my case it was a file missing from the build context because of .dockerignore.

The top-level version: "3.8" line is no longer needed, because V2 implements the Compose Specification. It does no harm. I leave it in old files and drop it in new ones.

Scripts and cron jobs

grep -rn 'docker-compose' ~/bin /etc/cron* /etc/systemd/system 2>/dev/null

When you cannot change every script at once, a small wrapper helps during the move:

sudo tee /usr/local/bin/docker-compose <<'SH'
#!/bin/sh
exec docker compose "$@"
SH
sudo chmod +x /usr/local/bin/docker-compose

systemd

Most of my stacks only need restart: unless-stopped in the compose file. One stack has to start after another service, so it has a unit:

[Unit]
Description=Sensor stack
Requires=docker.service
After=docker.service network-online.target

[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/opt/sensor-stack
ExecStart=/usr/bin/docker compose up -d
ExecStop=/usr/bin/docker compose down

[Install]
WantedBy=multi-user.target