Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flask is working inside container but not when I port forward it

What I'm trying: I'm trying to run a simple flask app using docker. Using this site as a reference.

My dockerfile:

FROM ubuntu:latest
RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential
COPY ./app /app
WORKDIR /app
RUN pip install -r "requirements.txt"
ENTRYPOINT ["python"]
CMD ["app.py"]

Python file:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello World!'

if __name__ == '__main__':
    app.run(port=5000, debug=True)

requirements.txt:

Flask==1.0.2

What I'm doing:

  • building image using docker build -t simple-flask2 .
  • then, starting the container using docker run -p 5000:5000 simple-flask2
  • the when I go to localhost:5000 nothing appears.
  • So, I opened the container's terminal using docker exec -it 3be bash & then did curl localhost:5000. To my surprise it was working inside the container.

Can anyone please point out what am I missing? I'm pretty new to this. TIA :)

like image 731
noobie Avatar asked Oct 02 '18 08:10

noobie


2 Answers

I guess it is running only on the localhost (default value host='127.0.0.1') in the container. Try to use all interfaces (host='0.0.0.0'):

if __name__ == '__main__':
    app.run(port=5000, debug=True, host='0.0.0.0')
like image 66
Jan Garaj Avatar answered Oct 05 '22 01:10

Jan Garaj


Try listening on every interface with:

app.run(host='0.0.0.0', port=5000, debug=True)
like image 21
dmitrybelyakov Avatar answered Oct 05 '22 03:10

dmitrybelyakov