I’m working on a simple Flask app for which I’m using the default template:
JavaScript
x
25
25
1
from flask import render_template
2
import connexion
3
4
# Create the application instance
5
app = connexion.App(__name__, specification_dir="./")
6
7
# read the swagger.yml file to configure the endpoints
8
app.add_api("swagger.yml")
9
10
11
# Create a URL route in our application for "/"
12
@app.route("/")
13
def home():
14
"""
15
This function just responds to the browser URL
16
localhost:5000/
17
18
:return: the rendered template "home.html"
19
"""
20
return render_template("home.html")
21
22
23
if __name__ == "__main__":
24
app.run(debug=True)
25
I’m just calling a dummy function that return and ‘ok’ response:
JavaScript
1
3
1
def test(features):
2
return 'ok'
3
When I call it directly on my machine with the following code:
JavaScript
1
8
1
headers = {'content-type': 'application/json'}
2
#url = "http://localhost:5000/api/document"
3
4
url = "http://localhost:5000/api/scraping"
5
data = {'features':features}
6
7
response = requests.post(url,data=json.dumps(data), headers=headers )
8
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:
JavaScript
1
20
20
1
FROM ubuntu:18.04
2
3
4
5
RUN apt-get update -y &&
6
apt-get install -y python-pip python-dev
7
8
# We copy just the requirements.txt first to leverage Docker cache
9
COPY ./requirements.txt /app/requirements.txt
10
11
WORKDIR /app
12
13
RUN pip install -r requirements.txt
14
15
COPY . /app
16
17
ENTRYPOINT [ "python" ]
18
19
CMD [ "app.py" ]
20
And I’m using the same 5000 port:
JavaScript
1
2
1
sudo docker run -d -p 5000:5000 flask-tutorial
2
Advertisement
Answer
you need to expose
port 5000
in your Dockerfile
:
JavaScript
1
21
21
1
FROM ubuntu:18.04
2
3
4
5
RUN apt-get update -y &&
6
apt-get install -y python-pip python-dev
7
8
# We copy just the requirements.txt first to leverage Docker cache
9
COPY ./requirements.txt /app/requirements.txt
10
11
WORKDIR /app
12
13
RUN pip install -r requirements.txt
14
15
COPY . /app
16
EXPOSE 5000
17
18
ENTRYPOINT [ "python" ]
19
20
CMD [ "app.py" ]
21