Docker: How do i find the docker-compose.yml file location of a running container?
Assume the following scenario! you are the on-call engineer of a company and they asked you to support them on a docker issue, the problem…
Assume the following scenario! you are the on-call engineer of a company and they asked you to support them on a docker issue, the problem is that you are not familiar with the environment and you see the running container using docker ps but you cannot locate the docker-compose.yml file, and make things worse there are multiple docker-compose.yml files with ambiguous naming, you feel anxious already ? dont worry! i will show you a simple and fast way to find the file!
Identify the container ID
First step is to identify the CONTAINER ID, to do this use the docker ps command, will generate something similar to the following output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6fac5a8225e4 telegraf:latest "/entrypoint.sh tele…" 7 days ago Up 7 days telegraf
bd0f34c2cbfc influxdb:1.8 "/entrypoint.sh infl…" 7 days ago Up 7 days influxdbUsing docker inspect
Next step (and final) is to use the docker inspect command, this command gives you information about images, containers, networks and other docker insides, in our case we will use it to get info about the container which contains the docker-compose.yml location!
Running this command will show all details related to this container, probably the output will be too much and too hard to read!
docker inspect 6fac5a8225e4How to extract only the Config.Labels
Fortunately docker inspect allows the usage of --format option which allows us to select what part of the output we want to filter, in our case we want the .Config.Labels formated as json output
docker inspect --format='{{json .Config.Labels}}' 203df74e3ba6This command will generate the following output which is not too much but a bit hard to read!
{"com.docker.compose.config-hash":"45139ec8caa3bbc0e178f3c07cd4985b19f6d1edd954c87b0f7c563817d62a9b","com.docker.compose.container-number":"1","com.docker.compose.depends_on":"","com.docker.compose.image":"sha256:2e936f7bddcc399a7da3ae9198f82eefba6c2c76eb31c7abe91d9c875fdb515b","com.docker.compose.oneoff":"False","com.docker.compose.project":"sshtunnel","com.docker.compose.project.config_files":"/data/docker/sshtunnel/docker-compose.yml","com.docker.compose.project.working_dir":"/data/docker/sshtunnel","com.docker.compose.service":"sshtunnel","com.docker.compose.version":"2.24.6"}Making the output more readable
Here you can use two tricks, the one is to use jq to format the output in a more readable form (you have found the docker-compose.yml line? ;) )
docker inspect --format='{{json .Config.Labels}}' 203df74e3ba6 | jq .
{
"com.docker.compose.config-hash": "45139ec8caa3bbc0e178f3c07cd4985b19f6d1edd954c87b0f7c563817d62a9b",
"com.docker.compose.container-number": "1",
"com.docker.compose.depends_on": "",
"com.docker.compose.image": "sha256:2e936f7bddcc399a7da3ae9198f82eefba6c2c76eb31c7abe91d9c875fdb515b",
"com.docker.compose.oneoff": "False",
"com.docker.compose.project": "sshtunnel",
"com.docker.compose.project.config_files": "/data/docker/sshtunnel/docker-compose.yml",
"com.docker.compose.project.working_dir": "/data/docker/sshtunnel",
"com.docker.compose.service": "sshtunnel",
"com.docker.compose.version": "2.24.6"
}The second option in case you dont have jq installed or you cannot install it is to use the tr command. In this case we use tr to replace the , character with a newline \n in order to make things more readable!
docker inspect --format='{{json .Config.Labels}}' 203df74e3ba6 | tr -s "," "\n"
{"com.docker.compose.config-hash":"45139ec8caa3bbc0e178f3c07cd4985b19f6d1edd954c87b0f7c563817d62a9b"
"com.docker.compose.container-number":"1"
"com.docker.compose.depends_on":""
"com.docker.compose.image":"sha256:2e936f7bddcc399a7da3ae9198f82eefba6c2c76eb31c7abe91d9c875fdb515b"
"com.docker.compose.oneoff":"False"
"com.docker.compose.project":"sshtunnel"
"com.docker.compose.project.config_files":"/data/docker/sshtunnel/docker-compose.yml"
"com.docker.compose.project.working_dir":"/data/docker/sshtunnel"
"com.docker.compose.service":"sshtunnel"
"com.docker.compose.version":"2.24.6"}Conclusion
As you notticed, extracting the docker-compose.yml location from a running container is not very hard, but it throws a lot of output and this might make things difficult in a situation that is already stressed if you running out of time and you need to get the location very fast! so i hope this article apart from the docker inspect command show you some quick tricks to filter out the “noise” and get only the config labels part that contains the docker-compose.yml location.