1. 구조

django로 웹 애플리케이션을 개발하고 나서 nginx web 서버를 사용하여 동작할 수 있도록 구성한다
이때 각각 도커 컨테이너로 구성 되어 있으며 nginx 서버의 경우 https로 변경해준다
목록 | 버전 | |
1 | python | 3.8 |
2 | nginx -- https적용 | |
3 | docker | |
4 | docker-compose |
2-(1). dockerfile -- for django web application
# 베이스 이미지로 Python 3.8 사용
FROM python:3.8-slim
# 작업 디렉토리 설정
WORKDIR /app
# 필수 시스템 의존성 설치
RUN apt-get update && apt-get install -y --no-install-recommends \
gcc \
libpq-dev \
&& rm -rf /var/lib/apt/lists/*
# Python 의존성 설치를 위한 requirements.txt 복사
COPY requirements.txt /app/
# 의존성 설치
RUN pip install --no-cache-dir -r requirements.txt
# 애플리케이션 코드 복사
COPY . /app/
# 포트 노출
EXPOSE 8000
# Gunicorn 실행 명령 설정
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "myproject.wsgi:application"]
장고 서비스를 띄우기 위한 dockerfile이다
이때 각각 필요한 패키지는 다를 것이므로 알맞게 requirements.txt 를 설치하고 gunicorn을 사용하여 8000번 포트를 열어준다
테스트를 위해 컨테이너를 띄워 본다
docker build -t django-app . #dockerfile 빌드
docker run -p 8000:8000 django-app #docker container run
https://localhost:8000 으로 접속하여 서비스가 잘 돌아가는 지 확인한다
문제가 있는 경우, 아래와 같은 명령어를 활용하여 확인해 보도록 한다
docker ps #현재 실행중인 도커 확인
docker exec -it <container id or name> /bin/bash #컨테이너 접속
docker logs <container id or name> #컨테이너 로그 확인
2-(2). docker container -- for nginx
nginx는 컨테이너 이미지를 pull 받아서 그대로 사용하기로 한다
따라서 docker compose에서 받아서 사용할 예정이다
3-(1). https 변경
기본 웹서버는 http로 설정되어 있으므로 https로 사용할 수 있도록 추가 과정이 필요하다
이번에는 openssl을 사용해 보았고, 자체 인증서를 발급받아서 테스트 해보도록 한다
이번 작성은 테스트 용이므로, 실제 운영 환경에서는 인증된 인증서를 쓰는 것이 바람직하다
우분투 계열에서 진행했기 때문에 openssl을 설치하고 진행한다
sudo apt update
sudo apt install openssl
다음으로는 https인증에 필요한 인증서를 발급받는다
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout privkey.pem \
-out cert.pem \
-subj "/C=KR/ST=Seoul/L=Seoul/O=Example Inc./CN=localhost"
x.509 형식의 인증서를 발급 받고 privkey.pem이라는 이름으로 비공개키를 저장한다
인증서 이름은 cert.pem으로 설정한다
이때 -nodes옵션으로 비공개키를 암호화 하지 않았다
이렇게 생성된 파일을 도커 컨테이너 안에서 mount 할 수 있도록 경로를 맞추어 설정한다
** 참조
openssl genrsa -des3 -out privkey.pem 2048
openssl req -x509 -newkey rsa:2048 -days 365 -keyout privkey.pem -out cert.pem -nodes
위 처럼 하면 암호화 하여 진행된다
3-(2). nginx.conf 수정
nginx 웹 서버 설정을 적용하도록 한다
이때 파일 변경 후에 docker file에 mount가 제대로 설정되었는지 확인하는 것이 중요하다
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/nginx/certs/fullchain.pem;
ssl_certificate_key /etc/nginx/certs/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
location /static/ {
alias /static/;
}
location / {
proxy_pass http://web:8000;
proxy_set_header Host $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;
}
}
4. docker-compose.yml
이제 위의 설정들을 적용한 compose file 을 작성한다
version: '3.8'
services:
web:
build:
context: . # 현재 경로에서 dockerfile 빌드
dockerfile: Dockerfile
container_name: django_app
command: gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application #gunicorn 8000번 실행
ports:
- "8000:8000"
volumes:
- static_volume:/app/static
# nginx를 최신 버전으로 다운
nginx:
image: nginx:latest
container_name: nginx
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d #nginx 설정 파일 mount
- ./nginx/certs:/etc/nginx/certs #https 인증서 파일 경로
- static_volume:/static #web application의 운영에 생기는 data mount
depends_on:
- web
volumes:
static_volume:
각 주석을 구문에 대한 설명을 확인할 수 있다
docker-compose는 아래와 같은 명령어로 빌드 할 수 있다
docker-compose up --build #도커 컴포즈 build 및 띄우기
docker-compose down #도커 컨테이너들 내리기
위와 같은 과정들이 정상적으로 이루어졌다면
https:// ip 주소 ~~ 로 web application의 화면이 보여질 것이다
'Back-end > Docker' 카테고리의 다른 글
[Docker] Dockerfile 만들기 (0) | 2021.07.28 |
---|