docker with [ Nginx + uWSGI + django ]


๐Ÿ’ก project์˜ dockerfile

dockerfile

#1
FROM python:3.9.7

#2
ENV PYTHONUNBUFFERED 1 

#3
RUN apt-get -y update 
RUN apt-get -y install vim 

#4
RUN mkdir /srv/docker-server 
ADD . /srv/docker-server

#5
WORKDIR /srv/docker-server 

#6
RUN pip install --upgrade pip
RUN pip install -r requirements.txt 

โ’ˆ python 3.9.7 ๋ฒ„์ „ ๋‹ค์šด

โ’‰ PYTHONBUFFERDED =1

์–ด๋–ค ์„ค์ •์ธ์ง€ ๋ชฐ๋ผ stackoverflow๋ฅผ ๊ฒ€์ƒ‰ํ–ˆ๋‹ค. log ๋ฉ”์„ธ์ง€๋ฅผ ๋ฐ”๋กœ ๋ณด์—ฌ์ค€๋‹ค๊ณ  ํ•œ๋‹ค.

โ’Š update, vim ์„ค์น˜

โ’‹ /srv/docker-server ๊ฒฝ๋กœ ์ƒ์„ฑ ํ›„ ํ˜„์žฌ ๊ฒฝ๋กœ(.)์˜ ๋ชจ๋“  ํŒŒ์ผ์„

๋„์ปค ๋‚ด๋ถ€ ๊ฒฝ๋กœ(/srv/docker-server)๋กœ ์ถ”๊ฐ€.

โ’Œ /srv/docker-server๋กœ ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€ ๊ฒฝ๋กœ ๋ณ€๊ฒฝ

โ’ ๋ช…๋ น์–ด ์‹คํ–‰.



uwsgi.ini

๋ช‡ ๊ฐ€์ง€ ์„ค์ •์„ ๋ฐ”๊ฟ”์ค˜์•ผ ํ–ˆ๋‹ค. chdir ๊ฒฝ๋กœ ์„ค์ •์œผ๋กœ ์‹œ๊ฐ„์„ ๋งŽ์ด ํ—ˆ๋น„ํ•จ.

์ฒ˜์Œ์—๋Š” chdir = /srv/docker-server/drf_todo ๋ผ๊ณ  ์„ค์ •ํ–ˆ๋Š”๋ฐ,

drf_todo๋ผ๋Š” ๋ชจ๋“ˆ์„ ๋ชป์ฐพ์Œ์ด๋ผ๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์—ˆ๋‹ค.

[uwsgi]

socket = /srv/docker-server/drf_todo.sock
chdir = /srv/docker-server
module = drf_todo.wsgi
env=DJANGO_SETTINGS_MODULE=drf_todo.settings
master = True
enable-threads = True
max-requests=5000
buffer-size=32768
chmod-socket = 666
logger = file:/tmp/uwsgi.log
vacuum = true


๐Ÿ’ก nginx

nginx/dockerfile

#1 
FROM nginx:latest

#2
COPY nginx.conf /etc/nginx/nginx.conf
COPY drf_todo.conf /etc/nginx/sites-available/

#3
RUN mkdir -p /etc/nginx/sites-enabled/\
    && ln -s /etc/nginx/sites-available/drf_todo.conf /etc/nginx/sites-enabled/\
    && mkdir -p /var/log/drf_todo


#4 
CMD ["nginx", "-g", "daemon off;"]

โ’ˆ nginx ์„ค์น˜

โ’‰ ํ˜„์žฌ ๊ฒฝ๋กœ์— ์žˆ๋Š” nginx.conf ํŒŒ์ผ์€ /etc/nginx/nginx.conf ํŒŒ์ผ์— ๋ฎ์–ด์”Œ์šฐ๊ณ ,

drf_todo.confํŒŒ์ผ์€ /etc/nginx/sites-available/ ๊ฒฝ๋กœ์— ๋ณต์‚ฌํ•˜๊ธฐ

โ’Š /etc/nginx/sites-enabled/ ๊ฒฝ๋กœ ์ƒ์„ฑํ•˜๊ณ , ์‹ฌ๋ณผ๋งํฌ ์„ค์ •.

โ’‹ ์ปค๋งจ๋“œ ์‹คํ–‰.


nginx/nginx.conf


