python - 在 Docker 容器中运行 uwsgi - 来自服务器的空回复
问题描述
我有一个用于 Flask 应用程序的简单 Dockerfile。当我使用 Flask 开发服务器作为 ENTRYPOINT 构建它时,它运行正确,但是当我使用时uwsgi
,我有错误Empty reply from server
。请注意,当我uwsgi
从主机运行相同的命令时,服务器会正确响应。
Dockerfile:
# sudo docker build -f Dockerfile-test-2 --rm -t cgtn-python-test-img .
# sudo docker run --rm -it --name cgtn-python-test -p 5000:5000 cgtn-python-test-img
FROM python:3-slim
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential gcc libreadline-gplv2-dev libncursesw5-dev openssl \
libssl-dev libsqlite3-dev tk-dev libgdbm-dev \
libc6-dev libbz2-dev libffi-dev python3-dev python3-pip \
libxml2-dev libxslt1-dev zlib1g zlib1g-dev python3-lxml \
&& rm -rf /var/lib/apt/lists/*
RUN mkdir /cgtn
WORKDIR /cgtn
# Copy the requirements file into the image
COPY requirements.txt .
# Install the requirements in the image
RUN pip install --no-cache-dir -r requirements.txt
# for production use socket
ENV UWSGI_PROTOCOL="http" \
PYTHONPATH=/cgtn \
FLASK_APP="private_search_engine/server.py"
# Copy the rest of the sources from host to container
COPY . .
RUN chown -R www-data:www-data /cgtn
USER www-data:www-data
EXPOSE 5000
#ENTRYPOINT ["python"]
#CMD ["-m", "flask", "run", "--host", "0.0.0.0", "--port", "5000"]
ENTRYPOINT ["uwsgi"]
CMD ["--http", "0.0.0.0:5000", "--wsgi-file", "private_search_engine/server.py", "--callable", "app"]
正如 Dockerfile 中所评论的,我使用以下方法构建图像:
sudo docker build -f Dockerfile-test-2 --rm -t cgtn-python-test-img .
我运行容器:
sudo docker run --rm -it --name cgtn-python-test -p 5000:5000 cgtn-python-test-img
当我使用注释的 ENTRYPOINT 和 CMD ( python -m
) 时,它可以工作。当我在主机上运行 uwsgi 命令时,它可以工作(服务器响应请求)。这是我在主机上运行的命令:
uwsgi --http 0.0.0.0:5000 --wsgi-file private_search_engine/server.py --callable app
但是当我在上面的 scriptlet 中使用 ENTRYPOINT 构建图像时,请求时出现错误:
% curl -XGET 'http://0.0.0.0:5000/api/v1/search?q="*"'
curl: (52) Empty reply from server
我uwsgi
在容器内做错了什么?
解决方案
所以,我改变CMD
了一点,现在它可以工作了。我替换--wsgi-file
为--module
. 我还添加了一个chdir
.
#...
ENTRYPOINT ["uwsgi"]
CMD ["--socket", "0.0.0.0:5000", \
"--chdir", "/cgtn", \
"--module", "private_search_engine.server:app", \
"--master", "--processes", "4", "--threads", "2", \
"--max-requests", "5000", \
"--harakiri", "20", \
"--vacuum", \
"--stats", "0.0.0.0:5001"]
推荐阅读
- javascript - 带有 include() 的 url 检查器
- python - 将 lambda 函数重写为常规函数
- amadeus - 使用 Amadeus Flight Offer API 区分去程航班和回程航班
- linux - 如何解决:错误:无法初始化主类com.amazonaws.services.dynamodbv2.local.main.ServerRunner
- ios - 删除 AVAssetWriter 的第一个黑色/空白帧
- python - 在 python 控制台中使用现有文件
- python - 如何在 tkinter 中嵌入 gnome-terminal?
- node.js - ForEach 循环等待生成 S3 url
- python - 在pygame中连续生成钢琴瓷砖中的瓷砖
- c# - 在 DataTable 上的 GroupBy 之后选择第一个不会为每个组返回一个值怎么可能?