Report this

What is the reason for this report?

Dockerized django app not accessible via web

Posted on September 15, 2020

I am following a tutorial on how to dockerize django with postgres, gunicorn and nginx on an ubuntu 20.04 system. (see as reference: https://testdriven.io/blog/dockerizing-django-with-postgres-gunicorn-and-nginx/ )

Unfortunately while setting up the nginx part of the app I’ve seem to have stumbled accross a problem with setting up the reverse proxy correctly.

When I try to access the django app via <ip-address>:1337 I get ‘The requested resource was not found on this server.’ error.

While I know that this is due to django not being properly served to nginx, but I’m quite new at web deployment and currently at a loss on how to proceed.

The relevant settings:

docker-compose.prod.yml

services:
  web:
    build:
      context: ./app
      dockerfile: Dockerfile.prod
    command: gunicorn hello_django.wsgi:application --bind 0.0.0.0:8000
    expose:
      - 8000
    env_file:
      - ./.env.prod
    depends_on:
      - db
  db:
    image: postgres:12.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file:
      - ./.env.prod.db
  nginx:
    build: ./nginx
    ports:
      - 1337:80
    depends_on:
      - web

volumes:
  postgres_data:

nginx.conf

upstream hello_django {
    server web:8000;
}

server {

    listen 80;

    location / {
        proxy_pass http://hello_django;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

}

nginx Dockerfile

FROM nginx:1.19.0-alpine

RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d

settings.py

ALLOWED_HOSTS = ['<ip-address>', 'localhost']

Any help would be really appreciated.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there @mikewinkel,

What I could suggest is checking the error log of both your Nginx and Web containers.

To do that you can run the following command:

  1. docker ps -a

This would return a list of your containers, and then get the container ID and check the logs:

  1. docker logs container_id_here

This will give you more information on why the services are not returning any responses.

Feel free to share the errors here if you wish.

Regards, Bobby

Hi @mikewinkel !!

Did you, by any chance, find a solution to this particular problem?

I’m struggling with the same problem on a very similar docker django-gunicorn/nginx configuration and I can’t figure it out.

Thanks a lot!

Heya,

an update on an older topic in case someone finds it helpful

Your setup and configuration for dockerizing Django with Postgres, Gunicorn, and Nginx seem mostly correct, but the issue you’re facing with accessing the Django app via <ip-address>:1337 and getting a ‘The requested resource was not found on this server’ error is likely due to a misconfiguration in Nginx or the Django settings. Let’s go through a few steps to troubleshoot and hopefully resolve this issue.

1. Check Django ALLOWED_HOSTS

In your settings.py, you’ve added <ip-address> and localhost to ALLOWED_HOSTS. Make sure that the <ip-address> here is correctly replaced with the actual IP address of your server.

ALLOWED_HOSTS = ['your_server_ip', 'localhost']

2. Nginx Configuration

Your Nginx configuration seems to be set up correctly to proxy requests to your Django app running in the web container. However, double-check the following:

  • Ensure the nginx.conf is correctly placed in the Nginx container and that the path in the Dockerfile is correct.
  • Make sure there are no typos or errors in nginx.conf.
  • The upstream directive in Nginx points to web:8000. Ensure that the web service is correctly named in your docker-compose.prod.yml file.

3. Docker Networking

Since you’re using Docker Compose, all containers should be on the same network by default, but it’s worth checking if there are any networking issues:

  • Verify that the containers are running and can communicate with each other. You can exec into the Nginx container and use tools like curl to test if it can reach the Django app.

4. Django URL Configuration

Ensure that your Django project’s URL configuration (urls.py) is correctly set up to serve the pages you’re trying to access.

5. Check Docker Logs

Inspect the logs for your Docker containers to identify any errors:

docker-compose logs web
docker-compose logs nginx

6. Firewall Rules

Make sure your server’s firewall is not blocking port 1337. On Ubuntu, you can check this with UFW:

sudo ufw status

If the port is not allowed, you can open it with:

sudo ufw allow 1337/tcp

7. Restart and Rebuild Containers

After making configuration changes, rebuild and restart your Docker containers:

docker-compose down
docker-compose up --build

8. Browser Cache

Sometimes, browsers cache pages aggressively. Try accessing your app in incognito mode or clearing the browser cache.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.