how can I mount local folder with dockerfile RUN --mount?

Viewed 1753

I'm trying to use the new RUN --mount option in docker's experimental feature set but am having difficulty getting it to do what I want which is...

project folder listing

hello.cpp
makefile
Dockerfile

And the Dockerfile looks like:

#syntax=docker/dockerfile:experimental
FROM ubuntu

RUN --mount=type=cache,target=/home,source=. make

CMD ["bash"]

and the build command looks like:

docker build -t myimage:latest .

basically, I want to preserve the advantage that make only compiles out of date targets which rules out using COPY . /home because this appears not too preserve the timestamps on the files copied across. make always makes everything.

N.B. I've simplified the --mount options to the minimal set, IRL I'd add uid=1000,gid=1000 or similar.

The error messages I get vary with how I set the options. Either docker build barfs with a 'directory not found' message, or make fails with 'no makefile found`.

I'm pretty sure I just don't know how to set the source and target values correctly, and am not finding the documentation helpful.

Thanks!

1 Answers

basically, I want to preserve the advantage that make only compiles out of date targets which rules out using COPY . /home because this appears not too preserve the timestamps on the files copied across.

I don't believe this is accurate. First, an example Dockerfile:

FROM busybox
COPY . /build-context
WORKDIR /build-context
CMD find .

I build that into an image called context:

$ docker build -f df.build-context -t context .
[+] Building 7.4s (8/8) FINISHED
 => [internal] load build definition from df.build-context         1.2s
 => => transferring dockerfile: 118B                               0.0s
 => [internal] load .dockerignore                                  0.7s
 => => transferring context: 34B                                   0.0s
 => [internal] load metadata for docker.io/library/busybox:latest  0.3s
 => CACHED [1/3] FROM docker.io/library/busybox                    0.0s
 => [internal] load build context                                  1.1s
 => => transferring context: 2.56kB                                0.4s
 => [2/3] COPY . /build-context                                    0.9s
 => [3/3] WORKDIR /build-context                                   0.6s
 => exporting to image                                             1.6s
 => => exporting layers                                            1.3s
 => => writing image sha256:c6971f5f817b746afd785c77d3...bcfb58f1  0.2s
 => => naming to docker.io/library/context                         0.1s

And if I look at files on the host and compare them to files that got included in the context, I'm seeing the last modified timestamp is identical (the only difference in display is from the difference in UTC and EDT timezones):

$ docker run -it --rm context stat hello.sh
  File: hello.sh
  Size: 29              Blocks: 8          IO Block: 4096   regular file
Device: fe03h/65027d    Inode: 24910346    Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-06-28 23:58:34.000000000
Modify: 2016-12-06 18:17:43.000000000
Change: 2019-06-28 23:58:32.000000000

$ stat hello.sh
  File: hello.sh
  Size: 29              Blocks: 8          IO Block: 4096   regular file
Device: fe03h/65027d    Inode: 16526503    Links: 1
Access: (0755/-rwxr-xr-x)  Uid: ( 1000/  bmitch)   Gid: ( 1000/  bmitch)
Access: 2019-06-28 19:58:30.984110011 -0400
Modify: 2016-12-06 13:17:43.937302516 -0500
Change: 2017-06-21 17:17:15.052283710 -0400
 Birth: -

From the man page on make:

The make program uses the makefile description and the last-modification times of the files to decide which of the files need to be updated.

You should check elsewhere for something changing your timestamps because if they are changing outside of docker, the RUN --mount will still include the broken last-modified timestamp.

Related