compose icon indicating copy to clipboard operation
compose copied to clipboard

Variable for referencing the absolute path to where the docker-compose.yml file resides

Open Jasper-Ben opened this issue 5 years ago • 3 comments

/!\ If your request is about evolving the compose file format, please report on the Compose Specification

Is your feature request related to a problem? Please describe. To my knowledge there currently is no way of mounting the "TOP_DIR" path of the docker-compose.yml to the exact same absolute path within the container, independent from the current host working directory.

Example: On the host I have the docker-compose.yml residing in an arbitrary (not hardcoded) absolute path, e.g. /foo/docker-compose.yml. I'd like to mount the folder containing the docker-compose.yml at the same absolute path within the docker container. Basically this, but without hardcoded paths:

volumes:
  /foo:/foo

Describe the solution you'd like I'd like to propose a variable which contains the absolute path to the docker-compose.yml file's basedir, lets call it "TOP_DIR", which would allow for the following:

volumes:
  ${TOP_DIR}:${TOP_DIR}

Describe alternatives you've considered A semi-working solution can be achieved using the PWD variable:

volumes:
  ${PWD}:${PWD}

However, this solution relies on executing the docker-compose command from within the correct directory, e.g. running docker-compose from within /foo/bar would result in the following mount: /foo/bar:/foo/bar, which we obviously do not want in this scenario.

. on the host-side of a volume mount resolves to the absolute path to the docker-compose.yml file's directory at runtime, however as far as I can tell there is no way to parse this path to the container side of a volume mount. The following will not work, as docker-compose expects an absolute path on the container side:

volume:
  - ".:."

Jasper-Ben avatar Jun 24 '21 10:06 Jasper-Ben

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jan 03 '22 20:01 stale[bot]

This issue has been automatically closed because it had not recent activity during the stale period.

stale[bot] avatar Apr 17 '22 18:04 stale[bot]

This feature would be very useful for writing docker compose.yaml files in subdirectories of monorepos that also want to use the VS Code dev containers extension, which calls docker compose with absolute path -f arguments to compose.yaml files in subdirectories from the top-level of the repository. As is, I can't find a way to write a single compose.yaml that mounts the whole repository (and nothing above it) whether calling docker compose from the subdirectory or from the top-level.

I thought the predefined COMPOSE_FILE environment variable would give me a path to a working solution, but then discovered it's only defined if defined by the user: that is, it works as an input COMPOSE_FILE=/home/codym48/code/my-repo/subdir/compose.yaml docker compose run, but isn't set by docker compose itself if the user just calls docker compose run.

Codym48 avatar Jun 22 '25 04:06 Codym48