Home > Blockchain >  Cannot access a docker webapp on EC2 from a web browser
Cannot access a docker webapp on EC2 from a web browser

Time:01-15

I am running a webapp in a docker container on Amazon EC2, but my web browser doesn't show anything and it fails to access. Did I forget anything? I would appreciate it if anybody could point out the cause and provide me with the hint.

This is how I run a webapp:

ubuntu@ip-172-31-29-212:~$ sudo docker run  -p5000:5000 makotodocker/my-image:test-0.0.2
 * Serving Flask app 'application.py' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL C to quit)

And here is my Security group settings on this EC2 instance. enter image description here

The public IPv4 address of my instance is 13.38.1.129, so I typed enter image description here

EDIT: Here is the result of docker ps --all

CONTAINER ID   IMAGE                              COMMAND                  CREATED          STATUS                         PORTS                                       NAMES
695a6671ebf4   makotodocker/my-image:test-0.0.2   "flask run"              20 seconds ago   Up 19 seconds                  0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   angry_taussig

And here is the result of ps -ef | grep 5000, not sure if this helps.

ubuntu@ip-172-31-29-212:~$ ps -ef | grep 5000
root        3784     567  0 05:09 ?        00:00:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 5000 -container-ip 172.17.0.2 -container-port 5000
root        3789     567  0 05:09 ?        00:00:00 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 5000 -container-ip 172.17.0.2 -container-port 5000
ubuntu      3856    3667  0 05:09 pts/0    00:00:00 grep --color=auto 5000

CodePudding user response:

You're running your app in your localhost address (127.0.0.1), run it using 0.0.0.0 as host and it should work.

The difference between localhost and 0.0.0.0 is that the former is a loopback address, while the latter is a meta-address that maps all addresses from your instance.

Working example: app.py

from flask import Flask

app = Flask(__name__)

@app.route("/")
def main():
    return "Hello, World!"

Run with:

flask run --host 0.0.0.0

Make sure the port 5000 is whitelisted in the instance's security group.

  •  Tags:  
  • Related