12

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).

4
  • Isn't the consul API enough? You can query by service name
    – Robert
    Commented Jun 8, 2017 at 12:21
  • if a container not goes up, i will never know nothing about it , is right? so i wish have a list of all container that compose my application..i don't want querying for a specific service. Maybe i can do that with consul..but i don't know how. Thanks. Commented Jun 8, 2017 at 13:45
  • @user2972221 Did you ever find the answer to this? Commented Oct 9, 2018 at 13:10
  • This post is old now, but wouldn't mounting the docker socket work just fine for this?
    – Zac Anger
    Commented Jan 12, 2021 at 21:52

3 Answers 3

4
docker compose config --services
3

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

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_-]+'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.