制作一个依赖关系容器并将其卷安装在其他容器上 [英] Making a dependencies container and mount its volumes on other containers

查看:74
本文介绍了制作一个依赖关系容器并将其卷安装在其他容器上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,就是我的容器变得太重,其中许多容器具有很多相同的依赖关系。

I have a problem where my containers became too heavy and many of them have a lot of the same dependencies.

我想制作一个基本容器,该容器将安装并保存所有依赖项,然后让其他容器指向该基本容器上的依赖项目录(使用卷) 。

I would like to make a base container that would install and hold all the dependencies and then have the other containers pointing to the dependencies dir (using volumes) on that base container.

我试图对此做一个小的POC,我首先尝试让一个容器安装python软件包,然后另一个容器使用该模块运行python脚本。

Im trying to do a small POC on this and I started by trying to have one container installing a python package, then the other container running a python script using that module.

我想在主机上创建一个目录,该目录将挂载在所有容器上,并将包含所有需要的数据和依赖项。

I'm thinking that I will make a directory in the host that will be mounted on all containers, and will contain all the data and dependencies that are needed.

我应该注意,即使这样做可能更好,我也无法使用docker compose。

I should note that I can't use docker compose even though that probably be better to.

这是我的基本容器的Dockerfile:

This is the Dockerfile for my base container:

FROM python:3.6-slim

RUN apt-get update && apt-get install -y vim

RUN pip install --install-option="--prefix=/volumes/shared/dependencies" deepdiff 

CMD tail -f /dev/null

您可以看到pip将安装到 / volumes /共享/依赖项目录。

You can see that pip will install to the /volumes/shared/dependencies dir.

我是这样运行的:

docker build -t base_container .
docker run -ti -v "$PWD/shared/base_dependencies":/volumes/shared/dependencies base_container

现在,如果我进入 / volumes / shared / dependencies 的容器,我会看到放在主机目录中的文件,但看不到已安装的软件包。另一方面,如果主机目录为空,则会看到已安装的软件包。

Now if I go into the container to /volumes/shared/dependencies I see the files I put in the host dir, but not the installed package. On the other hand, if the host dir is empty, I see the installed package.

我还尝试应用2个卷(其中一个用于输入文件,另一个用于容器将创建的文件)

I also tried applying 2 volumes (one for the files going in and ones for the files that the container will create)

在这种情况下如何获得双向容量,对此发生原因的解释也很好。

How can I get a two way volume in that situation, an explanation on why this is happening will also be nice.

推荐答案

对卷执行 docker run 时,它会首先在主机上创建目录(如果该目录不存在),然后挂载该卷,从而进行读取。所以事情是,容器中的目标目录将被主机上的目标目录替换,从而导致目录为空。

When you do docker run with a volume, it will first create the directory on your host machine if it does not exist, then mount the volume, thus reading it. So the thing is, the target directory in the container will be replaced by the one on the host, resulting in an empty directory.

只需在运行时复制依赖项,并且不再需要带有 tail -f 的容器

Just copy the dependency at "runtime", and you don't need the container anymore with tail -f

FROM python:3.6-slim

RUN apt-get update && apt-get install -y vim

RUN pip install --install-option="--prefix=/temp" deepdiff 

CMD cp -pr /temp /volumes/shared/dependencies

这篇关于制作一个依赖关系容器并将其卷安装在其他容器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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