I'm trying to deploy Django on Docker in AWS Lightsail, and the pages are stuck loading with no connection to server

Viewed 807

I have been trying to deploy a Django app on Lightsail with Gunicorn, NginX, and Docker. I've looked at multiple tutorials, all without success. I'm not familiar with most of the concepts, and I've pretty much been following blindly. So far, everything seems to work on the server itself, but I can't see the results on a webpage. I have configured it for "production" (not sure if I'm even doing it right), and I've added a record to my domain which redirects to this server. The webpage just buffers continuously, even when I try to set it to port 8000 (for development). I think I've gotten a few instances where I saw a "301 5" permanently moved log show up on the docker-compose logs, but that's about it.

Here are the Dockerfile, docker-compose.yml, and nginx conf.d file (which are probably the most important.

docker-compose.yml

version: '3.7'

services:
    web:
        build:
        environment: 
            - ENVIRONMENT=production
            - SECRET_KEY=NOT IMPORTANT
            - DEBUG=0
            - EMAIL_HOST_USER=EMAIL
            - EMAIL_HOST_PASSWORD=PASSWORD
        volumes:
            - .:/code
            - static_volume:/code/staticfiles
        depends_on: 
            - db
        networks:
            - nginx_network
            - db_network
    db:
        image: postgres:11
        env_file:
            - config/db/db_env
        networks:
            - db_network
        volumes:
            - db_volume:/var/lib/postgresql/data
    nginx:
        image: nginx:1.17.0
        ports:
            - 80:80
        volumes:
            - ./config/nginx/conf.d:/etc/nginx/conf.d
            - static_volume:/code/staticfiles
        depends_on:
            - web
        networks:
            - nginx_network
networks:
        nginx_network:
            driver: bridge
        db_network:
            driver: bridge
volumes:
        db_volume:
        static_volume:

Dockerfile:

   # Pull base image
FROM python:3.7

# Environment variables

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Work directory

WORKDIR /code

# Dependencies
COPY Pipfile Pipfile.lock /code/
RUN pip install pipenv && pipenv install --system

# Copy project
COPY . /code/

# expose port
EXPOSE 80

# gunicorn
CMD ["gunicorn", "--chdir", "my_project", "--bind", ":80", "mbdebate_project.wsgi:application"]

conf.d:

upstream hello_server {
    server web:80;
}

server {
    listen 80;
    server_name mydomain.com;

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

    location /static/ {
        alias /code/staticfiles/;
    }
}

The settings are pretty standard, and I don't think the problem is there. Any help would truly be appreciated :).

The tutorial I followed: tpawamoy.github.io/2018/02/01/docker-compose-django-postgres-nginx.html

1 Answers

When working on a flask application I had a similar issue connecting with nginx.

On nginx.conf, I used the same host configuration in my app.run.

Change web:80; in your conf.d to the host exposed by Django application.**

My case:-

app.py

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000)

nginx.conf

upstream flask-app {
    server 0.0.0.0:5000; 
}

server {

    listen 80 default_server;
    # https for production
    location / {
        proxy_pass http://flask-app;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }
}
Related