I want to make a folder on my host machine available during a RUN
statement. That is, similar to the effect of a container run with -v
:
docker run -v /path/on/host:/path/in/container mycontainer:tag
In the container this gives me /path/in/container
with all files/folder in path/on/host
.
To this end I am trying the experimental mount options from https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/experimental.md:
RUN --mount=type=bind,target=/path/on/host
This gives me a folder /path/on/host
during the RUN
.
I then have two problems:
I can ls
files inside /path/on/host
, but not use them (e.g. cat
them). I have tried changing type
to e.g. cache
and using source
like in https://devops.stackexchange.com/questions/6078/in-a-dockerfile-is-there-a-way-to-avoid-copying-files-to-make-them-accessible-t, but I cannot make it work.
I cannot figure out how to have a different path inside the "RUN
image", that is, /path/in/container
instead of /path/on/host
.
I think you have misunderstood what the RUN --mount=type=bind...
syntax is for. From the documentation:
This mount type allows binding directories (read-only) in the context or in an image to the build container.
In other words, this does not permit you to access arbitrary host directories in the build stage. It is not an analog to the -v
command line option on docker run
. It only permits you to:
So for example I can do this do mount a directory from one build stage into a subsequent build stage:
# syntax=docker/dockerfile:experimental
FROM centos AS centos
FROM alpine
RUN --mount=type=bind,from=centos,source=/,target=/centos ls /centos > /root/centos.txt
Or if I have a directory named example
in my build context, I can do this to mount it during the build process:
# syntax=docker/dockerfile:experimental
FROM centos AS centos
FROM alpine
RUN --mount=type=bind,source=example,target=/data cp /data/* /root/
The syntax you're using (with no from
specified)...
RUN --mount=type=bind,target=/path/on/host
...simply mounts the root of your build context on /path/on/host
inside the container. Remember that target
specifies the mountpoint inside the container. E.g., if my build context looks like this:
.
├── Dockerfile
└── example
└── README.md
And example/README.md
contains:
This is a test.
And the Dockerfile
contains a RUN
option similar to what you're using:
# syntax=docker/dockerfile:experimental
FROM centos AS centos
FROM alpine
RUN --mount=type=bind,target=/data cat /data/example/README.md > /root/README.md
Then when the image is built, /root/README.md
has the contents of example/README.md
.