【问题标题】:How execute container in Google Cloud Compute Engine如何在 Google Cloud Compute Engine 中执行容器
【发布时间】:2019-08-20 12:41:26
【问题描述】:

我有一个 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 正在工作,但它不处理我的请求。

【问题讨论】:

  • docs.bitnami.com/google/faq/administration/use-firewall 。这既不是 Docker 问题,也不是 python ...
  • 我已经创建了适当的防火墙规则来打开该端口,并使用该规则标记了我的实例,但我仍然无法访问我的容器端口。即使在 VM 实例内部,我也无法访问容器的 5000 端口。如果我在本地构建和部署,它可以工作。

标签: flask containers google-compute-engine gunicorn


【解决方案1】:

检查以下内容:

  • 当您从通过 dockerfile 创建的映像运行 Docker 容器时,在主机和容器上公开 5000 端口,在此 documentation 上发布或公开端口(-p,--expose)

docker run -p 5000:5000

  • 获取与容器的终端会话,从那里检查它是否正在运行服务并且它正在侦听正确的端口 (5000)

docker exec -t -i container /bin/bash

  • 当您运行wget localhost:5000 时,连接会转到您的主机而不是您的容器,如果端口转发设置不正确,则连接永远不会到达容器。您可以检索分配给容器的本地 ip,以测试特定端口上的连接性(使用 wget)

    docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container

问候。

【讨论】:

  • 谢谢,但这并没有解决我的问题。我已经减少了与 Google Instance VM 内的容器有关的问题,因为我可以看到 gunicorn 正在运行的日志,但它不处理我的请求。
猜你喜欢
  • 1970-01-01
  • 2017-12-08
  • 1970-01-01
  • 2018-05-07
  • 1970-01-01
  • 1970-01-01
  • 2021-12-22
  • 2020-04-30
相关资源
最近更新 更多