frank99
frank99

Reputation: 21

Docker image access on aws ec2

I created a Docker image of a Flask application running the following code on a EC2 server:

docker build -t app .
docker run -80:80 app .

The result seems to work as the server returns:

Serving Flask app "app" (lazy loading)
Environment: production
Debug mode: off
Running on http://127.0.0.1:5000/

How can I access http://127.0.0.1:5000/ direction on the EC2 server, or change the direction in order to see it?

Also the Docker image is supposed to be running on port 80, but I don't see what role this port playing on the process.

I am following "Simple way to deploy machine learning models to cloud".

Upvotes: 0

Views: 481

Answers (3)

frank99
frank99

Reputation: 21

I figured out I had to add to my flask app the port and the host. Substitute this:

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

by this:

if __name__ == '__main__':
    app.run(host= '0.0.0.0',port=80)

Upvotes: 0

Martin Do Santos
Martin Do Santos

Reputation: 137

First of all the python server has to run on 0.0.0.0. Otherwise, the flask server will not accept any connections from outside.

And if you deploy it on an EC2 Instance, you'll probably need an elastic Load balancing to expose or a Public IP. With ELB you can show the flask app from 80 through port 5000.

And always remember to set -p 5000:5000. If not, you never expose that port.

Warning: if you use public IP, set your security groups, with the ports and IP address with CIDRs correctly. Otherwise, your machine will be hacked.

Upvotes: 0

Chris Williams
Chris Williams

Reputation: 35146

Update your docker run, or add another port mapping i.e.

docker run -p 5000:5000 app .

OR

docker run -p 80:80 -p 5000:5000 app .

Upvotes: 2

Related Questions