Gunicorn Flask application in Docker Container not getting Exposed

9/8/2019

The application inside the container is inaccessible from the outside i.e if I exec into the docker container and do

curl localhost:5000 

it works correctly but not on the browser in my computer i get error : This site cant be reached

My Dockerfile:

# Use an official Python runtime as a parent image
FROM python:3.7-slim

# Set the working directory to /app
WORKDIR /web-engine

# Copy the current directory contents into the container at /app
COPY . /web-engine

# Install Gunicorn3
RUN apt-get update && apt-get install default-libmysqlclient-dev gcc -y

# Install any needed packages specified in requirements.txt
RUN pip3 install --trusted-host pypi.python.org -r requirements.txt

# Make port 5000 available to the world outside this container
EXPOSE 5000

# Define environment variable
ENV username root

# Run app.py when the container launches
CMD gunicorn --workers 4 --bind 127.0.0.1:5000 application:app --threads 1

UPON executing docker in this way:

sudo docker run -e password=$password -p 5000:5000 $reg/web-engine:ve0.0.2

I get the following output:

[2019-09-08 11:53:36 +0000] [6] [INFO] Starting gunicorn 19.9.0
[2019-09-08 11:53:36 +0000] [6] [INFO] Listening at: http://127.0.0.1:5000 (6)
[2019-09-08 11:53:36 +0000] [6] [INFO] Using worker: sync
[2019-09-08 11:53:36 +0000] [9] [INFO] Booting worker with pid: 9
[2019-09-08 11:53:36 +0000] [10] [INFO] Booting worker with pid: 10
[2019-09-08 11:53:36 +0000] [11] [INFO] Booting worker with pid: 11
[2019-09-08 11:53:36 +0000] [12] [INFO] Booting worker with pid: 12

So as you can see I'm mapping port 5000 of the container to port 5000 of my computer but localhost:5000 is not working

Therefore i tried everthing the same but with the development server of Flask with the following change in My dockerfile

FRom

CMD gunicorn --workers 4 --bind 127.0.0.1:5000 application:app --threads 1

TO

CMD python3.7 application.py

and IT WORKED; I goto localhost:5000 and see the application is working

There is nothing wrong with the application. I suppose there's an error in gunicorn server

the requirements.txt file :

Flask
Flask-SQLAlchemy
mysqlclient
gunicorn
bs4
html5lib

Please help me out

I also tried different forms of gunicorn and docker run command combinations like

CMD gunicorn -application:app && sudo docker run -e password=$password -p 5000:8000 $reg/web-engine:ve0.0.2

It didnt work terminal image of container working with development server

I would appreciate a solution involving nothing outside whats mentioned here like nginx, supervisor etc SOmeone please helllppp meeeeee.......

-- cosmicsage
docker
flask
gunicorn
kubernetes
python

2 Answers

9/9/2019

To expand on the other answer: the way Docker port forwarding works is to forward from the IPs on the host machine to one address in the container. Specifically, the external address that talks to the Docker bridge network that connects the container to the host network. It can't talk to to 127.0.0.1 on the container because that's a private IP—the host has its own 127.0.0.1.

So you need to either bind to that Docker bridge network, or just bind to all interfaces by binding to 0.0.0.0, which is the easiest solution.

Explaining this with prose is a little tricky, but I have longer version that also includes diagrams and will probably therefore be easier to understand: https://pythonspeed.com/articles/docker-connection-refused/

-- Itamar Turner-Trauring
Source: StackOverflow

9/8/2019

127.0.0.1 means a different thing inside the container than it does outside. Use 0.0.0.0:5000 instead.

-- Dave W. Smith
Source: StackOverflow