Synology/1. NAS를 웹서버로 사용하기

3. [시놀로지에 웹서버 띄우기] Docker 컨테이너로 묶고 Github에 Push하기 (docker, nginx, django, gunicorn)

cha2hyun 2021. 12. 7. 18:11

로컬 환경에서 도커가 깔려있다는 전제하에 다음 내용을 진행하겠다. docker-compose up을 통해 빌드하고 실행시킬 예정이다.


1. docker-compose up 빌드해보기

- 프로젝트 최상단 루트에 Dockerfile 을 추가한다.
- Dockerfile 

FROM python:3.8
RUN mkdir /code
WORKDIR /code

ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/​


- 프로젝트 최상단 루트에 docker-compose.yml 을 추가한다.
- docker-compose.yml

version: '3'
services:
  web:
    build:
      context: .
      dockerfile : Dockerfile
    command : python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code/
    ports:
      - "8000:8000"​

 

- 도커로 서버를 실행해본다.
- 127.0.0.1:8000 으로 들어가본다.

docker-compose up -build​

실행이 잘 된다.
도커 Gui에서도 컨테이너가 생성되고 터미널을 열어서 어떤 요청이 왔는지 확인할 수 있다.

 



2. Docker와 Nginx + Django + Gunicorn 으로 실행하기

- gunicorn 설치.

pipenv install gunicorn​


- nginx 연결과정 1 - nginx.conf 설정
- config > nginx > nginx.conf 생성
* 주의사항 : nas 가 기본적으로 80포트를 사용하기 때문에 nginx 는 100번 포트를 사용했다
- nginx.conf

upstream web {
    ip_hash;
    server web:8000;
}
server {
    location /static/ {
        alias /code/static;
    }

    location / {
        proxy_pass http://web/;
    }

    listen 100;
    server_name localhost;
}​

 

- nginx 연결과정 2
- 프로그램 최상위 루트에 생성한 docker-compose.yml 수정하여 nignx를 도커에 실행
- docker-compose.yml

version: '3'
services:
  nginx:
    image: nginx:latest
    ports:
      - "10:100"
    volumes:
      - .:/code
      - ./config/nginx:/etc/nginx/conf.d
    depends_on:
      - web
  web:
    build:
      context: .
      dockerfile : Dockerfile
    command : gunicorn config.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - .:/code/
    ports:
      - "8000"

 

- 빌드해서 실행해본다.

docker-compose up --build​


- 위 명령어로 빌드하여 웹페이지에서 실행이 되었는지 127.0.0.1을 쳐본다 (포트없이)

127.0.0.1 혹은 localhost로 접속시 해당 페이지가 보여져야한다. (따로 포트를 입력 안해도 된다)


- 터미널에 보여지는 내용은 다음과 같다.

 


3. github에 업로드

- 깃허브에 적당한 이름의 레포지토리를 만들고 푸쉬하면 된다. 
- 추후 시놀로지에서 깃허브에서 풀땡겨서 가져와야 한다.

git init
git remote add origin https://github.com/XXX/XXX.git
git add .
git commit -m "first commit"
git push origin master

 

다음화에서는 로컬에서 만든 프로젝트를 시놀로지 DS920+ 에 배포하는 내용을 다룰 예정이다.


2021.12.07 - [분류 전체보기] - 3. Synology에 배포하기 (docker, nginx, django, gunicorn)

 

3. Synology에 배포하기 (docker, nginx, django, gunicorn)

마지막 챕터로 시놀로지에 배포하려고 한다. 이전 글을 꼼꼼히 확인해주셨으면 아시겠지만 이 챕터에서 필요한건 synology에서 ddns, ssh 설정 docker, gitserver 설치 과정이 필요하고 자세한 설정 방법

cha2hyun.tistory.com