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
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.
~/.docker/cli-pluginsfolder if it doesn't exist
mkdir -p ~/.docker/cli-plugins/
- We start interacting with the API, by getting the JSON information from the latest release and presenting it nice in the console with
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
- Using the output above, we see that each
assetobject contains a
browser_download_urlproperties. Then, we can filter with
jqto return the
browser_download_urlbased on the asset's
namethat we want to install (
docker-compose-linux-x86_64in 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.
- If we want a single URL output, we can filter out the
*.sha256file 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:
- Download the plugin
curl -SL $URL -o ~/.docker/cli-plugins/docker-compose
- Apply executable permissions so that docker can run your binary
chmod +x ~/.docker/cli-plugins/docker-compose