CLOUD/Docker&Kubernetes

[Docker/Pycharm] Nginx - Gunicorn - MySQL 3계층 연동하기

alsruds 2023. 4. 19. 18:57

☆ Pycharm Professional 버전 준비 ( 도커 사용 ) ☆

 

 

[ 기본 구성 ]

● Pycharm에서 Docker 연결할 수 있게 설정

# vi /usr/lib/systemd/system/docker.service 변경
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 --containerd=/run/containerd/containerd.sock

# 파일 변경 후 재시작
systemctl daemon-reload
systemctl restart docker

 

※ 다시 원래대로 돌릴 때

alias docker="docker -H [본체 ip]"

 

방화벽 설정

》 방화벽이 활성화 되어 있어야 도커 네트워크 사용 가능

firewall-cmd --permanent --add-port=2375/tcp
firewall-cmd --reload

 

[ Pycharm - Docker 연동 ]

File > Settings > Build, Execution, Deployment > Docker > Docker for Windows

리눅스에 연결하려면 TCP socket 에 본체 ip:2375 작성

 

[ Dockerfile & docker-compose.yml 작성 ]

현재 구성

 

back (Gunicorn)

 

》 Dockerfile

FROM python:3.9
ENV PYTHONUNBUFFERED 1
WORKDIR /app
COPY requirements.txt /app/requirements.txt
RUN pip install -r requirements.txt
COPY . /app

requirements.txt

# pip freeze > requirements.txt 로 만들어주기
asgiref==3.6.0
Django==4.2
django-cors-headers==3.14.0
gunicorn==20.1.0
mysqlclient==2.1.1
sqlparse==0.4.4
tzdata==2023.3

 

front (Nginx)

Dockerfile

FROM nginx:latest
RUN rm -rf /usr/share/nginx/html/index.html
ADD build /usr/share/nginx/html/
RUN rm -rf /etc/nginx/conf.d/default.conf
ADD default.conf /etc/nginx/conf.d/default.conf

default.conf

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /usr/share/nginx/html;	# index.html 보여주는 경로

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

	location /dev {
                proxy_set_header Host $http_host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_pass http://192.168.0.177:8000;	# 본체 ip 작성
        }

}

# default.conf 파일을 적용시키려면 container build 가 필요
# docker.compose.yml 우클릭 > modify run configuration > docker-compose up에 build: always 추가

 

docker-compose.yml

services:
  frontend:
    build:
      context: ./front
      dockerfile: Dockerfile
    command: nginx -g "daemon off;"
    ports:
      - 80:80
  db:
    image: mysql:8.0.32-debian
    command: --init-file /init/db.sql	# init 폴더 생성 후 db.sql 만들어주기
    volumes:
      - ./data:/var/lib/mysql	# data 폴더 생성해주기
      - ./init:/init
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: qwer1234
    ports:
      - 3306:3306
    healthcheck:
      test: mysqladmin ping -pqwer1234
      timeout: 20s
      retries: 10
  backend:
    build:
      context: ./back
      dockerfile: Dockerfile
    command: sh -c "sleep 5 && python manage.py makemigrations && python manage.py migrate && gunicorn config.wsgi --bind 0.0.0.0:8000"
    ports:
      - 8000:8000
    depends_on:
      db:
        condition: service_healthy
        
# docker-compose.yml 우클릭 > modify run configuration > before launch > run build

 

》 db.sql

DROP DATABASE IF EXISTS `web`;
CREATE DATABASE IF NOT EXISTS `web`;