简体   繁体   English

如何运行与Dockerfile分开存放在单独文件夹中的python文件?

[英]How to run python files kept in separate folders from a Dockerfile?

i have a dockerfile which looks like this: 我有一个看起来像这样的dockerfile:

FROM python:3.7-slim-stretch

ENV PIP pip

RUN \
    $PIP install --upgrade pip && \
    $PIP install scikit-learn && \
    $PIP install scikit-image && \
    $PIP install rasterio && \  
    $PIP install geopandas && \ 
    $PIP install matplotlib

COPY sentools sentools
COPY data data
COPY vegetation.py .

Now in my project i have two python files vegetation and forest. 现在在我的项目中,我有两个python文件植被和森林。 i have kept each of them in separate folders. 我把它们每个都放在单独的文件夹中。 How can i create separate docker images for both python files and execute the containers for them separately? 如何为两个python文件创建单独的docker映像并分别为它们执行容器?

If the base code is same, and only the container is supposed to run up with different Python Script, So then I will suggest using single Docker and you will not worry about the management of two docker image. 如果基本代码相同,并且仅容器应使用不同的Python脚本运行,那么我建议使用单个Docker,而您不必担心管理两个Docker映像。

Set vegetation.py to default, when container is up without passing ENV it will run vegetation.py and if the ENV FILE_TO_RUN override during run time, the specified file will be run. vegetation.py FILE_TO_RUN设置为默认值,当容器未通过ENV的情况下启动时,它将运行vegetation.py FILE_TO_RUN并且如果在运行时覆盖ENV FILE_TO_RUN ,则将运行指定的文件。

FROM python:3.7-alpine3.9 
ENV FILE_TO_RUN="/vegetation.py"
COPY vegetation.py /vegetation.py
CMD ["sh", "-c", "python $FILE_TO_RUN"]

Now, if you want to run forest.py then you can just pass the path file to ENV. 现在,如果要运行forest.py则只需将路径文件传递给ENV。

docker run -it -e FILE_TO_RUN="/forest.py" --rm my_image

or 要么

docker run -it -e FILE_TO_RUN="/anyfile_to_run.py" --rm my_image

updated: 更新:

You can manage with args+env in your docker image. 您可以在docker映像中使用args + env进行管理。

FROM python:3.7-alpine3.9 
ARG APP="default_script.py"
ENV APP=$APP
COPY $APP /$APP
CMD ["sh", "-c", "python /$APP"]

Now build with ARGs 现在使用ARG进行构建

docker build --build-arg APP="vegetation.py" -t app_vegetation .

or 要么

docker build --build-arg APP="forest.py" -t app_forest .

Now good to run 现在可以运行了

docker run --rm -it app_forest

copy both 复制两个

FROM python:3.7-alpine3.9 
# assign some default script name to args
ARG APP="default_script.py"
ENV APP=$APP
COPY vegetation.py /vegetation.py
COPY forest.py /forest.py
CMD ["sh", "-c", "python /$APP"]

If you insist in creating separate images, you can always use the ARG command. 如果您坚持要创建单独的图像,则始终可以使用ARG命令。

FROM python:3.7-slim-stretch
ARG file_to_copy
ENV PIP pip

RUN \
    $PIP install --upgrade pip && \
    $PIP install scikit-learn && \
    $PIP install scikit-image && \
    $PIP install rasterio && \  
    $PIP install geopandas && \ 
    $PIP install matplotlib

COPY sentools sentools
COPY data data
COPY $file_to_copy .

And the build the image like that: 然后像这样构建图像:

docker build --buid-arg file_to_copy=vegetation.py .

or like that 或那样

docker build --buid-arg file_to_copy=forest.py .

When you start a Docker container, you can specify what command to run at the end of the docker run command. 启动Docker容器时,可以在docker run命令的末尾指定要docker run命令。 So you can build a single image that contains both scripts and pick which one runs when you start the container. 因此,您可以构建包含两个脚本的单个映像,并选择启动容器时运行的脚本。

The scripts should be "normally" executable: they need to have the executable permission bit set, and they need to start with a line like 脚本应该是“通常”可执行的:它们需要设置可执行权限位,并且需要以类似

#!/usr/bin/env python3

and you should be able to locally (outside of Docker) run 并且您应该能够在本地 (Docker外部)运行

. some_virtual_environment/bin/activate
./vegetation.py

Once you've gotten through this, you can copy the content into a Docker image 完成此操作后,您可以将内容复制到Docker映像中

FROM python:3.7-slim-stretch
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY sentools sentools
COPY data data
COPY vegetation.py forest.py .
CMD ["./vegetation.py"]

Then you can build and run this image with either script. 然后,您可以使用任一脚本构建和运行该映像。

docker build -t trees .
docker run --rm trees ./vegetation.py
docker run --rm trees ./forest.py

If you actually want this to be two separate images, you can create two separate Dockerfiles that differ only in their final COPY and CMD lines, and use the docker build -f option to pick which one to use. 如果您实际上希望将其作为两个单独的映像,则可以创建两个单独的Dockerfile,它们的最终COPYCMD行仅不同,然后使用docker build -f选项选择要使用的一个。

$ tail -2 Dockerfile.vegetation
COPY vegetation.py ./
CMD ["./vegetation.py"]
$ docker build -t vegetation -f Dockerfile.vegetation .
$ docker run --rm vegetation

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

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