Get list of containers/services of docker-compose from inside a container

7.7k views Asked by At

There is a smart way to get the list of services (or containers) of docker-compose files from inside of a container.

I would prefer not to using volumes, or passing statically the compose-file to container and parse it or pass a list with these infromations from environment variable.

One perfect result is given by docker-compose config --service, that returns the list of all services of compose but I don't know how to pass it to my container from the host.

(I would like get the list of all containers that they compose my application so I can monitor which go up and down with using registrator and consul).

2

There are 2 answers

0
Hossein Mayboudi On

Please follow the instruction:

  1. Install yq on your machine. for instance, if your operating system is macOS/OSX brew install yq.
  2. Then run one of these commands: docker-compose config | yq '.services[]|key' or docker compose config | yq '.services[]|key'.

you will find the list of all available services.

0
srr7 On

If you follow 2 space in your docker-compose for defining services, you can get list of services by following command

docker-compose config  |grep -E '^  [a-zA-Z_-]+'