Get docker-compose.yml file location from running container?

南笙酒味 提交于 2020-08-01 09:29:31

问题


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

回答1:


I think what you want is impossible at a certain point, the only things you can do in my opinion is:

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



回答2:


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}}'



回答3:


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": [
....
...
],


来源:https://stackoverflow.com/questions/42108163/get-docker-compose-yml-file-location-from-running-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!