简体   繁体   中英

Docker Add Directory to Container to Run Flask App

I have a folder structure that looks like this:

 +--mlservice
 |   __init__.py
 |   production
 |   requirements.txt
 |   venv
 |   DockerFile

where production is a directory and init.py is the main script.

This is my DockerFile.

FROM python:3.7

ENV FLASK_APP "mlservice/__init__.py"

RUN mkdir /app
WORKDIR /app

# Install the dependencies specified in requirements file
COPY requirements.txt /app/
RUN pip install -r requirements.txt

# Copy all directories and files from host to container
COPY . /app
COPY production /app/mlservice/production

EXPOSE 5000

# Run the application; default command to run when container starts
CMD flask run --host=0.0.0.0

My app runs fine in my Virtual Environment when running in Command Prompt, but when I try to run it in the Docker Container I get the following error:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
    __import__(module_name)
  File "/app/__init__.py", line 18, in <module>
    from production.dataprocessing import DataProcessing
ModuleNotFoundError: No module named 'production'

I have tried all different ways to COPY my directory into the container, but to no avail. I do not think that I have a problem with relative imports because the production folder is found successfully when running in Command Prompt. I think the problem lies in how I am copying the mlservice directory to the container. Can anyone see what I am doing wrong here?

I'd probably move your requirement.txt into the production/ directory then do something like:

FROM python:3.7

ENV FLASK_APP "/app/__init__.py"

COPY ./production /app/production
COPY ./__init__.py /app/__init__.py

WORKDIR /app

# Install the dependencies specified in requirements file
RUN pip install -r production/requirements.txt

EXPOSE 5000

# Run the application; default command to run when container starts
CMD flask run --host=0.0.0.0

This has the added advantage of:

  • Not copying the venv folder into the image
  • Not copying the Dockerfile into the image.

You could probably do away with the fourth line that copies __init__.py into the container by itself, by putting this in the production directory also, but this would depend on what the code looks like.

I got it working, but I am not certain which of my changes fixed the problem.

I kept the structure very similar but added a layer under "mlservice" called "app" and added an init.py to the "production" directory:

 +--mlservice
  |--app
    |--main.py
    |--production
       |--__init__.py
       |--dataprocessing.py
    |--requirements.txt
    |--venv
    |--DockerFile

I was able to simplify the Dockerfile:

FROM python:3.7

EXPOSE 5000

WORKDIR /app
ADD . /app

RUN pip install -r requirements.txt

ENTRYPOINT [ "python" ]
CMD [ "app/main.py" ]

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