docker - 如何使用dockerfile RUN --mount挂载本地文件夹?

标签 docker makefile dockerfile

我正在尝试在Docker的实验性功能集中使用新的RUN --mount选项,但很难使其执行我想要的工作...

项目文件夹列表

hello.cpp
makefile
Dockerfile

Dockerfile看起来像:
#syntax=docker/dockerfile:experimental
FROM ubuntu

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

CMD ["bash"]

生成命令如下所示:
docker build -t myimage:latest .

基本上,我想保留make仅编译过期目标(使用COPY . /home排除)的优点,因为这似乎并不能保留跨文件复制的时间戳。 make始终可以完成所有工作。

N.B.我已将--mount选项简化为最小设置,IRL我将添加uid=1000,gid=1000或类似名称。

我收到的错误消息因我设置选项的方式而异。带有“未找到目录”消息的docker build barfs,或者make失败,显示“找不到makefile”。

我敢肯定,我只是不知道如何正确设置sourcetarget值,并且没有发现the documentation有用。

谢谢!

最佳答案

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.



我不认为这是正确的。首先,一个示例Dockerfile:
FROM busybox
COPY . /build-context
WORKDIR /build-context
CMD find .

我将其构建到称为上下文的图像中:
$ 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

而且,如果我查看主机上的文件并将它们与上下文中包含的文件进行比较,我会看到最后修改的时间戳是相同的(显示的唯一区别是UTC和EDT时区的区别):
$ 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: -

在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.



您应该检查其他地方是否更改了时间戳,因为如果它们在docker之外更改,则RUN --mount仍将包含损坏的上次修改的时间戳。

关于docker - 如何使用dockerfile RUN --mount挂载本地文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56813899/

相关文章:

Docker 构建无法计算缓存 key

Dockerfile VOLUME 定义挂载项目树

bash - 无法在 makefile 中捕获命令退出代码

c++ - 如何正确构建静态库?

c - 头文件和 makefile 的格式/用途是什么? (C)

jenkins - 将cloudbees ec2 docker镜像与docker中的docker结合

git - 将 git 存储库添加到 docker 镜像(工作流最佳实践)

windows - 如何仅针对Windows 7下载Docker CLI

docker - 为什么 Docker ADD 命令不复制这个文件?

docker - 在 Docker 中安装 GCP Profiler 代理时遇到问题