Docker port forwarding not working Django app

I have a Django app that works fine locally when I run it without a container. However, when I containerize the app, create an image, and run the container, the app is not accessible at the forwarded port:

http://localhost:8081

When I exec into the container and run the curl command:

curl localhost:8005

I am able to see the response inside the container.

Here is my Dockerfile, which I am using to build the image and run the container with some environment variables, which are present when I exec into the container:

FROM python:3.11
ENV PYTHONUNBUFFERED 1
RUN mkdir /app
WORKDIR /app
COPY requirements.txt /app
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY . /app
EXPOSE 8005
#ENTRYPOINT ["bash", "entrypoint.bash"]
CMD ["python", "manage.py", "runserver", "0.0.0.0:8005"]

To build the image and run the container, I use the following commands:

docker build -t app_backend:v1 .
docker run -dit --rm -p 8081:8005 --name app_backend app_backend:v1

For testing, I tried running a sample command and was able to access Nginx on port 8009, but I cannot access the Django app at the specified port:

docker run -d -p 8009:80 nginx:alpine
Back to Top