I have implemented a small Flask application by using a Docker environment.
This is my project structure:
dockertest
├─ .vscode
│ └─ settings.json
├─ compose_flask
├─ Dockerfile
├─ app.py
├─ docker-compose.yml
└─ requirements.txt
This is the Dockerfile:
FROM python:3.8.0
ADD . /code
WORKDIR /code
RUN pip3 install -r requirements.txt
RUN pip3 install Werkzeug==0.16.0
CMD python app.py
This is the requirements.txt:
flask-restplus
flask
redis
This is the docker-compose.yml:
version: '2'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
depends_on:
- redis
redis:
image: redis
And finally my app.py:
from flask import Flask
from redis import Redis
from flask_restplus import fields, Api, Resource
app = Flask(__name__)
redis = Redis(host='redis', port=6379)
@app.route('/')
def hello():
redis.incr('hits')
print(redis.incr('hits'))
return 'This Compose/Flask demo has been viewed %s time(s).' % redis.get('hits')
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)
The build with the command: build -t compose-flask:latest .
works just fine!
When I try to start the container with docker-compose up
, the following Error Message appears:
redis_1 | 1:M 10 Mar 2020 23:10:05.011 * Ready to accept connections
web_1 | Traceback (most recent call last):
web_1 | File "app.py", line 4, in <module>
web_1 | from flask_restplus import fields, Api, Resource
web_1 | ImportError: No module named flask_restplus
compose_flask_web_1 exited with code 1
Seems like Python is not able to find the flask-RESTPlus package? The console output of the build tells me, that flask-RESTPlus has been successfully installed. The strange thing is that the same example without Docker works just fine without any import errors!!
I can't figure out what the problem is... I would be glad if someone could help!