0

I have a flask python 3.7 app that I want to run on docker and Kubernetes.

I have following Docker files

This one works (using python eggs)

FROM python:3-stretch
WORKDIR /app
ADD . /app/
RUN pip install -e . 
EXPOSE 5000
CMD ["python", "myapp/application.py"]

But this does not (but I want to stick to this)

FROM python:3-stretch
WORKDIR /app
ADD . /app/
RUN pip -r requirements.txt
EXPOSE 5000
CMD ["python", "myapp/application.py"]

Running this command (after building the container) docker run -it -p 5000:5000 myapp-python:latest gives following error

Traceback (most recent call last):
  File "myapp/application.py", line 3, in <module>
    import myapp.config as config
ModuleNotFoundError: No module named 'myapp'

But as mentioned above this works perfectly fine with the previous Docker file. Any help is much appreciated.

Additional notes:

  • No errors while building the Docker container

Here is my requirements.txt

flask
requests
flask-restful
flask-migrate
flask-sqlalchemy
flask-marshmallow
flask-jwt-extended
marshmallow-sqlalchemy
python-dotenv
passlib
tox
neomodel==3.2.9
marshmallow>=3.0.0b11
apispec
phonenumbers
pillow
emailage>=1.1.2
emailage-official==1.0.2
boto3
geopy
fuzzywuzzy
py2neo>=4.1.0
python-dotenv
eth_utils
neo4j>=1.7.0
neotime>=1.7.1
python-jose
eth-utils
eth-hash[pycryptodome]
python-Levenshtein
jsonpatch
google-cloud-vision
google-cloud-translate
pycountry
sspyjose>=0.2.5
pysinglesource>=0.1.0
flask-cors
pynamodb
5
  • 3
    The second one only installs the requirements, not the application itself.
    – jonrsharpe
    Commented Oct 29, 2019 at 7:54
  • Thanks heaps. Can you please elaborate a bit more? how can I fix it. I'm not very familiar with python eggs. Commented Oct 29, 2019 at 7:58
  • I'm thinking that the second one can't find your project in PYTHONPATH, since it seems to be running the application.py file as specified in CMD, but can't import the config.py file.
    – michjnich
    Commented Oct 29, 2019 at 8:02
  • if it's python3, shouldn't it be pip3 install?
    – suren
    Commented Oct 29, 2019 at 8:02
  • @suren. Nope. I just ran that docker image for python:3-stretch to check, and pip works just fine - it defaults to pip 19.2.3 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
    – michjnich
    Commented Oct 29, 2019 at 9:50

1 Answer 1

1

I found the solution.

  1. I built the two containers separately
  2. Then inside each container ran the following command.
root@64118c352a3e:/app# python
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', '/app', '/usr/local/lib/python37.zip', '/usr/local/lib/python3.7', '/usr/local/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/site-packages']

Then found out in my second config I was missing the '/app' path in the second container.

Then I added the path using

ENV PYTHONPATH "${PYTONPATH}:/app"

Full Dockerfile looks like following.

FROM python:3-stretch
WORKDIR /app
ADD . /app/
RUN pip install -r requirements.txt
ENV PYTHONPATH "${PYTONPATH}:/app"
EXPOSE 5000
CMD ["python", "myapp/application.py"]

Now it works.

Hope this will help someone

3
  • What is the content of the requirements.txt? It looks like you are not actually installing your application but only its requirements. Changing the PYTHONPATH should not be necessary. Proof of this is that it works with pip install -e .. Looks like you need a pip install . in there somewhere.
    – sinoroc
    Commented Oct 29, 2019 at 15:25
  • @sinoroc not sure if I understand this "It looks like you are not actually installing your application but only its requirements". Someone else also mentioned this but I don't know how to address this. I tried adding this but it doesn't work pip install .. Thanks for your time. :) Commented Oct 29, 2019 at 19:16
  • It is a bit difficult to pinpoint without knowing more about your exact setup, but something doesn't seem right. First there's rarely a good reason to modify PYTHONPATH. Does your project have a correctly functioning setup.py? Can you update the question with the content of your requirements.txt?
    – sinoroc
    Commented Oct 29, 2019 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.