我在Google Cloud Compute实例内的容器中部署了Flask应用程序,使用gunicorn监听端口5000。
bootstrap.sh文件:
#!/bin/sh
exec gunicorn -b 0.0.0.0:5000 wsgi:app
我已经创建了适当的防火墙规则来打开该端口,并使用该规则标记了我的实例。
NAME NETWORK DIRECTION PRIORITY ALLOW DENY DISABLED
rule-allow-tcp-5000 default INGRESS 1000 tcp:5000 False
实例内部的命令netstat -a
显示端口监听:
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 6 0 0.0.0.0:5000 0.0.0.0:* LISTEN
但是,当我尝试通过REST客户端从外部访问实例时,它会无限期地等待响应。
即使在内部,从实例的SSH,我也没有任何响应:
nahuel@instance-1 ~ $ wget localhost:5000
--2019-08-20 12:27:05-- http://localhost:5000/
Resolving localhost... 127.0.0.1
Connecting to localhost|127.0.0.1|:5000... connected.
HTTP request sent, awaiting response...
我认为问题与容器有关,这是我的Dockerfile:
FROM python:3.6-buster
RUN useradd -m tagger
WORKDIR /home/tagger
COPY requirements.txt requirements.txt
RUN pip install --upgrade pip && pip install -r requirements.txt && pip install gunicorn
COPY autokeras autokeras
COPY static static
COPY tagger tagger
COPY templates templates
COPY model model
COPY application.cfg config.ini tagger-xxxxxx.json main.py wsgi.py bootstrap.sh ./
RUN mkdir uploads
RUN chmod +x bootstrap.sh
ENV FLASK_APP main.py
ENV GOOGLE_APPLICATION_CREDENTIALS tagger-xxxxxx.json
RUN chown -R tagger:tagger ./
USER tagger
EXPOSE 5000
ENTRYPOINT ["./bootstrap.sh"]
当我在本地测试容器时,使用-p选项运行容器,可以正常运行,但是我不知道如何使计算实例打开容器端口。
谢谢您的帮助。
我已经减少了有关Google Instance VM中容器的问题,正如我在容器日志中看到的那样,gunicorn正在运行,但是它不处理我的请求。
检查以下内容:
docker run -p 5000:5000
docker exec -t -i container /bin/bash
当您运行wget localhost:5000
,连接将转到主机而不是容器,如果未正确设置端口转发,则连接将永远不会到达容器。 您可以检索分配给容器的本地IP,以测试特定端口上的连接性(使用wget)
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container
问候。