首页 > 解决方案 > 在 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在容器内做错了什么?

标签: pythondockerflaskdockerfileuwsgi

解决方案


所以,我改变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"]

推荐阅读