如何在父docker共享docker.sock创建的子docker中装入卷 [英] How to mount volume inside child docker created by parent docker sharing docker.sock

查看:75
本文介绍了如何在父docker共享docker.sock创建的子docker中装入卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个包装容器,以使用无法修改的docker-compose构建和运行一组容器.docker-compose会挂载多个卷,但是从包装docker内部启动docker-compose时,由于docker .sock已被卷挂为主机的docker.sock,因此仍会从主机上挂载这些卷.

I am trying to create a wrapper container to build and run a set of containers using a docker-compose I cannot modify. The docker-compose mounts several volumes, but when starting the docker-compose from inside of the wrapper docker, the volumes are still mounted from the host since the docker .sock is volume mounted to be the host's docker.sock.

由于我也想避免 volume-from ,因为我无法编辑前面提到的docker-compose文件.

I would also like to avoid volume-from since I cannot edit the docker-compose file mentioned previously.

是否有办法让此代码段正确使用父码头工人的文件,而不是转到主机文件系统并从那里安装它?

Is there a way to get this snippet to correctly use the parent docker's file instead of going to the host filesystem and mounting it from there?

FROM docker:latest

RUN mkdir -p /tmp/parent/ && echo "This is from the parent docker" > /tmp/parent/parent.txt
CMD docker run -v /tmp/parent/parent.txt:/root/parent.txt --rm ubuntu:18.04 bash -c "cat /root/parent.txt"

使用与此类似的命令运行时:

when run with a command akin to this:

docker build -t parent . && docker run --rm -v /var/run/docker.sock:/var/run/docker.sock parent

推荐答案

在主机映像和docker映像内部使您的路径相同,例如

Make your paths the same on the host and inside of the docker image, e.g.

docker run -v /var/run/docker.sock:/var/run/docker.sock \
  -v /home/user:/home/user -w /home/user/project parent_image ...

通过以/home/user的身份将卷安装在映像内的同一位置,当与docker套接字通信时,诸如 docker-compose up 之类的带有相对绑定安装的命令将使用容器路径名,它将与主机上的路径匹配.

By mounting the volume as /home/user in the same location inside the image, a command like docker-compose up with relative bind mounts will use the container path names when talking to the docker socket, which will match the paths on the host.

这篇关于如何在父docker共享docker.sock创建的子docker中装入卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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