带Gunicorn Docker化功能的Flask App,在以下位置进行监听:http://0.0.0.0:8000,但URL没有响应

问题描述

在Ubuntu Linux服务器上,我有一个Flask App(具有4条路由)作为Docker映像运行。

我的Dockerfile-

FROM ubuntu:18.04
FROM python:3
RUN apt-get update -y && apt-get install -y python-pip python-dev
copY . /backend
workdir /backend
RUN pip3 install -r requirements.txt
EXPOSE 8000
CMD gunicorn --bind 0.0.0.0:8000 --workers=4 wsgi:app

运行时-

sudo docker run -it flaskApp:1.16

stdout显示

[2020-10-01 14:03:25 +0000] [6] [INFO] Starting gunicorn 20.0.4
[2020-10-01 14:03:25 +0000] [6] [INFO] Listening at: http://0.0.0.0:8000 (6)
[2020-10-01 14:03:25 +0000] [6] [INFO] Using worker: sync
[2020-10-01 14:03:25 +0000] [8] [INFO] Booting worker with pid: 8
[2020-10-01 14:03:25 +0000] [9] [INFO] Booting worker with pid: 9
[2020-10-01 14:03:25 +0000] [10] [INFO] Booting worker with pid: 10
[2020-10-01 14:03:25 +0000] [11] [INFO] Booting worker with pid: 11

但是当我使用服务器的公共IP访问应用程序和API时,它无法连接。

  1. 我需要配置其他东西吗?
  2. 我的Dockerfile是否正确?

解决方法

Docker必须通过-p / --publish命令公开端口! docs

sudo docker run -it -p=8000:8000 flaskApp:1.16
,

作为documentation says,您需要从主机公开要访问的端口。在您的情况下,正确的命令将是:

sudo docker run -it -p 8000:8000 flaskApp:1.16