- Docker
Backend / Frontend 배포
[postgresql 컨테이너 생성]
- root : vi docker-compose.yml
version: '3.8'
volumes:
postgres: {} # postgresql에서 사용 할 볼륨 지정
services:
postgres:
container_name: postgres
image: postgres:14.5
volumes:
- postgres:/var/lib/postgresql/data/
environment: # postgresql 컨테이너에서 사용할 환경변수 지정해주기
- POSTGRES_USER=user # 데이터베이스 사용자 지정
- POSTGRES_PASSWORD=P@ssw0rd # 사용자 비밀번호 지정
- POSTGRES_DB=django # 데이터베이스 이름 지정
restart: always
- sudo docker compose up -d / sudo docker compose logs → 실행 후 정상작동 확인
[gunicorn 컨테이너 생성]
내가 배포 할 프로젝트를 담은 컨테이너 생성
1.
배포할 프로젝트 들어갈 mkdir 후 → git clone 주소 ./django (프로젝트 들어갈 경로 지정)
#1
ALLOWED_HOSTS = ['*']
STATIC_ROOT = BASE_DIR / "static"
#2
timezone을 변경해주기 위해, 기존 timezone 설정을 Asia/Seoul로 덮어씁니다.
sudo ln -sf /usr/share/zoneinfo/Asia/Seoul /etc/localtime
이후 settings.py / timezone 설정필요!
2.
backend/Dockerfile
# python 3.10.8버전 이미지를 사용해 빌드
FROM python:3.10.6
# .pyc 파일을 생성하지 않도록 설정합니다.
ENV PYTHONDONTWRITEBYTECODE 1
# 파이썬 로그가 버퍼링 없이 즉각적으로 출력하도록 설정합니다.
ENV PYTHONUNBUFFERED 1
# /app/ 디렉토리를 생성합니다.
RUN mkdir /app/
RUN apt-get update
RUN apt-get -y install libgl1-mesa-glx
# /app/ 경로를 작업 디렉토리로 설정합니다.
WORKDIR /app/
# requirments.txt를 작업 디렉토리(/app/) 경로로 복사합니다.
COPY ./django/requirements.txt .
# secrets.json .을 작업 디렉토리(/app/) 경로로 복사합니다.
COPY ./django/secrets.json .
# 프로젝트 실행에 필요한 패키지들을 설치합니다.
RUN pip install --no-cache-dir -r requirements.txt
# gunicorn을 사용하기 위한 패키지를 설치합니다.
RUN pip install gunicorn psycopg2
3.
root : docker-compose.yml
version: '3.8'
services:
backend:
container_name: backend
build: ./backend/
# drf_project.wsgi는 프로젝트 경로에 맞게 지정해야 합니다.
entrypoint: sh -c "python manage.py collectstatic --no-input && python manage.py migrate && gunicorn drf_project.wsgi --workers=5 -b 0.0.0.0:8000"
ports:
- 80:8000
volumes:
- ./backend/django/:/app/
- /etc/localtime:/etc/localtime:ro # host의 timezone 설정을 컨테이너에 적용합니다.
# ro 은 읽기 전용(read only) 속성으로 볼륨을 설정하는 것을 의미합니다.
restart: always
👉 entrypoint 설정 주의!
[nginx를 사용해 웹서버 컨테이너 생성]
1.
root : docker-compose.yml
version: '3.8'
services:
nginx:
container_name : nginx
image: nginx:1.23.2
ports:
- "80:80" # http 포트포워딩
- "443:443" # https 포트포워딩
restart: always
'woncoding > TIL' 카테고리의 다른 글
| TIL | 12.20.화 [DRF ↔️ JS CRUD] (0) | 2022.12.23 |
|---|---|
| TIL | 12.19.월 [Docker | nginx + postgresql + django] (0) | 2022.12.19 |
| TIL | 12.15.목 [DRF / Deep Learning / JS] (0) | 2022.12.16 |
| TIL | 12.14.수 [DRF ↔️ JS CRUD] (0) | 2022.12.15 |
| TIL | 12.13.화 [DRF ↔️ JS CRUD] (0) | 2022.12.13 |