Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker compose not recognized when using sudo

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?

like image 780
Neil Haskins Avatar asked Oct 25 '25 03:10

Neil Haskins


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

$ sudo mkdir -p /usr/local/lib/docker/cli-plugins
$ sudo mv /home/<username>/.docker/cli-plugins/docker-compose /usr/local/lib/docker/cli-plugins/docker-compose

And now everything works as expected.

like image 122
Neil Haskins Avatar answered Oct 27 '25 17:10

Neil Haskins


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

like image 29
Jack Avatar answered Oct 27 '25 17:10

Jack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!