Docker volumes during build time

Hi all,

I just wanted to confirm some behavior I’m seeing. Are volumes defined in the docker-compose.yml file usable at build time in any of the Dockerfiles? For example, lets say we have mounted a shared drive on the host machine to a location /prod, and we defined the following files:

version: '3'
      context: .
      dockerfile: /Dockerfile
      - "/prod:/prod"
    container_name: "my-web-service"
    restart: always


FROM rockylinux:9.3-minimal

RUN ls /prod # This line cannot access this volume


The RUN ls /prod line in the Dockerfile cannot access the volume created in the docker-compose.yml right?


When you build an image with compose, everything underneath the build key is used to configure the build. Furthermore, If the image key exists, it will be used to tag the created images.

The RUN instruction might provide a workaround, as it allows to bind files or folders into the build context:

High likely the source folder must be within the build context (in your case the folder where the compose.yml is located). It could look something like this:

RUN --mount=type=bind,source=prod,target=/prod \
    ls /prod

I am not 100% sure if the source is really restricted to the build context, but I would be surprised if it wasn’t.

1 Like

Unless it changed, I can confirm it is. But I don’t think it will change, because that would mean anyone could share a long Dockerfile, ask a user to build the image and that way mount anything from the filesystem, even system folders and send it to a remote server. A compose file could do the same, but an image has to be reproducable and mounting anything outside the build context would make it harder.


Thank you both for the confirmation.

Is this something that is mentioned explicitly in the docs? If not, maybe it could be added? I’m not incredibly familiar with Docker and this behavior was a stumbling block for me that caused several hours of wasted time.

What I’m asking for is a section of the docs that mentions that storage mounts are not available in the build context (Dockerfile).

The documentation covers the supported elements:

The absence of content usually means it is either not relevant for this context, or simply doesn’t exist.