Why does docker-compose ps not run correctly in a different directory?

Viewed 1188

Running through the docker-compose walkthrough.

At one point it says

The docker-compose run command allows you to run one-off commands for your services. For example, to see what environment variables are available to the web service:

$ docker-compose run web env

When I first ran this my 2nd bash terminal was open in a different directory:

$ docker-compose run web env
ERROR: No such service: web

Odd, so I ran

$ docker-compose ps
Name   Command   State   Ports
------------------------------

I was perplexed. So I ran the command in the other window:

āœ— docker-compose ps
       Name                      Command               State           Ports         
-------------------------------------------------------------------------------------
composetest_redis_1   docker-entrypoint.sh redis ...   Up      6379/tcp              
composetest_web_1     python app.py                    Up      0.0.0.0:5000->5000/tcp

OK. What's the difference? Oh, the directory.

The thing I don't get is: why? Isn't docker-compose ps talking to a service to figure out who's running? Clearly not. So what's happening here? Best guess is that docker is reading the compose file then using the names of the services etc to filter the list of running docker images. It's just a bit surprising that it wouldn't return all the running containers if it didn't find a compose file in the same directory that the command is being run in.

1 Answers

docker-compose is project based. It needs a docker-compose.yml file in the current directory or any parent directory. It will only show service related to current applicable docker-compose.yml. If you want to see everything that is running you use docker ps and docker exec for running a command. But if you want to do it in a docker-compose project then your current directory is important, try and keep it as the one where docker-compose.yml direct exists in the same directory

Related