简体   繁体   中英

Python package I created not importing correctly in Docker container

I created a package out of a project and I want to run that package in a Docker container. The package is a Flask application and after installing the package locally I can run the project with FLASK_APP=packagename and flask run . When I do the same in the Docker container I get the error Error: Could not import "packagename.packagename". This is my Dockerfile:

FROM python:3.7
FROM pytorch/pytorch

MAINTAINER Nikolay Valkov nikolay1499@gmail.com

# set a directory for the app
WORKDIR /usr/app/

# copy all the files to the container
COPY . .

# install dependencies
RUN pip install --no-cache-dir -r requirements.txt

WORKDIR /usr/app/src/

RUN pip install -e .

WORKDIR /usr/app/src/packagename

# tell the port number the container should expose
EXPOSE 5000

ENV FLASK_APP packagename

# run the command
CMD flask run

Any ideas why this occurs only in the Docker container and not locally.

You can try do it like this, it should copy the package to your working directory: changes replace WORKDIR with ADD OR COPY CMD

FROM python:3.7
FROM pytorch/pytorch

MAINTAINER Nikolay Valkov nikolay1499@gmail.com

# set a directory for the app
WORKDIR /usr/app/

# copy all the files to the container
COPY . .

# install dependencies
RUN pip install --no-cache-dir -r requirements.txt

WORKDIR /usr/app/src/

RUN pip install -e .

ADD /usr/app/src/packagename packagename

# tell the port number the container should expose
EXPOSE 5000

ENV FLASK_APP packagename

# run the command
CMD flask run

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