user  root;
worker_processes  1;
pid /run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    *** ์„œ๋ฒ„ ์ถ”๊ฐ€ ***
    include       /etc/nginx/sites-enabled/*;
}


nginx/drf_todo.conf

# upstream(proxy) ์„ค์ •
upstream django {
    server unix:///srv/docker-server/drf_todo.sock;
}

# configuration of the server
server {
    # client๊ฐ€ ๋“ค์–ด์˜ค๋Š” port
    listen      80;
    
    # client๊ฐ€ ์‹คํ–‰ํ•˜๋Š” ์„œ๋ฒ„์˜ IP์ฃผ์†Œ or Domain
    server_name localhost;

    charset     utf-8;

    # log settings.
    access_log  /var/log/drf_todo/access.log;
    error_log   /var/log/drf_todo/error.log;

    # max upload size
    client_max_body_size 75M;

    # Django media ๋””๋ ‰ํ† ๋ฆฌ ๊ฒฝ๋กœ (alias๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰์— / ๋„ฃ๊ธฐ.)
    location media/  {
        alias /svr/docker-server/drf_todo/media/;
    }

    # Django static ๋””๋ ‰ํ† ๋ฆฌ ๊ฒฝ๋กœ
    location static/ {
        alias /svr/docker-server/drf_todo/static/;
    }

    # media์™€ static์„ ์ œ์™ธํ•œ ๋ชจ๋“  ์š”์ฒญ์„ upstream์œผ๋กœ ๋ณด๋ƒ…๋‹ˆ๋‹ค.
    location / {
        # uwsgi_pass [upstream name] (์œ„์— upstream์œผ๋กœ ์„ค์ •ํ•œ block์˜ ์ด๋ฆ„)
        uwsgi_pass  django;

        # uwsgi_params์˜ ๊ฒฝ๋กœ
        include uwsgi_params;
    }
}


๐Ÿ’ก docker-compose

version: "3"

services: 
  db:
    platform: linux/x86_64
    container_name: main.mysql
    image: mysql
    restart: always
    command: mysqld --character-set-server=utf8 --collation-server=utf8_general_ci --default-authentication-plugin=mysql_native_password
    environment: 
      MYSQL_ROOT_PASSWORD: "wjdrlfwns1!"
      MYSQL_DATABASE: "drf_todo"
      MYSQL_USER: "gilbert"
      MYSQL_PASSWORD: "wjdrlfwns1!"
    ports:
        - "7001:3306"
  web:
    build: ./drf_todo
    image: docker-server/drf_todo
    container_name: main.django
    command: uwsgi --ini uwsgi.ini
    volumes:
      - ./drf_todo:/srv/docker-server
      - ./log:/var/log/uwsgi
    depends_on: 
      - db
  nginx:
    build: ./nginx
    container_name: nginx
    image: docker-server/nginx
    restart: always
    volumes:
      - ./drf_todo:/srv/docker-server
    ports:
        - '80:80'
    depends_on:
      - web

ports

์—ฌ๊ธฐ์„œ 80:80์ด๋ผ๊ณ  ์„ค์ •ํ•ด ์ค€ ๊ฒƒ์€ port forwarding ์ด๋ผ๊ณ  ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

๋„์ปค ์ปจํ…Œ์ด๋„ˆ๋Š” 80๋ฒˆ ํฌํŠธ๋กœ listening์„ ํ•œ๋‹ค. (nginx ์„œ๋ฒ„์˜ ํฌํŠธ๋ฅผ 80๋ฒˆ์œผ๋กœ ์„ค์ •ํ–ˆ๊ธฐ ๋•Œ๋ฌธ.)

client์˜ ์š”์ฒญ์€ โ€˜๋„์ปค์˜ 80๋ฒˆโ€™ํฌํŠธ๋กœ ๋“ค์–ด๊ฐ€๊ณ , nginx์—์„œ ์„ค์ •ํ•ด์ค€ socket ๊ฒฝ๋กœ๋ฅผ ๋”ฐ๋ผ

uWSGI์— ์š”์ฒญ์„ ์ „๋‹ฌํ•œ๋‹ค.


volumes

๋‚ด ํ•˜๋“œ๋“œ๋ผ์ด๋ธŒ์˜ ๊ฒฝ๋กœ์™€ ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์˜ ๊ฒฝ๋กœ๋ฅผ ๋งคํ•‘ํ•ด์ฃผ๋Š”๊ฒƒ.

ํ•ด๋‹น ์„ค์ •์„ ํ•  ๊ฒฝ์šฐ, vscode์—์„œ ์ฝ”๋“œ ํŽธ์ง‘์„ ํ•˜๋ฉด, ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์˜ ์ฝ”๋“œ๋„ ๋ณ€๋™๋œ๋‹ค.


  • ๋‚ด์šฉ์ถ”๊ฐ€(22.01.13)

dockerfile์„ ์ƒ์„ฑํ•ด ์ค„๋•Œ, requirements.txt๋งŒ1 COPYํ•ด์ฃผ๊ณ  ๋‚˜๋จธ์ง€๋Š” docker-compose.yml์—์„œ

volumes์—์„œ ๊ฒฝ๋กœ๋ฅผ ๋งตํ•‘ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. (๊ตณ์ด ์ด๋ฏธ์ง€ ๋นŒ๋“œ์‹œ์— ๋ชจ๋“  ํŒŒ์ผ์„ COPYํ•  ํ•„์š” ์—†๋‹ค.)