Introduction

After a recent update to Docker Desktop for macOS, our development environment services failed to start. The error indicated that the extends configuration option had been deprecated.

Since our project depends on multiple environments, but the services used in local development, testing, and production environments are not exactly the same, I wanted to use extends to better manage the service orchestration files.

Version Comparison

  • Development Language: v1 was developed in Python, while v2 is completely developed in Go. I remember having headaches when upgrading Docker Compose on servers due to Python dependency issues. Now it’s much better - just download a single binary file.

  • Componentization: Docker Compose is integrated into the Docker ecosystem as a plugin, functioning as a subcommand of the docker command.

  • De-versioning: In Docker Compose v1, docker-compose.yml files had to declare a version at the beginning, but in v2 and v3, specifying version is no longer required. This might be for compatibility with Swarm.

Which Version Should You Use?

I personally prefer v2 because it has significant advantages over v1. Of course, the Docker team has also provided a compatibility solution called Compose-Switch to translate commands to v2.

Installing Docker Compose v2

On Windows and macOS, you don’t need to install it manually as it’s already included in Docker Desktop. For single users on Linux, you can install it with the following commands:

DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
mkdir -p $DOCKER_CONFIG/cli-plugins
(x)
curl -SL https://github.com/docker/compose/releases/download/v2.4.1/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose
(x)
chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose
(x)
docker compose version
(x)Docker Compose version v2.4.1

If you want to install it globally, you can place docker-compose in any of the following four directories:

  • /usr/local/lib/docker/cli-plugins
  • /usr/local/libexec/docker/cli-plugins
  • /usr/lib/docker/cli-plugins
  • /usr/libexec/docker/cli-plugins

Compose Switch

Automatic Installation with Script

curl -fL https://raw.githubusercontent.com/docker/compose-switch/master/install_on_linux.sh | sh

Manual Installation

Download the Executable

(x)# Download the executable
curl -fL https://github.com/docker/compose-switch/releases/download/v1.0.4/docker-compose-linux-amd64 -o /usr/local/bin/compose-switch
(x)
(x)# Add executable permissions
chmod +x /usr/local/bin/compose-switch

Rename Docker Compose v1 Executable

mv /usr/local/bin/docker-compose /usr/local/bin/docker-compose-v1

Create Replaceable Aliases:

  • RHEL-based systems use the alternatives command
  • Debian-based systems use the update-alternatives command

I’m using CentOS, so all the commands below use alternatives for configuration.

alternatives --install /usr/local/bin/docker-compose docker-compose /usr/local/bin/docker-compose-v1 1
alternatives --install /usr/local/bin/docker-compose docker-compose /usr/local/bin/compose-switch 99

Verify the Result

$ alternatives --display docker-compose
(x)
(x)docker-compose - status is auto.
(x)link currently points to /usr/local/bin/compose-switch
(x)/usr/local/bin/docker-compose-v1 - priority 1
(x)/usr/local/bin/compose-switch - priority 99
(x)Current `best' version is /usr/local/bin/compose-switch.

Switch Versions

alternatives --config docker-compose
(x)
(x)There are 2 programs which provide 'docker-compose'.
(x)
(x)  Selection    Command
(x)-----------------------------------------------
(x)   1           /usr/local/bin/docker-compose-v1
(x)*+ 2           /usr/local/bin/compose-switch
(x)
(x)Enter to keep the current selection[+], or type selection number: 1
(x)# Verify the switch was successful
docker-compose version
(x)docker-compose version 1.29.2, build 5becea4c
(x)docker-py version: 5.0.0
(x)CPython version: 3.7.10
(x)OpenSSL version: OpenSSL 1.1.0l  10 Sep 2019

I hope this is helpful, Happy hacking…