简体   繁体   中英

How to create a multistage dockerfile for a python app?

Below is the directory structure and the dockerfile for my python application. 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. How can I create a multistage dockerfile in order to first create the data and then run the main.py file? I'm new to using docker and I feel overwhelmed.

在此处输入图像描述

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

start.sh:

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

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. and replacing CMD with:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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