为多个python应用程序重用Docker映像

编程入门 行业动态 更新时间:2024-10-12 03:17:02
本文介绍了为多个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映像中看到

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映像

本文发布于:2023-11-13 23:05:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585567.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   映像   应用程序   python   Docker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!