无法在 Google Cloud Run 上部署 Ubuntu 20.04 Docker 容器



我正在尝试通过Google Cloud Run部署一个简单的基于Python的基于Ubuntu 20.04的Docker容器。我已成功生成映像,但是当我尝试部署 Cloud Run 服务时,出现以下错误(省略项目详细信息(:

Cloud Run error: Invalid argument error. Invalid ENTRYPOINT. [name: "gcr.io/{PROJECT_ID}/{SERVICE_NAME}@sha256:{HASH}"
error: "Invalid command "/bin/sh": fil
e not found"
e not found"
]....failed
Deployment failed

不过,奇怪的是,如果我在本地拉取并运行映像,它就可以正常工作。

docker run --rm --publish 5000:5000 -e PORT=5000 -it gcr.io/{PROJECT_ID}/{SERVICE_NAME}@sha256:{HASH}

我的 Dockerfile 几乎是基本的:

FROM ubuntu:20.04
COPY . /app
WORKDIR /app
RUN apt-get update 
&& DEBIAN_FRONTEND=noninteractive apt-get install -y python3 python3-pip 
&& pip3 install gunicorn Flask flask-cors
CMD exec gunicorn --bind :$PORT --worker-tmp-dir /dev/shm --timeout 900 wsgi:app

更奇怪的是,如果我用debian:buster-slim替换基本图像,它就可以正常工作。

有谁知道会发生什么?


附加信息:

status:
conditions:
- type: Ready
status: 'False'
message: |-
Cloud Run error: Invalid argument error. Invalid ENTRYPOINT. [name: "gcr.io/{PROJECT_ID}/{SERVICE_NAME}@sha256:{HASH}"
error: "Invalid command "/bin/sh": file not found"
].
lastTransitionTime: '2020-05-12T07:40:12.804Z'
- type: ConfigurationsReady
status: 'False'
message: |-
Cloud Run error: Invalid argument error. Invalid ENTRYPOINT. [name: "gcr.io/{PROJECT_ID}/{SERVICE_NAME}@sha256:{HASH}"
error: "Invalid command "/bin/sh": file not found"
].
lastTransitionTime: '2020-05-12T07:40:12.804Z'
- type: RoutesReady
status: 'True'
lastTransitionTime: '2020-05-12T06:19:12.224Z'

我遇到了同样的问题。这似乎是间歇性的:白天我无法部署到Cloud Run,但在深夜,各种解决方法大约一半的时间有效。

我发现最可靠的解决方法是不依赖于CMD或ENTRYPOINT中的/bin/sh或/bin/bash。它似乎在运行时存在/bin/sh,但在部署之前 Cloud Run 测试容器时,它有时不存在。

而不是在 Dockerfile 中

CMD exec gunicorn --bind :$PORT --workers 1 --threads 4 main:app

我改用了这个:

CMD ["/usr/bin/python3", "/app/run_gunicorn.py", "--workers", "1", "--threads", "4", "main:app"]

然后我添加了run_gunicorn.py脚本:

import os
import sys
from gunicorn.app.wsgiapp import run
port = os.environ['PORT']
sys.argv[-1:-1] = ['--bind', f':{port}']
print("sys.argv:", sys.argv)
run()

这是保持端口号动态的一种方法。

最新更新