Docker
docker-compose
container
devops
configuration

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:

bash
docker inspect <container_name>

A more focused form:

bash
docker inspect --format '{{json .Config.Labels}}' <container_name>

Many Compose-managed containers carry labels such as:

  • com.docker.compose.project
  • com.docker.compose.service
  • com.docker.compose.project.working_dir
  • com.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

bash
docker inspect --format '{{ index .Config.Labels "com.docker.compose.project.config_files" }}' <container_name>

And for the working directory:

bash
docker inspect --format '{{ index .Config.Labels "com.docker.compose.project.working_dir" }}' <container_name>

Why the labels are not guaranteed

  • Older docker-compose versions and newer Compose plugin versions may use different labels.
  • Containers not started by Compose have no Compose metadata at all, including plain docker run containers.

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:

yaml
1services:
2  app:
3    image: nginx:latest
4    labels:
5      app.compose_source: "/srv/projects/sample/docker-compose.yml"

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.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design