Docker 不遵循构建目录中的符号链接 [英] Docker does not follow symlinks within build directory

查看:32
本文介绍了Docker 不遵循构建目录中的符号链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对一个应用程序进行 Dockerizing,该应用程序涉及通过 Clang 将二进制文件与其他 C 文件链接.我们维护二进制文件的符号链接版本,因为它们在整个代码库中使用.我的 Docker 构建目录包含整个代码库(包括源文件以及这些源文件的符号链接),当我执行诸如 cat [symlinked_file] 之类的操作时,Docker 会识别这些文件(即文件是cat 正确).但是,当我在我的 Makefile 中运行我的 Clang 命令时,它无法链接符号链接文件(这些在 Docker 中运行良好).然后我将原件复制到符号链接所在的目录中,替换符号链接,Docker 在构建时没有抛出任何错误.

I am Dockerizing an application which involves linking binaries with other C files via Clang. We maintain a symlinked version of the binaries since they are used throughout the codebase. My Docker build directory contains this entire codebase (including the source files as well as the symlinks to those source files), and Docker recognizes those files when I do things like cat [symlinked_file] (i.e. the file is cated properly). However, it fails to link the symlinked files when I run my Clang commands in my Makefile (these work fine not in Docker). I then copied the originals into the directory where the symlinks are, replacing the symlinks, and Docker did not throw any errors on build.

有谁知道如何解决这个问题?我需要在这里向 Docker 或 Clang 提供任何特殊命令吗?我不确定为什么 Clang 在 Docker 容器内的行为与在容器外的行为不同.我从 Ubuntu 16.04 基础映像运行以供参考.

Does anyone know how to get around this? Are there any special commands I need to give to Docker or Clang here? I am not sure why Clang is behaving differently inside a Docker container than outside of it. I am running from an Ubuntu 16.04 Base Image for reference.

推荐答案

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

这篇关于Docker 不遵循构建目录中的符号链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