Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 230
Imran Said Avatar asked Dec 10 '25 07:12

Imran Said


1 Answers

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:

  • https://www.youtube.com/watch?v=4uoWRXuYfJs&ab_channel=Hackn%27Stack
like image 53
illusionist Avatar answered Dec 11 '25 20:12

illusionist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!