이번에는 docker compose을 통해 nginx와 django 프로젝트를 연결한다.
🐳 docker-compose.yaml
version: "3.9"
services:
db:
image: postgres
ports:
- "5432:5432"
volumes:
- ./data:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=postgres
backend:
build: backend/.
command: gunicorn config.wsgi:application --bind 0.0.0.0:8000
ports:
- "8000:8000"
volumes:
- ./backend:/app
- static_volume:/app/static
- media_volume:/app/media
depends_on:
- db
nginx:
image: nginx
ports:
- "80:80"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
- static_volume:/static
- media_volume:/media
depends_on:
- backend
volumes:
static_volume:
media_volume:
도커내의 nginx.conf와 default.conf 파일 변경을 위해 로컬에 nginx 폴더를 만든 다음 각각 defalut.conf와 nginx.conf을 마운트 했다.
📁 nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
client_max_body_size 0;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
📁 deafult.conf
upstream backend {
server backend:8000;
}
server {
listen 80;
location / {
proxy_pass http://backend;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /media/ {
autoindex on;
alias /media/;
}
location /static/ {
autoindex off;
alias /static/; # settings.STATIC_ROOT와 동일
}
}
docker compose up 명령어를 하기 전에 장고 프로젝트로 들어가 python manage.py collectstatic 명령어를 입력하여 정적 파일을 모은다. 이때 정적 파일의 위치는 장고 프로젝트의 settings.py의 STATIC_ROOT의 위치에 따라 결정된다.
그다음 터미널에 docker compose up 명령어를 통해 컨테이너를 실행시키고 80 포트로 들어가면 nginx와 django 프로젝트가 성공적으로 연결되었음을 확인할 수 있다.
'Cloud > Docker' 카테고리의 다른 글
docker compose을 이용하여 django, postgres 연결하기 (0) | 2024.07.25 |
---|---|
Docker compose 작성하는 방법과 명령어 (0) | 2024.07.25 |
Docker Hub와 이미지 버전 관리 (1) | 2024.07.25 |
Dockerfile 작성하는 방법과 Docker 명령어 사용하기 (0) | 2024.07.25 |
Docker에 대해서 알아보기 (0) | 2024.07.25 |