2

I noticed that docker-compose 1.x is gently being dropped in favor of the new v2:
https://www.docker.com/blog/announcing-compose-v2-general-availability/

So I then removed my docker-compose v1.x command (I think it was the way to go, but I cannot find the old documentation on how to install/uninstall docker-compose anymore):

$ sudo rm -rf /usr/local/bin/docker-compose

Then I installed the new version according to the doc:

$ sudo apt-get install docker-compose-plugin

and voilà:

$ docker compose version
Docker Compose version v2.5.0

But all my scripts which used to call docker-compose (with a dash) are no more working. Obviously.

Hence my question; how could I alias the new compose command to docker-compose?

Is it sufficient to add this line to by .bashrc file?

$ echo "alias docker-compose='docker compose'" >> ~/.bashrc
$ source ~/.bashrc
1
  • Didn't the alias work as intended? Commented May 24, 2022 at 7:58

3 Answers 3

6

The absolute easiest solution is to make an alias, like you state in your question:

alias docker-compose='docker compose'

This will work without any issue.

Also, I would recommend installing Docker Compose using the apt package docker-compose-plugin, and not by manually installing with curl, unless you have a very good reason to do so.

By installing with apt (which is the recommended way on Ubuntu), you ensure that you can update Docker Compose automatically with new Docker releases using apt upgrade or even with unattended upgrades.

6

Adding an alias to .bashrc will work for most cases, but for some scripts may not work.

IMHO creating a small script named docker-compose to your PATH that just passes arguments to docker compose is a better way to solve the problem.

This answer might help: https://stackoverflow.com/a/72187587/11418220

2
  • 2
    You're 100% correct than in some edge cases, a function might be more versatile than an alias (most notably if the script attempts to execute with sudo, and you haven't implemented a sudo/alias workaround) - upvoted! Commented Aug 8, 2022 at 12:01
  • 1
    Thanks for upvote @ArturMeinild I don'e have enough reputation to write comment yet, but I wanted to mention that using docker-compose-plugin is better than docker-compose as you mentioned.
    – Mimyo
    Commented Aug 8, 2022 at 12:14
0

It was not 100% clear to me but there are two ways to install compose on the documentation:

  1. the former, by apt install docker-compose-plugin

  2. a second way (user either the first way or the second, but not both) by manually downloading the binary with curl but it misses some details I think, especially the newly created folder is not in the default path. Here's how I proceeded to install it globally (i.e. in /usr/local/...):

$ sudo apt-get remove --purge docker-compose-plugin

$ sudo su

# DOCKER_CONFIG_CLI="/usr/local/lib/docker/cli-plugins"

# mkdir -p $DOCKER_CONFIG_CLI

# curl -SL https://github.com/docker/compose/releases/download/v2.5.0/docker-compose-linux-x86_64 -o $DOCKER_CONFIG_CLI/docker-compose

# chmod +x $DOCKER_CONFIG_CLI/docker-compose

# cd /usr/local/bin

# mv docker-compose docker-compose_v1 # only if you had a previous docker-compose version!

# ln -s /usr/local/lib/docker/cli-plugins/docker-compose

# exit

$ which docker-compose
/usr/local/bin/docker-compose

or in one shot:

#!/bin/bash

apt-get update \
&& apt-get remove --purge docker-compose-plugin \
&& DOCKER_CONFIG_CLI="/usr/local/lib/docker/cli-plugins" \
&& mkdir -p $DOCKER_CONFIG_CLI \
&& curl -SL https://github.com/docker/compose/releases/download/v2.5.0/docker-compose-linux-x86_64 -o $DOCKER_CONFIG_CLI/docker-compose \
&& chmod +x $DOCKER_CONFIG_CLI/docker-compose \
&& cd /usr/local/bin \
&& mv docker-compose docker-compose_v1 \
&& ln -s /usr/local/lib/docker/cli-plugins/docker-compose \
&& which docker-compose

Then you got the two ways of calling it, the transition is smoother:

$ docker compose version
Docker Compose version v2.5.0
$ docker-compose --version
# or: docker-compose version
Docker Compose version v2.5.0
4
  • This seems like an extremely hacky and avant-garde solution, and I fail to see how it actually answers your question. Why not just stick with an alias? Commented May 24, 2022 at 7:59
  • 1
    The behavior of the two commands is not exactly the same. I think the one with the dash in it is intended for a smoother transition, and, to me, it seems less hacky than an alias for the moment. E.g. try docker-compose --version with the alias... it won't work.
    – s.k
    Commented May 24, 2022 at 19:17
  • This is because the --version flag is not supported in v.2. The alias does exactly what it's supposed to do - substitute docker compose with docker-compose. But of course it uses v.2 syntax all the way through. Why wouldn't you want this? Commented May 24, 2022 at 19:51
  • 1
    But hey, it's your system, so you should whatever you want. I just wouldn't recommend anyone else to use the method you describe here. 😎 Commented May 24, 2022 at 19:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .