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

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

问题描述

我有一个问题,我的容器变得太重,并且其中许多具有许多相同的依赖项.

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/shared/dependencies 目录.

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天全站免登陆