Search code examples
pythondockerflaskdockerfileuwsgi

Running uwsgi inside a Docker container - Empty reply from server


I have a simple Dockerfile for a Flask application. It runs correctly when I build it using as ENTRYPOINT the Flask development server, but when I use uwsgi, I have the error Empty reply from server. Note that when I run the same uwsgi command from the host, the server responds correctly.

The 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"]

As commented in the Dockerfile, I build the image with:

sudo docker build -f Dockerfile-test-2 --rm -t cgtn-python-test-img .

And I run the container with:

sudo docker run --rm -it --name cgtn-python-test -p 5000:5000 cgtn-python-test-img

When I use the commented ENTRYPOINT and CMD (python -m), it works. When I run the uwsgi command on the host, it works (the server responds to requests). This is the command I run on the host:

uwsgi --http 0.0.0.0:5000 --wsgi-file private_search_engine/server.py --callable app 

But when I build the image with the ENTRYPOINT as in the scriptlet above, I have an error when requesting:

% curl -XGET 'http://0.0.0.0:5000/api/v1/search?q="*"'
curl: (52) Empty reply from server

What am I doing wrong with uwsgi inside the container ?


Solution

  • So, I changed the CMD a little bit and now it works. I replaced --wsgi-file with --module. I also added a 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"]