конфигурация nginx, обратитесь к докеризованному приложению django

#django #amazon-web-services #docker #nginx #amazon-lightsail

Вопрос:

ЧТО Я ПЫТАЮСЬ СДЕЛАТЬ И ЧТО Я СДЕЛАЛ

я пытаюсь развернуть приложение django через контейнер docker в контейнерах amazon lightsail.

я следовал этому руководству, чтобы использовать контейнеры docker. Я пропустил часть Postgres и использую ее остальную часть. это сработало.

У меня есть мультиконтейнерная установка. nginx действует как обратный прокси-сервер, который получает все запросы и обслуживает статические файлы и носители. Другой контейнер-приложение django. Я использую docker-compose для создания и запуска изображений. Насколько я понял, я не могу использовать docker-compose с контейнерами lightsail. я должен определить два контейнера отдельно. Вот в чем проблема. docker-составьте определение моей веб-службы(приложение django) в ее конфигурации. конфигурация nginx возьмите веб-сервер в качестве прокси-сервера с директивой восходящего потока и обратитесь к нему с именем, определенным в docker-compose.

Я пытался определить web как 127.0.0.1, но это не работает.

МОЯ ПРОИЗВОДСТВЕННАЯ КОНФИГУРАЦИЯ

Это мои файлы, которые управляют контейнерами.

Докерфайлы

это файл Dockerfile в моем приложении:

 # 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 . .
 

и это докер-файл nginx, он в основном удаляет конфигурацию по умолчанию и помещает мою в контейнер:

 FROM nginx:1.21

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

nginx conf

это 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/;
    }

}
 

as you can see the config file use upstream and then define a server called web. I cant use that name since it is defined in the docker-compose file i putted below

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:
 

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 access 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.

The distribution doesn’t work. these are the logs of the two containers:

Nginx log

 [10/ott/2021:20:33:03] [deployment:4] Creating your deployment
[10/ott/2021:20:34:00] /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
[10/ott/2021:20:34:00] /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
[10/ott/2021:20:34:00] /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
[10/ott/2021:20:34:00] 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist
[10/ott/2021:20:34:00] /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
[10/ott/2021:20:34:00] /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
[10/ott/2021:20:34:00] /docker-entrypoint.sh: Configuration complete; ready for start up
[10/ott/2021:20:34:00] 2021/10/10 20:34:00 [emerg] 1#1: host not found in upstream "web:8000" in /etc/nginx/conf.d/nginx.conf:2
[10/ott/2021:20:34:00] nginx: [emerg] host not found in upstream "web:8000" in /etc/nginx/conf.d/nginx.conf:2
[10/ott/2021:20:34:54] [deployment:4] Started 1 new node
[10/ott/2021:20:35:56] /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
[10/ott/2021:20:35:56] /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
[10/ott/2021:20:35:56] /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
[10/ott/2021:20:35:56] 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist
[10/ott/2021:20:35:56] /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
[10/ott/2021:20:35:56] /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
[10/ott/2021:20:35:56] /docker-entrypoint.sh: Configuration complete; ready for start up
[10/ott/2021:20:35:56] 2021/10/10 20:35:56 [emerg] 1#1: host not found in upstream "web:8000" in /etc/nginx/conf.d/nginx.conf:2
[10/ott/2021:20:35:56] nginx: [emerg] host not found in upstream "web:8000" in /etc/nginx/conf.d/nginx.conf:2
[10/ott/2021:20:37:22] [deployment:4] Started 1 new node
[10/ott/2021:20:37:40] [deployment:4] Took too long
 

i think that the problem is that he cant find the app so it gives me the error host not fount in upstream

sito log

 [10/ott/2021:20:33:03] [deployment:4] Creating your deployment
[10/ott/2021:20:34:02] [2021-10-10 20:34:02  0000] [1] [INFO] Starting gunicorn 20.1.0
[10/ott/2021:20:34:02] [2021-10-10 20:34:02  0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
[10/ott/2021:20:34:02] [2021-10-10 20:34:02  0000] [1] [INFO] Using worker: sync
[10/ott/2021:20:34:02] [2021-10-10 20:34:02  0000] [7] [INFO] Booting worker with pid: 7
[10/ott/2021:20:34:54] [deployment:4] Started 1 new node
[10/ott/2021:20:35:58] [2021-10-10 20:35:58  0000] [1] [INFO] Starting gunicorn 20.1.0
[10/ott/2021:20:35:58] [2021-10-10 20:35:58  0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
[10/ott/2021:20:35:58] [2021-10-10 20:35:58  0000] [1] [INFO] Using worker: sync
[10/ott/2021:20:35:58] [2021-10-10 20:35:58  0000] [7] [INFO] Booting worker with pid: 7
[10/ott/2021:20:37:22] [deployment:4] Started 1 new node
[10/ott/2021:20:37:40] [deployment:4] Took too long
 

this is even worse since it only say simply Took too long i suppose that not even django is starting.

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 ).

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

thank you for your attention and help.