为多个python应用程序重用Docker映像 [英] Reuse Docker Images for multiple python applications

查看:65
本文介绍了为多个python应用程序重用Docker映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对整个Docker领域来说还是一个新手。实际上,我正在尝试为不同的python机器学习应用程序建立环境,这些环境应在自己的docker容器中彼此独立运行。由于我不太了解使用基本映像并尝试扩展这些基本映像的方式,因此我为每个新应用程序使用了一个dockerfile,该文件定义了我使用的不同软件包。它们都有一个共同点-它们使用命令 FROM python:3.6-slim 作为基础。

I am quite new to the whole world of Docker. Actually I am trying to establish an environment for different python machine learning applications which should act independently from each other in an own docker container. Since I dont really understood the way of using base images and trying to extend those base images I using a dockerfile for each new application which defines the different packages that I use. They all have in one thing in common - they use the command FROM python:3.6-slim as the base.

我正在寻找一个我可以轻松地扩展此基础映像以形成一个新映像的起点或方式,该映像包含每个应用程序需要的单个软件包,以节省磁盘空间。现在,每个图像的文件大小约为。 1GB,希望这可以是减少数量的解决方案。

I am looking for a starting point or way that I can easily extend this base image to form a new image which contains the individual packages that each of the application needs in order to save disk space. Right now, each of the images has a file size of approx. 1gb and hopefully this could be a solution to reduce the amount.

推荐答案

无需详细介绍Docker的不同存储后端解决方案(请检查 Docker-关于存储驱动程序供参考),docker重用了图像的所有共享中间点。

Without going into details about the different storage backend solutions for Docker (check Docker - About Storage Drivers for reference), docker reuses all the shared intermediate points of an image.

话虽如此,即使您在 docker映像中看到输出的 [1.17 GB ,1.17 GB,1.17 GB,138MB,918MB] 并不意味着正在使用存储中的总和。我们可以这样说:

Having said that, even though you see in the docker images output [1.17 GB, 1.17 GB, 1.17 GB, 138MB, 918MB] it does not mean that is using the sum in your storage. We can say the following:

sum(`docker images`) <= space-in-disk

Dockerfile 中的每个步骤都会创建一个层。

Each of the steps in the Dockerfile creates a layer.

让我们采用以下项目结构:

Let's take the following project structure:

├── common-requirements.txt
├── Dockerfile.1
├── Dockerfile.2
├── project1
│   ├── requirements.txt
│   └── setup.py
└── project2
    ├── requirements.txt
    └── setup.py

使用 Dockerfile.1

FROM python:3.6-slim
# - here we have a layer from python:3.6-slim -

# 1. Copy requirements and install dependencies
# we do this first because we assume that requirements.txt changes 
# less than the code
COPY ./common-requirements.txt /requirements.txt
RUN pip install -r requirements
# - here we have a layer from python:3.6-slim + your own requirements-

# 2. Install your python package in project1
COPY ./project1 /code
RUN pip install -e /code
# - here we have a layer from python:3.6-slim + your own requirements
# + the code install

CMD ["my-app-exec-1"]

使用 Dockerfile.2

FROM python:3.6-slim
# - here we have a layer from python:3.6-slim -

# 1. Copy requirements and install dependencies
# we do this first because we assume that requirements.txt changes 
# less than the code
COPY ./common-requirements.txt /requirements.txt
RUN pip install -r requirements
# == here we have a layer from python:3.6-slim + your own requirements ==
# == both containers are going to share the layers until here ==
# 2. Install your python package in project1
COPY ./project2 /code
RUN pip install -e /code
# == here we have a layer from python:3.6-slim + your own requirements
# + the code install ==

CMD ["my-app-exec-2"]

两个docker镜像将与python和common-requirements.txt共享图层。当使用大量库构建应用程序时,这非常有用。

The two docker images are going to share the layers with python and the common-requirements.txt. It is extremely useful when building application with a lot heavy libraries.

要进行编译,我会这样做:

To compile, I will do:

docker build -t app-1 -f Dockerfile.1 .
docker build -t app-2 -f Dockerfile.2 .

因此,请考虑在 Dockerfile 确实重要。

So, think that the order how you write the steps in the Dockerfile does matter.

这篇关于为多个python应用程序重用Docker映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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