I have a flask application and I am trying to run it inside of a docker container using gunicorn.
This is my dockerfile
FROM python:3.6
WORKDIR /app
COPY ./requirements.txt /.requirements.txt
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt
EXPOSE 8000
ENV FLASK_APP=<some_name>
ENV FLASK_ENV=development
CMD gunicorn -b :8000 -w 4 app:app
This is how I am running the container -
docker run <name>
And this is how I am testing it-
curl -X POST http://172.17.0.2:8000/login -H 'cache-control: no-cache' -H 'content-type: application/json' -d '<SOME_PAYLOAD>'
curl: (7) Failed to connect to 172.17.0.2 port 8000: Operation timed out
I've looked through a couple of answers on this site
As far as I can tell, I am
Why is this operation timing out?
I have also tried
CMD gunicorn -b 0.0.0.0:8000 -w 4 app:app
which should map everythingdocker run -p 8000:8000 iterative
, which should force the mapping between the ports on the host and the container.But to no avail.
On my computer the app works fine.
Why is it not working in the docker container?
You are running your docker container incorrectly
docker run -p 8080:8080 <the-name-of-your-image>
assuming Your Dockerfile is getting the correct files this should solve the problem
goto localhost:8080 to verify
In my experience I have found the below method to be more effective when dealing with gunicorn and docker for flask. I would suggest you run the CMD in the Dockerfile as follows:
CMD ["gunicorn", "-b", "0.0.0.0:8000", "<scriptname>:<runtimefunction>"]
The script name in your case will most likely be "app.py" and the runtime function "app". Just to indicate what I am suggesting:
in app.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello, World!"
if __name__ == "__main__":
app.run(debug=True)
To add the workers as well:
CMD ["gunicorn", "-w", "5", "-b", "0.0.0.0:8000", "<scriptname>:<runtimefunction>"]
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With