Find the docker-compose.yml location from a running container
Codemia Knowledge Hub ·
Inspect the container from the Docker host and read its Compose labels: com.docker.compose.project.config_files often points to the Compose file path, and com.docker.compose.project.working_dir to the project directory. Inside the container there is usually no way to find out, because the Compose file is a host-side input to the docker compose command and is not mounted into the container.
Inside the container: usually no answer
If you docker exec into a running container, do not expect a built-in file or environment variable that says "this container came from /path/to/docker-compose.yml." Compose does not generally copy that file into the container. From inside, the answer is usually no. From the host, via container metadata, it is sometimes yes.
Read the Compose labels on the host
Modern Docker Compose typically adds labels to the containers it creates:
A more focused form:
Many Compose-managed containers carry labels such as:
com.docker.compose.projectcom.docker.compose.servicecom.docker.compose.project.working_dircom.docker.compose.project.config_files
If com.docker.compose.project.config_files is present, it can point to the Compose file path or paths used on the host.
Commands to print the file path and working directory
And for the working directory:
Why the labels are not guaranteed
- Older
docker-composeversions and newer Compose plugin versions may use different labels. - Containers not started by Compose have no Compose metadata at all, including plain
docker runcontainers.
So Compose-managed containers often carry helpful host-side labels, but this is metadata, not a universal feature.
Record the path yourself with a label
If the Compose file path matters to your workflow, record it explicitly:
Now the metadata is under your control instead of depending on Compose internals.
If the application itself needs deployment metadata, pass it deliberately through environment variables, labels, mounted config files, or deployment manifests stored elsewhere. That is far more reliable than reverse-engineering the launch source later.
When the project root is enough
Sometimes you only need the project identity or working directory, not the exact file path. If you can retrieve the Compose project name, the working directory and the service name, you may not need the YAML path at all.
Related reading
- Get Docker container id from container name
- Get Environment Variable from Docker Container
- Get replica set of the deployment
- Get the image and SHA image ID of images in pod on Kubernetes deployment
- Get environment variable from kubernetes pod?
- Get exception description and stack trace which caused an exception, all as a string
- Get YAML for deployed Kubernetes services?
- Getting Broker may not be available error when spring boot container tries to connect kafka container
System design interview problems on this topic
- Design a Container Orchestration System: solution and walkthrough
- Design a Code Deployment System: solution and walkthrough
- Design GitHub Actions: solution and walkthrough
- Design a Serverless Architecture Framework: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.