Custom module import error only during deployment

  docker, flask, python

I have a very simple Flask app structured like this:

Dockerfile
requirements.txt
caller --> app.py
       --> user.py
       --> templates
       --> static

I am initializing and running my Flask app in app.py:

app.py

from twilio.rest import Client 
import sys, os
...
from user import User, Anonymous   
app = Flask(__name__)
...
def create_app():
    app.run(debug=True, host='0.0.0.0', port=5005)

When I run my app locally, it’s working fine but when I am trying to make an image and run the image, it’s giving me an error:

  File "/caller/caller/app.py", line 11, in <module>
    from user import User, Anonymous
ModuleNotFoundError: No module named 'user'

dockerfile

FROM python:3.7.5-slim-buster


ENV INSTALL_PATH /caller
RUN mkdir -p $INSTALL_PATH

WORKDIR $INSTALL_PATH

COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
RUN pip install flask_login

COPY . .

CMD gunicorn -b 0.0.0.0:8000 --access-logfile - "caller.app"

user.py

import uuid

class User():
  ...


class Anonymous():
  ...

do you know how can I fix this issue? It’s also my first Dockerfile and it’s probably the issue. To build and run the image I am doing docker build -t foo . && docker run -it foo

Source: Docker Questions

LEAVE A COMMENT