How do I publish a port at run time of a Docker image on Amazon Ec2?

Question:

Currently, my Docker image runs as expected when run with the following line inside VSCode's CLI.

docker run -it -d -p 5000:5000 flaskapp 

This allows me to open up http://localhost:5000/ and access it. However, if I were to run it on Docker desktop, it does not allow me to access the localhost. Under my Dockerfile, I have made sure to include EXPOSE 5000 inside.

docker build -t flaskapp:latest .

How do I run a Docker image inside Docker desktop or EC2 with the -p flag?

Asked By: Peek0

||

Answers:

We need to specify host as 0.0.0.0 in the app.run().

Eg: app.run(host="0.0.0.0")

Then add an inbound rule in the ec2 instance security group to expose the port.

Use the ec2 instance IP with the port number to access it.

Answered By: Mathan