Google Cloud Run error: Invalid Command \"/bin/sh\": file not found when deploying from Docker image

Google Cloud Run error: Invalid Command \"/bin/sh\": file not found when deploying from Docker image

我正在尝试使用 FastAPI 和 Gunicorn 在 Google 云 运行 上部署一个相当简单的 Python 网络应用程序,在 this tutorial 之后有一个 Docker 容器部署后,我一直犯同样的错误:

Invalid ENTRYPOINT. [name: "gcr.io/<project_id>/<image_name>@sha256:xxx" error: "Invalid command \"/bin/sh\": file not found" ].

构建镜像并将其推送到 Container Registry 时工作正常。

在云端 运行 我已经为数据库连接设置了我的秘密,我将作为参数传递给 Docker 文件,settings.py 文件用于生产环境,正如我在本地对 build/run 容器所做的那样。

知道我在这个过程中遗漏了什么或做错了什么吗?这是我第一次尝试在云服务上部署 Web 应用程序,所以我可能还没有掌握所有的概念。

Docker文件

FROM ubuntu:latest

ENV PYTHONUNBUFFERED 1

RUN apt update && apt upgrade -y

RUN apt install -y -q build-essential python3-pip python3-dev
RUN pip3 install -U pip setuptools wheel
RUN pip3 install gunicorn uvloop httptools

COPY requirements.txt /code/requirements.txt
RUN pip3 install -r /code/requirements.txt

COPY . code/

# Pass the settings_module as an argument when building the image
ARG SETTINGS_MODULE
ENV SETTINGS_MODULE $SETTINGS_MODULE

EXPOSE $PORT

CMD exec /usr/local/bin/gunicorn -b :$PORT -w 4 -k uvicorn.workers.UvicornWorker app.main:app --chdir /code

cloudbuild.yaml

steps:
- name: gcr.io/cloud-builders/docker
  args: ["build", "--build-arg", "SETTINGS_MODULE=app.settings_production", "-t", "gcr.io/$PROJECT_ID/<image_name>", "."]
images:
- gcr.io/$PROJECT_ID/<image_name>
gcloud builds submit --config=cloudbuild.yaml

更新

我用 debian:buster-slim 替换了 ubuntu:latest (==20.04) 并且成功了。

以前

部署到云 运行,我也收到错误...我怀疑是 PORT,正在调查。不是 PORT。奇怪的是,图像 运行 在本地。尝试不同的 OS!

我在一个项目中复制了你的 Dockerfilecloudbuild.yaml,构建和 运行 对我来说成功了:

docker run \
--interactive --tty \
--env=PORT=8888 \
gcr.io/${PROJECT}/67486954
[2021-05-11 16:09:44 +0000] [1] [INFO] Starting gunicorn 20.1.0
[2021-05-11 16:09:44 +0000] [1] [INFO] Listening at: http://0.0.0.0:8888 (1)

NOTE To build from a Dockerfile, you need not create a cloudbuild.yaml and can just gcloud builds submit --tag gcr.io/PROJECT_ID/...

诊断问题的一个好方法是 运行 本地 docker build

docker build \
--build-arg=SETTINGS_MODULE=app.settings_production \
--tag=gcr.io/$PROJECT_ID/<image_name> \
.

然后尝试运行它:

docker run \
--interactive --tty --rm \
gcr.io/$PROJECT_ID/<image_name>

这将 Cloud Build 隔离为问题,并且可能会导致相同的错误。

错误表明容器在 ubuntu:latest 图像中找不到 shell (/bin/sh),这很奇怪。

我认为你可以|应该在 `CMD` 之后删除 `exec`

NOTE I read through Google's tutorial and see that the instructions include CMD exec ..., I'm unclear why that would be necessary but presumably it's not a problem.

你能在本地 运行 gunicorn 命令没有问题吗??

/usr/local/bin/gunicorn -b :$PORT -w 4 -k uvicorn.workers.UvicornWorker app.main:app --chdir /code

--chdir /code的位置也很好奇。怎么样:

WORKDIR code
COPY . .

...

CMD /usr/local/bin/gunicorn -b :$PORT -w 4 -k uvicorn.workers.UvicornWorker app.main:app

link 或许也可以将 --chdir 移到 app.main:app 之前,以便它应用于 gunicorn 而不是您的应用程序。

/usr/local/bin/gunicorn -b :$PORT -w 4 -k uvicorn.workers.UvicornWorker --chdir /code app.main:app