Docker和符号链接 [英] Docker and symlinks

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

问题描述

我有一个这样的存储库设置:

I've got a repo set up like this:

/config
   config.json
/worker-a
   Dockerfile
   <symlink to config.json>
   /code
/worker-b
   Dockerfile
   <symlink to config.json>
   /code

但是,构建镜像失败,因为Docker无法处理符号链接。我应该说我的项目远比这复杂,因此重组目录不是一个好选择。我该如何处理这种情况?

However, building the images fails, because Docker can't handle the symlinks. I should mention my project is far more complicated than this, so restructuring directories isn't a great option. How do I deal with this situation?

推荐答案

Docker 不支持在构建上下文外部进行文件链接

以下是一些使用容器中的共享文件:

Here are some different methods for using a shared file in a container:

创建 Dockerfile 表示包含共享配置/文件的基本 worker-config 映像。

Create a Dockerfile for the base worker-config image that includes the shared config/files.

COPY config.json /config.json

将图像构建并标记为 worker-config

docker build -t worker-config:latest .

为基本 worker-config 图像来源所有您的工作人员 Dockerfile s

Source the base worker-config image for all your worker Dockerfiles

FROM worker-config:latest



构建脚本



使用脚本来推送

Build script

Use a script to push the common config to each of your worker containers.

./ build worker-n

#!/bin/sh
set -uex 
rundir=$(readlink -f "${0%/*}")
container=$(shift)
cd "$rundir/$container"
cp ../config/config.json ./config-docker.json
docker build "$@" .



从URL构建



拉配置从所有 worker-n 构建的通用URL中删除。

Build from URL

Pull the config from a common URL for all worker-n builds.

ADD http://somehost/config.json /



增加映像构建上下文的范围



通过从包含共享文件和特定容器文件的父目录。

Increase the scope of the image build context

Include the symlink target files in the build context by building from a parent directory that includes both the shared files and specific container files.

cd ..
docker build -f worker-a/Dockerfile .

您在 Dockerfile中引用的所有源路径还必须更改以匹配新的构建上下文:

All the source paths you reference in a Dockerfile must also change to match the new build context:

COPY workerathing /app

成为

COPY worker-a/workerathing /app

如果您有一个,使用此方法可以使 all 的构建上下文变大大型构建环境,因为它们全部共享。它会减慢构建速度,尤其是对远程Docker构建服务器的构建。

Using this method can make all build contexts large if you have one large build context, as they all become shared. It can slow down builds, especially to remote Docker build servers.

像这样的卷只能用作目录,因此您可以

Volumes like this only work as directories, so you can't specify a file like you could when mounting a file from the host to container.

docker volume create --name=worker-cfg-vol
docker run -v worker-cfg-vol:/config worker-config cp config.json /config

docker run -v worker-cfg-vol:/config:/config worker-a



从数据容器安装配置目录



同样,目录仅与上面的基本相同。但是,这将自动将文件从目标目录复制到新创建的共享卷中。

Mount config directory from data container

Again, directories only as it's basically the same as above. This will automatically copy files from the destination directory into the newly created shared volume though.

docker create --name wcc -v /config worker-config /bin/true
docker run --volumes-from wcc worker-a



从主机安装配置文件



Mount config file from host

docker run -v /app/config/config.json:/config.json worker-a

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

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