0

I make a docker image for datetime flask api in python. I cannot see the output on the specified port when I visit browser on http://127.0.0.1:5000/

flask api python code app.py

from flask import Flask,jsonify
from datetime import datetime
app = Flask(__name__)

@app.route("/")
def hello():
    now = datetime.now()
    return jsonify( {"Date and Time: ": now})

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

Dockerfile

FROM python:3.7.4
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["app.py"]

requirements.txt

flask==1.1.2

The three files [ 'app.py', 'Dockerfile', 'requirements.txt'] are in 'my_docker_flask' folder. I run the following commands inside the folder

I build and run the docker

docker build -t my_docker_flask:latest .
docker run  -p 5000:5000 my_docker_flask:latest

I cannot see the output on the specified port when I visit browser on http://127.0.0.1:5000/ What am I doing wrong?

  • 1
    Specify `app.run(host='0.0.0.0')`. The default for Flask is to bind to the localhost-only interface, but inside Docker that makes the server inaccessible from outside the container. – David Maze Dec 13 '20 at 02:00
  • @DavidMaze I changed the line with your suggestion. It still does not work. – Ayyaz Azeem Dec 13 '20 at 02:13
  • What _do_ you see? (Both the output of the container logs, and the error message from the browser.) – David Maze Dec 13 '20 at 03:22

0 Answers0