Isuru Palliyaguru
Isuru Palliyaguru

Reputation: 315

Docker: pip install -e . works but pip install -r requirements.txt does not work

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:

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

Upvotes: 0

Views: 749

Answers (1)

Isuru Palliyaguru
Isuru Palliyaguru

Reputation: 315

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

Upvotes: 1

Related Questions