简体   繁体   English

无法从 web 浏览器访问 EC2 上的 docker webapp

[英]Cannot access a docker webapp on EC2 from a web browser

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.我在 Amazon EC2 上的 docker 容器中运行 web 应用程序,但我的 web 浏览器没有显示任何内容,并且无法访问。 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:这就是我运行 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.这是我在这个 EC2 实例上的安全组设置。 在此处输入图像描述

The public IPv4 address of my instance is 13.38.1.129, so I typed https://13.38.1.129:5000 .我的实例的公共 IPv4 地址是 13.38.1.129,所以我输入了 https://13.38.1.129:5000 However the browser doesn't show anything and it just fails to connect.但是浏览器没有显示任何内容,只是无法连接。

在此处输入图像描述

EDIT: Here is the result of docker ps --all编辑:这是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这是ps -ef | grep 5000的结果ps -ef | grep 5000 , not sure if this helps. ps -ef | grep 5000 ,不确定这是否有帮助。

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

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.您在本地主机地址 (127.0.0.1) 中运行您的应用程序,使用 0.0.0.0 作为主机运行它,它应该可以工作。

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. localhost 和 0.0.0.0 之间的区别在于前者是一个环回地址,而后者是一个映射您实例中所有地址的元地址。

Working example: app.py工作示例: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.确保端口 5000 在实例的安全组中列入白名单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM