且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Docker 不遵循构建目录中的符号链接

更新时间:2023-11-19 13:35:10

Docker 将使用符号链接(至少在我的 linux 主机上构建时),但它复制的是符号链接本身,而不遵循链接.因此,您还需要将符号链接 target 单独包含在构建上下文中并复制到图像中.此上下文被发送到 docker 引擎,并且构建发生在容器内的该服务器上,因此任何指向该上下文之外的文件的链接都不会解析.您还希望这些链接是相对的,或者链接的绝对路径必须指向图像内的相同绝对路径.这是一个带有相对链接的示例,显示了上下文内外文件之间的差异:

Docker will work with symlinks (at least when building on my linux host), but what it copies is the symlink itself, without following the link. Therefore you also need the the symlink target to be separately included in the build context and copied into the image. This context is sent over to the docker engine and the build occurs on that server within containers, so any link to files outside of that context will not resolve. You will also want these links to be relative, or the absolution path of a link must point to the same absolute path inside the image. Here's an example with relative links showing the difference between files inside and outside the context:

$ ls -al
total 8
drwxr-xr-x  2 bmitch bmitch 4096 Mar  2 21:08 .
drwxr-xr-x 13 bmitch bmitch 4096 Mar  2 21:07 ..
lrwxrwxrwx  1 bmitch bmitch   11 Mar  2 21:08 outside.txt -> ../test.out
lrwxrwxrwx  1 bmitch bmitch   10 Mar  2 21:08 source.txt -> target.txt
-rw-r--r--  1 bmitch bmitch    0 Mar  2 21:08 target.txt

$ cat Dockerfile
FROM busybox
COPY . /build-context
WORKDIR /build-context
CMD find .

$ docker build -t test-symlink .
Sending build context to Docker daemon 3.584 kB
Step 1/4 : FROM busybox
 ---> 7968321274dc
Step 2/4 : COPY . /build-context
 ---> 8238dac16669
Removing intermediate container dd653dfdf7a4
Step 3/4 : WORKDIR /build-context
 ---> c1850cb52f0e
Removing intermediate container 7ee87e20d525
Step 4/4 : CMD find .
 ---> Running in e710e965d98c
 ---> fd57eb8f426b
Removing intermediate container e710e965d98c
Successfully built fd57eb8f426b

$ docker run test-symlink
.
./outside.txt
./Dockerfile
./source.txt
./target.txt

$ docker run -it --rm test-symlink /bin/sh
/build-context # ls -al
total 12
drwxr-xr-x    2 root     root          4096 Mar  3 02:09 .
drwxr-xr-x   20 root     root          4096 Mar  3 02:09 ..
-rw-r--r--    1 root     root            69 Mar  3 02:08 Dockerfile
lrwxrwxrwx    1 root     root            11 Mar  3 02:08 outside.txt -> ../test.out
lrwxrwxrwx    1 root     root            10 Mar  3 02:08 source.txt -> target.txt
-rw-r--r--    1 root     root             0 Mar  3 02:08 target.txt
/build-context # cat outside.txt
cat: can't open 'outside.txt': No such file or directory
/build-context # cat target.txt
/build-context # exit