gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 19:57:37 24 4
gpt4 key购买 nike

我正在尝试在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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com