69

I have a few running docker containers created by executing docker-compose up.

Is there any way to get the exact file path of the corresponding docker-compose.yml file used to start these containers, just by inspecting the running containers?

As far as I can see, docker inspect CONTAINER_NAME does not provide this information, nor does docker-compose provide a method to get compose-related information from a running container.

What I'd like to do in a script:

  • list certain running containers on a docker host
  • get the corresponding docker-compose.yml file locations
  • use docker-compose to restart all containers of the corresponding docker-compose projects at once

6 Answers 6

53

The answer to this question seems to have changed with new versions of docker-compose. There is a label "com.docker.compose.project.working_dir": "/var/opt/docker", that points to the directory where I started docker-compose. I have not checked if that is pwd or the actual location of the docker-compose.yml file.

This got me interesting information about docker-compose:

samuel@vmhost1:~$ docker inspect fc440a1afbaa | grep com.docker.compose
                "com.docker.compose.config-hash": "89069285a4783b79b421ea84f2b652becbdee148fbad095a6d9d85aab67ececc",
                "com.docker.compose.container-number": "1",
                "com.docker.compose.oneoff": "False",
                "com.docker.compose.project": "docker",
                "com.docker.compose.project.config_files": "docker-compose.yml",
                "com.docker.compose.project.working_dir": "/var/opt/docker",
                "com.docker.compose.service": "jenkins",
                "com.docker.compose.version": "1.25.0"
samuel@vmhost1:~$ 

I'm running docker-compose.yml configuration version 3.6

6
  • 3
    The working_dir is the context specified in the compose file, it doesn't point to location of compose file
    – Guerrilla
    Sep 23, 2020 at 12:51
  • @Guerrilla: What do you mean?
    – at54321
    Jul 8, 2021 at 20:39
  • 1
    @at54321 The context and the compose file are not the same thing. The context can be located in a different location to the compose file
    – Guerrilla
    Jul 8, 2021 at 21:05
  • 1
    this should be the answer
    – itinance
    Jul 12, 2021 at 12:29
  • 1
    @RobBlanchard the context is defined inside the compose file (i assume if it's not defined it will take same dir). try to set the context to a different location inside the compose file and see if it still works for you. I keep my compose files together in a separate location and it didn't work for me when setting a context location inside compose file.
    – Guerrilla
    Apr 5, 2022 at 19:36
15

It is not currently possible.

As an alternative might find the following helpful:

  • Use docker ps -a | grep <certain_container>
  • Use locate docker-compose.yml and find the one that you want
  • Use docker-compose restart (do docker-compose to see option)
1
  • 1
    As of 2022, with Docker 20.10, I was able to run cd / and find . -name "docker-compose.yml" and locate my missing compose files (just cat the returned paths).
    – qJake
    Nov 27, 2022 at 17:05
14

You can identify it using the inspect command, as follow:

docker inspect <container_id> | grep compose
1
  • 1
    Simple command and it works!
    – Fad Lee
    Jul 4, 2022 at 20:36
11

Update: Since this was asked, docker compose v2 was released, which is written in Go and accessible from docker compose instead of docker-compose (there may also be a shim directing docker-compose to this new version depending on your install). This version now embeds the directory into the image labels that you can retrieve with:

docker container inspect ${container_name_or_id} \
  --format '{{ index .Config.Labels "com.docker.compose.project.working_dir" }}'

This isn't perfect for the OP's request since there may be more than one compose file, the file could be located in a different directory from where compose was run, and it doesn't capture things like environment variables or profiles that may modify how compose starts the project. However I suspect it gets most people close enough to find the source.

If you're using an older version of compose, you can use one of the options in the original answer below:


As far as I can see, docker inspect CONTAINER_NAME does not provide this information, nor does docker-compose provide a method to get compose-related information from a running container.

From an already running container that you do not control, the information is not there. You can infer the location using bind mount directories if the container creates any host mounts to relative directories. Otherwise, it's possible to deploy containers without compose, and it's possible to use compose without a compose file on the filesystem (piped via stdin), and compose does not store these details on running containers for you.


What I'd like to do in a script:

  • list certain running containers on a docker host
  • get the corresponding docker-compose.yml file locations
  • use docker-compose to restart all containers of the corresponding docker-compose projects at once

If you just want to run a restart on all containers in the same project, you don't need the first two steps, or even docker-compose. Instead, you can run:

docker ps --filter "label=com.docker.compose.project=${your_compose_project}" -q \
| xargs docker restart

Which uses a label docker-compose adds to each project it deploys.


If you want to proactively store the compose file location for later use, you can inject that as a label in your compose file:

version: '2'
services:
  test:
    image: busybox
    command: tail -f /dev/null
    labels:
      COMPOSE_PATH: ${PWD} # many Linux shells define the PWD variable

If your shell does not set a ${PWD} environment variable, you can start compose with:

PWD=$(pwd) docker-compose up -d

Then you can later inspect containers for this label's value with:

docker inspect --format '{{.Config.Labels.COMPOSE_PATH}}' ${your_container_id}

And you can chain a filter and inspect command together to find the path for a specific project:

docker ps --filter "label=com.docker.compose.project=${your_compose_project}" -q \
| xargs docker inspect --format '{{.Config.Labels.COMPOSE_PATH}}'
4
  • Bad idea. With pwd you can never user docker-compose -f ...
    – A.B.
    Sep 25, 2019 at 10:49
  • @A.B. the point of the pwd section was that you can inject a label into your containers with the details you want later. If you override the compose file, inject that as a label that you manage.
    – BMitch
    Sep 25, 2019 at 11:06
  • You can an its even there by default. I was going to suggest curl --unix-socket /var/run/docker.sock http://localhost/containers/json | jq .[].Labels | grep com.docker.compose.project.working_dir | uniq but it's the same idea as your answer.
    – Utopiah
    Dec 23, 2021 at 21:35
  • 1
    @Utopiah that's compose v2, it didn't exist when I first answered this.
    – BMitch
    Dec 23, 2021 at 22:25
5

you know, your question turns to be a useful answer to the same issue I have. I used docker inspect <containerID> and then it gave me the location that I should look into. specifically in these lines:

HostConfig": {
            "Binds": [
....
...
],
2

If you mounted a local volume, e.g ./data then inspecting the container will give you the path, e.g docker inspect peertube_peertube_1 | jq .[0].HostConfig.Binds. It doesn't work for containers without volumes but it's rare enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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