Skip to content
Advertisement

Flask app on Docker: Max retries exceeded with URL

I’m working on a simple Flask app for which I’m using the default template:

from flask import render_template
import connexion

# Create the application instance
app = connexion.App(__name__, specification_dir="./")

# read the swagger.yml file to configure the endpoints
app.add_api("swagger.yml")


# Create a URL route in our application for "/"
@app.route("/")
def home():
    """
    This function just responds to the browser URL
    localhost:5000/

    :return:        the rendered template "home.html"
    """
    return render_template("home.html")


if __name__ == "__main__":
    app.run(debug=True)

I’m just calling a dummy function that return and ‘ok’ response:

def test(features):
    return 'ok'

When I call it directly on my machine with the following code:

headers = {'content-type': 'application/json'}
#url = "http://localhost:5000/api/document"

url = "http://localhost:5000/api/scraping"
data = {'features':features}

response = requests.post(url,data=json.dumps(data), headers=headers )

It works without any issue, but if I run it from the docker image I get the following error:

ConnectionError: HTTPConnectionPool(host=’localhost’, port=5000): Max retries exceeded with url: /api/scraping (Caused by NewConnectionError(‘<urllib3.connection.HTTPConnection object at 0x7f088060ef60>: Failed to establish a new connection: [Errno 111] Connection refused’))

This is the docker file I’m using:

FROM ubuntu:18.04 



RUN apt-get update -y && 
    apt-get install -y python-pip python-dev

# We copy just the requirements.txt first to leverage Docker cache
COPY ./requirements.txt /app/requirements.txt

WORKDIR /app

RUN pip install -r requirements.txt

COPY . /app

ENTRYPOINT [ "python" ]

CMD [ "app.py" ]

And I’m using the same 5000 port:

sudo docker run -d -p 5000:5000 flask-tutorial

Advertisement

Answer

you need to expose port 5000 in your Dockerfile:

FROM ubuntu:18.04 



RUN apt-get update -y && 
    apt-get install -y python-pip python-dev

# We copy just the requirements.txt first to leverage Docker cache
COPY ./requirements.txt /app/requirements.txt

WORKDIR /app

RUN pip install -r requirements.txt

COPY . /app
EXPOSE 5000

ENTRYPOINT [ "python" ]

CMD [ "app.py" ]
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement