简体   繁体   中英

Cannot connect to flask application with gunicorn in a docker container

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

  1. Exposing the port
  2. Hitting the right IP
  3. Hitting the right port on my host computer
  4. Mapping the port on my host computer to my docker container.

Why is this operation timing out?

I have also tried

  1. CMD gunicorn -b 0.0.0.0:8000 -w 4 app:app which should map everything
  2. docker 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.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM