Docker Django 설치하기(Nginx, Gunicorn, Django)

정규호·2023년 7월 11일
0
post-thumbnail
post-custom-banner

Docker 설치

Docker-compose 설치

Django, Gunicorn 설치(docker-compose)

docker_proejct_dir 생성

/somepath/Designer_Docker

docker-compose.yml 생성

docker_project_dir/docker-compose.yml

version: '3.8'

services:
  designer_backend: # designer_backend
    image: designer_backend
    container_name: designer_backend
    build:
        context: .
        dockerfile: ./designer_backend/Dockerfile
    volumes:
      - ./designer_backend/backend_server:/backend_server
    expose:
      - "8000"
    # gunicorn 장고프로젝트명.wsgi:application --bind 0.0.0.0:8000
    command: gunicorn config.wsgi:application --bind 0.0.0.0:8000
    networks:
      - backend

networks:
  backend:
    driver: bridge

Django_Project_Dir 생성

/docker_project_dir/Designer_Docker/designer_backend

Dockerfile 생성

django_service_dir/Dockerfile

FROM python:3.9

# Configure python
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8 PYTHONUNBUFFERED=1

ARG REQUIREMENT='requirements.txt'

RUN apt-get -y update && apt-get -y install vim && apt-get clean
RUN mkdir /backend_server
ADD . /backend_server

WORKDIR /backend_server

# get packages
# docekr-compose up -d --build 시에는 아래 COPY 명령어 사용
COPY ./designer_backend/$REQUIREMENT $REQUIREMENT
RUN pip install --upgrade pip
RUN pip install --upgrade django-rest-framework
RUN pip install --no-cache-dir -r ./$REQUIREMENT



COPY . /backend_server

# EXPOSE 8080 <= docker-compose.yml 에서 설정

# CMD ["python", "src/main.py"]

requirements.txt

django_service_dir/requirements.txt

mysql-connector-python==8.0.31
redis==4.3.4
Django==4.2
gunicorn
djangorestframework
django-filter
djangorestframework-filters
drf-yasg
mysqlclient
requests
flex
ssv

Django Project 생성(명령어)

django_service_dir/backend_server/config

