1

I created the following Flask app:

app.py

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def inicio():
    return render_template('index.html')

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

index.html

<html>
    <head>
    </head>
    <body>
        <p>This is a beautiful world indeed</p>
    </body>
</html>

Dockerfile

FROM python:3.9-alpine

COPY . app

COPY ./requirements.txt /app/requirements.txt

WORKDIR app

EXPOSE 5000:5000

RUN pip install -r requirements.txt

CMD [ "python", "app.py" ]

Then I created the image and run it:

docker build -t myimage .
docker run -t -i myimage 

But when I receive the link, I click on it Running on http://127.0.0.1:5000 and it takes me to a browser. However, nothing displays. Is there anything I am doing wrong?

Alexis
  • 2,104
  • 2
  • 19
  • 40

2 Answers2

2

There are two things you need to fix here.

  1. You should be binding to 0.0.0.0 if you want the container to be accessible from the outside
  2. You should bind the port when running docker

So modify the Python file to have the following:

app.run(debug=True, host='0.0.0.0', port=5000)

and then:

$ docker build -t myimage .
$ docker run -t -i -p 5000:5000 myimage 
Paolo
  • 21,270
  • 6
  • 38
  • 69
1

The EXPOSE keyword doesn't actually make it so the port is published at runtime
Try:

docker run -t -i myimage -p 5000:5000
Paolo Sini
  • 176
  • 10