Web &App/Django

[Django] Django + Gunicorn + Nginx + MariaDB

twoDeveloper 2022. 9. 22. 09:36

개요

Django와 Gunicorn은 하나의 Container에 Image를 만들고, Nginx와 MariaDB는 Docker Compose를 통해 서로의 의존관계를 성립하여 각각의 Container로 실행시키도록 하겠습니다.

 

물론 하나의 Container 안에 Django, Nginx, MariaDB를 함께 실행시킬 수 있지만, 각각 Container로 분리, 관리하는 것이 효율적입니다. 또한 Image를 각각 Container로 구동시켜 Expose 시켜 연결할 수 있지만 과정이 복잡합니다. 이를 손쉽게 연결하고 Container를 생성할 수 있게 해주기 위해 Docker Compose를 사용하였습니다.

 

* 다음 과정을 진행하기 위해 선행으로 Django + Gunicorn이 구축 및 이미지화 되어 있어야 합니다.

https://twodeveloper.tistory.com/105?category=583708 

 

[Django] Django Install

개요 Django를 설치하고 MariaDB와 연동 및 Gunicorn을 통해 bind 시켜 Web UI에 띄우는 것 까지 진행하도록 하겠습니다. 진행환경 OS : CentOS 7.6 Python : 3.6 Django : MariaDB : Django Install 1. Python In..

twodeveloper.tistory.com

https://twodeveloper.tistory.com/106

 

[docker] Django + Gunicorn image build

개요 Django + Gunicorn에 대한 docker image를 생성할 것입니다. 실행 전 Django 및 Gunicorn 설치 후 UI 띄우는 것 까지 진행하고 오십시오. https://twodeveloper.tistory.com/105 [Django] Django Install 개..

twodeveloper.tistory.com

Docker Image를 통해 Django를 실행시켰다면 다음 과정을 진행해도 됩니다.

 


 

WEB

1. Nginx 설정

Dockerfile에 위치한 디렉토리에 nginx 디렉토리를 생성합니다.

$ mkdir nginx
$ cd nginx

$ vi nginx.conf
---
upstream web {
    ip_hash;
    server web:8000;
}
server {
    location / {
        proxy_pass http://web/;
        proxy_redirect     off;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location /static {
        alias /staticfiles/;
    }
    listen 80;
    server_name localhost;
}

Nginx 설정 파일 입니다. 여기서 upstream 과 proxy_pass에서 web으로 되어있는 이유는 docker compose에서 작성한 서비스 name이 'web' 이기 때문입니다.

1.1 Nginx (docker-compose)

version: "2"
services:
  nginx:
    image: nginx:latest
    container_name: nginx
    ports:
      - "80:80/tcp"
    volumes:
      - ./nginx:/etc/nginx/conf.d
      - ./static:/staticfiles
    depends_on:
      - web

 

 

2. Django + Gunicorn Docker Image (docker-compose)

  web:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: web
    command: gunicorn djangoproject.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - ./static:/djangoproject/staticfiles
    expose:
      - "8000"
    depends_on:
      - db

앞서 생성하였던 Django + Gunicorn Dockerfile을 통해 Image를 Build 시키는 docker-compose 설정 파일입니다.


DB

1. MariaDB

  db:
    image: mariadb:latest
    container_name: mysql
    expose:
      - "3306"
    environment:
      MYSQL_DATABASE: djangoproject
      MYSQL_USER: djangoproject
      MYSQL_PASSWORD: djangoproject
      MYSQL_ROOT_PASSWORD: djangoproject
    volumes:
      - ../db/data:/var/lib/mysql
      - ../db/data/conf.d:/etc/mysql/conf.d

DB는 MariaDB를 사용하였고, 환경 변수 값들은 임의로 지정 해 주었습니다.

 

docker-compose 전체 코드

version: "2"
services:
  nginx:
    image: nginx:latest
    container_name: nginx
    ports:
      - "80:80/tcp"
    volumes:
      - ./nginx:/etc/nginx/conf.d
      - ./static:/staticfiles
    depends_on:
      - web
      
  web:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: web
    command: gunicorn djangoproject.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - ./static:/djangoproject/staticfiles
    expose:
      - "8000"
    depends_on:
      - db

  db:
    image: mariadb:latest
    container_name: mysql
    expose:
      - "3306"
    environment:
      MYSQL_DATABASE: djangoproject
      MYSQL_USER: djangoproject
      MYSQL_PASSWORD: djangoproject
      MYSQL_ROOT_PASSWORD: djangoproject
    volumes:
      - ../db/data:/var/lib/mysql
      - ../db/data/conf.d:/etc/mysql/conf.d

docker-compose 실행

$ docker-compose up --build

 

'Web &App > Django' 카테고리의 다른 글

[Django] Django Install  (0) 2022.09.21