简体   繁体   English

如何为 python 应用程序创建多级 dockerfile?

[英]How to create a multistage dockerfile for a python app?

Below is the directory structure and the dockerfile for my python application.下面是我的 python 应用程序的目录结构和 dockerfile。 In order to run the main.py , I need to create a data set by running generate_data.py , which is in the data directory.为了运行main.py ,我需要通过运行generate_data.py创建一个数据集,它位于数据目录中。 How can I create a multistage dockerfile in order to first create the data and then run the main.py file?如何创建多级 dockerfile 以便首先创建数据然后运行main.py文件? I'm new to using docker and I feel overwhelmed.我是使用 docker 的新手,我感到不知所措。

在此处输入图像描述

FROM python:3.7.2-slim
WORKDIR /usr/src/app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . /usr/src/app
CMD ["python", "./src/main.py"]

You can create a shell script then use that for CMD您可以创建一个 shell 脚本,然后将其用于 CMD

start.sh:开始.sh:

#!/bin/bash
python generate_data.py
python ./src/main.py

Dockerfile: Dockerfile:

FROM python:3.7.2-slim
WORKDIR /usr/src/app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . /usr/src/app
CMD ["sh", "start.sh"]

A key point of using docker might be to isolate your programs, so at first glance, you might want to move them to separate containers and talk to each other using a shared volume or a docker network, but if you really need them to run in the same container, you can achieve this by using a bash script.使用 docker 的一个关键点可能是隔离您的程序,因此乍一看,您可能希望将它们移动到单独的容器中并使用共享卷或 docker 网络相互通信,但如果您真的需要它们在同一个容器,您可以使用 bash 脚本来实现。 and replacing CMD with:并将 CMD 替换为:

COPY run.sh
RUN chmod a+x run.sh
CMD ["./run.sh"]

You can also include if statements into a bash script and pass arguments to the bash script through docker.您还可以将 if 语句包含到 bash 脚本中,并通过 arguments 将 arguments 通过 Z05B6053CCE691A694A999CCE69Z 脚本传递给 bash 脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM