Installing docker compose v2 release as a plugin in Linux

Context #

After recently installing docker Linux machine, I found out that docker compose v2 is out and the installation process is different. Since Docker Desktop is not available for Linux, we need to install it from the project release page

Installation #

The Linux installation docs explain how to install a particular version of the docker compose plugin. That is sufficient when you are doing those operations manually; but may potentially install an old version, specially if using as part of a script that runs automatically.

Installing the latest version always #

If instead, we want to install the binary docker-compose-linux-x86_64 from latest version, we need to interact with the GitHub API to find the binary's download URL. This is where jq comes handy, as it allows to filter and extract data from JSON.

  1. Create ~/.docker/cli-plugins folder if it doesn't exist
mkdir -p ~/.docker/cli-plugins/
  1. We start interacting with the API, by getting the JSON information from the latest release and presenting it nice in the console with jq
curl -s https://api.github.com/repos/docker/compose/releases/latest | jq

The output from the above command should be similar to:

curl -s https://api.github.com/repos/docker/compose/releases/latest
| jq
{
"url": "https://api.github.com/repos/docker/compose/releases/50567216",
"assets_url": "https://api.github.com/repos/docker/compose/releases/50567216/assets",
...
"assets": [
{
"url": "https://api.github.com/repos/docker/compose/releases/assets/45961009",
...
"name": "docker-compose-darwin-aarch64",
...
"browser_download_url": "https://github.com/docker/compose/releases/download/v2.0.1/docker-compose-windows-x86_64.exe.sha256"
},
...
],
"tarball_url": "https://api.github.com/repos/docker/compose/tarball/v2.0.1",
"zipball_url": "https://api.github.com/repos/docker/compose/zipball/v2.0.1",
...
}
  1. Using the output above, we see that each asset object contains a name and a browser_download_url properties. Then, we can filter with jq to return the browser_download_url based on the asset's name that we want to install (docker-compose-linux-x86_64 in this exercise).
curl -s https://api.github.com/repos/docker/compose/releases/latest | jq -r ".assets[] | select(.name | contains(\"docker-compose-linux-x86_64\")) | .browser_download_url"

Although this narrows the search significantly, the above command outputs 2 URLs as a result, one for the compose binary and the other for its sha256 manifest file.

  1. If we want a single URL output, we can filter out the *.sha256 file and the final command looks like:
 $URL=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | jq -r ".assets[] | select(.name | contains(\"linux-x86_64\")) | select(.name | contains(\"sha256\") | not) | .browser_download_url")

Then, we can proceed with the same steps as the specific-version installation:

  1. Download the plugin
curl -SL $URL -o ~/.docker/cli-plugins/docker-compose
  1. Apply executable permissions so that docker can run your binary
chmod +x ~/.docker/cli-plugins/docker-compose

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.