Reputation: 7772
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?
Upvotes: 4
Views: 2513
Reputation: 18792
I came across this problem too, the issue of course being that I had specified a different port somewhere in the chain of tools.
Do double-check your ports!
app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return "example"
Dockerfile
FROM foo-image:tag
...
RUN pip3 install gunicorn Flask
COPY *.py /app/
WORKDIR /app
EXPOSE 8001
CMD ["gunicorn", "-b", "0.0.0.0:8001", "--workers", "2", "app:app"]
run command
https://docs.docker.com/engine/reference/run/#expose-incoming-ports
docker run -p 8001:8001 "$CONTAINER_TAG"
Upvotes: 1
Reputation: 676
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.
Upvotes: 1
Reputation: 155
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
Upvotes: 1