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

3

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

Kain answered 8/6, 2017 at 8:44 Comment(4)
Isn't the consul API enough? You can query by service nameZirkle
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.Kain
@Kain Did you ever find the answer to this?Abduct
This post is old now, but wouldn't mounting the docker socket work just fine for this?Clubwoman
T
4
docker compose config --services
Tinkling answered 8/4, 2024 at 23:34 Comment(0)
B
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.

Busby answered 21/7, 2023 at 6:32 Comment(0)
R
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_-]+'
Router answered 7/3, 2024 at 6:32 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.