Gunicorn 在 Docker 中使用 CMD 命令失败

问题描述 投票:0回答:1

我有一个在 Gunicorn 上运行的 Flask 应用程序,我想对我的应用程序进行 docker 化。为了实现这一目标,我设置了一个 Dockerfile,我想在我的 Docker 镜像中启动 Gunicorn 服务器。应使用以下 CMD 命令启动 Gunicorn 服务器。

WORKDIR /home/glue_user/workspace/flask_app/
CMD ["gunicorn", "-b", "0.0.0.0:443", "app_flask:application","--workers=5","--timeout=600", "--worker-tmp-dir=/dev/shm" ,"--certfile=gunicorn_local.crt", "--keyfile=gunicorn_local.key"] 

不幸的是,它失败并出现以下错误。

/home/glue_user/.local/bin/gunicorn: line 3: import: command not found
/home/glue_user/.local/bin/gunicorn: line 4: import: command not found
/home/glue_user/.local/bin/gunicorn: line 5: from: command not found
/home/glue_user/.local/bin/gunicorn: gunicorn: line 7: syntax error near unexpected token `('

该错误源自 Gunicorn 启动脚本,但我不确定是什么原因导致的。当我在 bash 端点上启动 Docker 容器并手动启动 Gunicorn 服务器时,一切正常。这是我用于手动启动的命令。

docker run --rm -it --entrypoint bash -v ~/.aws:/home/glue_user/.aws -e AWS_PROFILE=$AWS_PROFILE  -it my_docker_image

该图像基于 amazon/aws-glue-libs:glue_libs_4.0.0_image_01。 这是我的 Dockerfile

FROM amazon/aws-glue-libs:glue_libs_4.0.0_image_01

# set current env
ENV FLASK_DEBUG 0
ENV PYTHONPATH /home/glue_user/workspace/flask_app

RUN mkdir /home/glue_user/workspace/flask_app
WORKDIR /home/glue_user/workspace/flask_app/
COPY requirements.txt /home/glue_user/workspace/flask_app/

## Install pip requirements
RUN pip3 install --no-cache-dir -r /home/glue_user/workspace/flask_app/requirements.txt

COPY . /home/glue_user/workspace/flask_app/


# Docker container listen to port 
EXPOSE 443

# start gunicorn wsgi server, module:flask_instance
CMD ["gunicorn", "-b", "0.0.0.0:443", "app_flask:application","--workers=5","--timeout=600", "--worker-tmp-dir=/dev/shm" ,"--certfile=gunicorn_local.crt", "--keyfile=gunicorn_local.key"]
python docker flask gunicorn aws-glue
1个回答
0
投票

该基础镜像的 Docker Hub 镜像页面在细节上有点稀疏,但它确实包含 自动生成的 Dockerfile 摘录。最后一行是

ENTRYPOINT ["bash", "-l"]

这条线有问题。因为 CMD 作为参数传递给 ENTRYPOINT,所以您会得到

bash -l gunicorn ...
的有效命令,并且 Bourne shell 解释器会尝试运行 Python 脚本。这会导致您看到的问题。看起来该图像还试图将所需的设置脚本放入
/home/glue_user/.bashrc
中,这使用起来很棘手。

如果您可以使用普通的 Docker Hub

python
镜像作为基础,它可能会工作得更好。

您还可以禁用基础图像的

ENTRYPOINT

ENTRYPOINT []
CMD ["gunicorn", ...]

如果您确实需要

.bashrc
文件,您可以通过 shell 运行命令

SHELL ["/bin/bash", "-l", "-c"]
ENTRYPOINT []
CMD gunicorn -b 0.0.0.0:4443 ...
# Note, plain string as `CMD`, not JSON-array syntax

或者编写一个入口点包装器脚本来读取初始化文件,然后运行主容器进程。

#!/bin/bash
. $HOME/.bashrc
exec "$@"
ENTRYPOINT ["./entrypoint.sh"]
CMD ["gunicorn", "-b", "0.0.0.0:443", ...]
© www.soinside.com 2019 - 2024. All rights reserved.