Deploy django app on aws lightsail containers

WHAT I'M TRYING TO DO AND WHAT I DID

i'm trying to deploy a django app through a container in amazon lightsail containers.

i've found several tutorial on how to do this but some of them seem to overcomplicate things to such a way that the files became unreadable. in particular everybody start using some complex database but i should be fine using the standard db.sqlite3 of django.

finally i stumbled upon this tutorial that seems clear enough. i thought i could simply skip the Postgres part and use the rest of it. it worked.

MY CONFIGURATION

These are my files that manage the containers. i have multicontainer setup. nginx act as reverse proxy that get all the request and serve static file and media. The other container is the django app. I use docker-compose to build and start the images.

Dockerfiles

this is the Dockerfile in my app:

# pull official base image
FROM python:3.9.6

# set work directory its an absolute path
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt

# copy project
COPY . .

and this is the Dockerfile of nginx:

FROM nginx:1.21

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

docker-compose

docker-compose is used to orchestrate the two in development:

version: '3.8'

services:
  web:
    build:
      context: ./sito
      dockerfile: Dockerfile
    command: gunicorn sito.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - static_volume:/home/app/web/static
      - media_volume:/home/app/web/media
    expose:
      - 8000
    env_file:
      - ./.env.dev
  
  nginx:
    build: ./nginx
    ports:
      - 1337:80
    volumes:
      - static_volume:/home/app/web/static
      - media_volume:/home/app/web/media
    depends_on:
      - web

volumes:
  static_volume:
  media_volume:

nginx conf

finally this is the nginx.conf:

upstream sito {
    server web:8000;
}

server {

    listen 80;

    location / {
        proxy_pass http://sito;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
        client_max_body_size 100M;
    }

    location /static/ {
        autoindex on;
        alias /home/app/web/static/;
    }

    location /media/ {
        autoindex on;
        alias /home/app/web/media/;
    }

}

MY PROBLEM

Now for the deployment to lightsail container. I cant use docker-compose to spin up everything so I have to recreate the settings specified there when creating the container on aws.

I want to use the containers that require a "distribution" that is an image+container. My images have been uploaded on dockerHub.

I create 2 container,

the first one is nginx ( with the name "nginx"), the image is taken from dockerHub after I uploaded my version of it ( so its not the original nginx). in my docker-compose i specify the ports, the internal one is 80 and the external one is 1337. Though the menu I can specify the external port, I'm not sure if i have to specify ( or how) the other port (80).


the second container is the app ( with name "sito"), the image is also taken from dockerhub where i uploaded it. my docker-compose specify a command that start gunicorn making it listen to the port 8000, that i can put while creating the container, and expose the port 8000.

I also declare 3 variables:

  • DJANGO_ALLOWED_HOSTS = localhost 127.0.0.1 [::1]
  • SECRET_KEY = mySecretKeyFromDjangoProject
  • DEBUG = 1

settings.py should look at these variables and use them in django.

details of the distribution

The distribution doesn't work. The logs don't give me any clue on what to do. these are the logs of the two containers:

Nginx log

enter image description here

the puzzling part for me in this log is the host not found in upstream.

sito log

enter image description here

this is even worse since it only say hat took too long.

I'm very new to deployment especially with containers so I may have done a lot of mistakes and sadly I can't find any proper explanation on how this work since amazon docs are terrible ( in my opinion ) and usually people try to deploy using an instance.

N.B. i know that the volumes specified in the docker-compose files are not created by the two containers but i suppose that is not the problem since it may give problems serving static files but it should be able to function anyway you may wonder what is my question, but i don't have since i'm completely lost on what to do. I suppose they could be :

  1. Did i misunderstood something?
  2. Can you point me to some good tutorial about amazon lightsail containers?
  3. what do you think about aws containers?

PS: please don't bury me in downvotes

thank you for your attention and help.

Back to Top