(django_project_dir/WORKDIR/projectName

docker-compose run designer_backend django-admin startproject config .

Django App 생성(명령어)

django_project_dir/backend_server/designer_server

mkdir ./backend_server/designer_server
docker-compose run designer_backend django-admin startapp designer_server ./designer_server

NginX 설치

Nginx_Dir 생성

docker_project_dir/nginx

/docker_project_dir/nginx

Dockerfile 생성

docker_project_dir/nginx/Dockerfile

FROM nginx:1.20.2

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

nginx.conf 생성

docker_project_dir/nginx/nginx.conf

#ngnix.conf

#Define the user that will own and run the Nginx server
user  nginx;

# Define the number of worker processes; recommended value is the number of
# cores that are being used by your server
worker_processes  1;

# Define the location on the file system of the error log, plus the minimum
# severity to log messages for
error_log  /var/log/nginx/error.log warn;

# Define the file that will store the process ID of the main NGINX process
pid        /var/run/nginx.pid;


# events block defines the parameters that affect connection processing.
events {
    # Define the maximum number of simultaneous connections that can be opened by a worker process
    worker_connections  1024;
}


# http block defines the parameters for how NGINX should handle HTTP web traffic
http {
    # Include the file defining the list of file types that are supported by NGINX
    include       /etc/nginx/mime.types;

    # Define the default file type that is returned to the user
    default_type  text/html;

    # Define the format of log messages.
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    # Define the location of the log of access attempts to NGINX
    access_log  /var/log/nginx/access.log  main;

    # Define the parameters to optimize the delivery of static content
    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay    on;

    # Define the timeout value for keep-alive connections with the client
    keepalive_timeout  65;

    # Define the usage of the gzip compression algorithm to reduce the amount of data to transmit
    #gzip  on;

    # Include additional parameters for virtual host(s)/server(s)
    include /etc/nginx/conf.d/*.conf;
}

project.conf 생성

docker_project_dir/nginx/project.conf

# nginx 가 80 port listen 하고 있다가, designer_backend:8000에 연결 
# designer_backend upstream 설정
upstream designer_backend {
    server designer_backend:8000;
}

server {

    listen 80;
    # server_name localhost;
    server_name designer_backend;

    location / {
        proxy_pass http://designer_backend;
        # proxy_pass http://localhost:8000;

        # Do not change this
        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 /static/;
    }
}

docker-compose.yml 에 nginx 추가

docker_Project_Dir/docker-compose.yml

version: '3.8'

services:
  nginx:
    container_name: nginx
    image: nginx
    build: ./nginx
    ports:
      # 외부 포트: 내부 포트
      # designer_backend port mapping
      - "80:80"
      # server port mapping sample
      # - "8080:8080"
    volumes:
      - ./nginx:/etc/nginx.conf.d
      - ./.static_root:/static
    depends_on:
      - designer_backend
    networks:
      - backend

  designer_backend: # designer_backend
    image: designer_backend
    container_name: designer_backend
    build:
        context: .
        dockerfile: ./designer_backend/Dockerfile
    volumes:
      - ./designer_backend/backend_server:/backend_server
    expose:
      - "8000"
    # python manage.py runserver 0:8000 역할을 gunicorn 에서 수행
    # gunicorn 장고프로젝트명.wsgi:application --bind 0.0.0.0:8000
    command: gunicorn config.wsgi:application --bind 0.0.0.0:8000
    networks:
      - backend

networks:
  backend:
    driver: bridge

Nginx + Gunicorn + Django 빌드 및 실행(명령어)

docker-compose build
docker-compose up

(이미지 build 하고 container 실행)
또는
(container 실행 시 이미지 build)

docker-compose up --build

localhost 실행 시 결과 화면

http://localhost/

localhost 실행 시 결과 화면

Postgresql 설치

Postgresql_Dir 생성

docker_project_dir/postgresql

/docker_project_dir/postgresql

docker-compose.yml 에 postgresql 추가

docker_project_dir/docker-compose.yml

version: '3.8'

services:
  nginx:
    container_name: nginx
    image: nginx
    build: ./nginx
    ports:
      # 외부 포트: 내부 포트
      # designer_backend port mapping
      - "80:80"
      # server port mapping sample
      # - "8080:8080"
    volumes:
      - ./nginx:/etc/nginx.conf.d
      - ./designer_backend/backend_server/.static_root/:/static
    depends_on:
      - designer_backend
    networks:
      - backend

  postgresql:
    image: postgres:latest
    container_name: postgres
    restart: always
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: "postgres"
      POSTGRES_PASSWORD: "postgres"
    volumes:
      - ./postgresql/data:/var/lib/postgresql/data
    networks:
      - backend


  designer_backend: # designer_backend
    image: designer_backend
    container_name: designer_backend
    build:
        context: .
        dockerfile: ./designer_backend/Dockerfile
    volumes:
      - ./designer_backend/backend_server:/backend_server
    depends_on:
      - redis
      - postgresql
      - mssql
      - mysql
    expose:
      - "8000"
    # python manage.py runserver 0:8000 역할을 gunicorn 에서 수행
    # gunicorn 장고프로젝트명.wsgi:application --bind 0.0.0.0:8000
    command: gunicorn config.wsgi:application --bind 0.0.0.0:8000
    networks:
      - backend

networks:
  backend:
    driver: bridge

Mysql 설치

mysql_Dir 생성

docker_project_dir/mysql

/docker_project_dir/mysql

conf.d_Dir 생성

docker_project_dir/mysql/conf.d

mysql 설정 관련
/docker_project_dir/mysql/conf.d

my.cnf 생성

docker_project_dir/mysql/conf.d/my.cnf

mysql 설정 관련
[client]
default-character-set = utf8mb4

[mysql]
default-character-set = utf8mb4

[mysqld]
character-set-client-handshake = FALSE
character-set-server           = utf8mb4
collation-server               = utf8mb4_unicode_ci

data_Dir 생성

docker_project_dir/mysql/data

데이터 저장 공간(volume)
/docker_project_dir/mysql/data

initdb.d_Dir 생성

docker_project_dir/mysql/initdb.d

데이터 init 처리 파일 등(create_table.sql, load_data.sql etc)
/docker_project_dir/mysql/initdb.d

docker-compose.yml 에 mysql 추가

docker_project_dir/docker-compose.yml

version: '3.8'

services:
  nginx:
    container_name: nginx
    image: nginx
    build: ./nginx
    ports:
      # 외부 포트: 내부 포트
      # designer_backend port mapping
      - "80:80"
      # server port mapping sample
      # - "8080:8080"
    volumes:
      - ./nginx:/etc/nginx.conf.d
      - ./designer_backend/backend_server/.static_root/:/static
    depends_on:
      - designer_backend
    networks:
      - backend

  postgresql:
    image: postgres:latest
    container_name: postgres
    restart: always
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: "postgres"
      POSTGRES_PASSWORD: "postgres"
    volumes:
      - ./postgresql/data:/var/lib/postgresql/data
    networks:
      - backend

  mysql: # See Also: https://hub.docker.com/_/mysql
    container_name: mysql
    image: mysql:8.0.31
    hostname: hostname-mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: designer_app
      # MYSQL_USER: root
      MYSQL_PASSWORD: root
      TZ: Asia/Seoul
    networks:
      - backend
    ports:
      - "3306:3306"
    volumes:
      - ./mysql/conf.d:/etc/mysql/conf.d
      - ./mysql/data:/var/lib/mysql
      - ./mysql/initdb.d:/docker-entrypoint-initdb.d
#    env_file: .env
    expose:
      - "3306"

  designer_backend: # designer_backend
    image: designer_backend
    container_name: designer_backend
    build:
        context: .
        dockerfile: ./designer_backend/Dockerfile
    volumes:
      - ./designer_backend/backend_server:/backend_server
    depends_on:
      - redis
      - postgresql
      - mssql
      - mysql
    expose:
      - "8000"
    # python manage.py runserver 0:8000 역할을 gunicorn 에서 수행
    # gunicorn 장고프로젝트명.wsgi:application --bind 0.0.0.0:8000
    command: gunicorn config.wsgi:application --bind 0.0.0.0:8000
    networks:
      - backend

networks:
  backend:
    driver: bridge
profile
The Man Who Lift
post-custom-banner

0개의 댓글