Search code examples
pythondockerflaskgunicornwindows-subsystem-for-linux

Docker Flask wsl2 ERR_EMPTY_RESPONSE


I have the following Dockerfile in my flask application inside WSL2

FROM python:3.9

WORKDIR /usr/src/app/api

EXPOSE 5000

# install dependencies
RUN python -m pip install --upgrade pip
COPY ./requirements.txt /usr/src/app/api/requirements.txt
RUN pip install -r requirements.txt

# copy project
COPY . /usr/src/app/api/

# CMD [ "EXPORT","FLASK_APP","=","manage.py" ]
   
CMD [ "gunicorn", "-w", "4", "-b","localhost:5000","--reload","app:create_app('development')" ]

However after building the image and running a container from it:

docker build -t "backend"
docker run -p 5000:5000 backend

I get an "ERR_EMPTY_RESPONSE" when I open 'localhost:5000' on my local browser.

NOTE: when I run flask with gunicorn directly inside WSL2 without Docker, the site runs perfectly.

Thanks in advance


Solution

  • As Ervin Szilagyi said, you need to set 0.0.0.0 as the host IP.

    CMD [ "gunicorn", "-w", "4", "-b","0.0.0.0:5000","--reload","app:create_app('development')" ]
    
    

    For others who are getting this error in development env

    or

    Trying docker for the first time

    See a sample Dockerfile

    # syntax=docker/dockerfile:1
    FROM python:3.7-alpine
    ENV FLASK_APP=app.py
    ENV FLASK_RUN_HOST=0.0.0.0
    ENV FLASK_RUN_PORT=5000
    RUN apk add --no-cache gcc musl-dev linux-headers
    
    WORKDIR /app
    COPY . .
    RUN pip install -r requirements.txt
    EXPOSE 5000
    CMD [ "flask", "run" ]
    

    Helpful Links: