My OS is Ubuntu 20.04.3 LTS.
I’ve installed docker compose V2, and I can access it from the command line regularly:
$ docker compose version
Docker Compose version v2.2.2
I’ve also installed compose-switch according to the manual instructions here: https://docs.docker.com/compose/cli-command/#compose-switch and it’s working fine:
$ docker-compose version
Docker Compose version v2.2.2
But if I use sudo neither will work:
$ sudo docker compose version
docker: 'compose' is not a docker command.
See 'docker --help'
$ sudo docker-compose version
docker: 'compose' is not a docker command.
See 'docker --help'
docker version
is the same with or without sudo:
Version: 20.10.12
API version: 1.41
So, how can I get docker compose working with sudo?
2
Answers
I had installed docker-compose under my user's home directory. I had to move the file docker-compose from ~/.docker/cli-plugins to /usr/local/lib/docker/cli-plugins
And now everything works as expected.
The docker command you are running as your local user must be calling a different binary than what it calls when running as another user (i.e. root user).
When you invoke a command using sudo, it will by default use the root user shell environment which includes the
PATH
env variable.I suspect you will see a different path output when running these two commands:
type docker
sudo type docker