1. 파일 수정하기

  • requirments.txt 에 gunicorn 추가
  • Dockerfile 수정
FROM python:3

RUN apt-get update && apt-get -y install \
    libpq-dev

WORKDIR /app
ADD    ./requirements.txt   /app/
RUN    pip install -r requirements.txt

ADD    ./djangosample   /app/djangosample/
ADD    ./manage.py      /app/
ADD    ./blog           /app/blog/

EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0:8000"]
  • docker-compose.yml 수정
version: '3'

volumes:
  django_sample_db_dev: {}

services:
  db:
    image: postgres
    volumes:
      - django_sample_db_dev:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=sampledb
      - POSTGRES_USER=sampleuser
      - POSTGRES_PASSWORD=samplesecret
      - POSTGRES_INITDB_ARGS=--encoding=UTF-8

  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./nginx:/etc/nginx/conf.d
      - ./media:/var/media
      - .:/app
    depends_on:
      - django

  django:
    build:
      context: .
      dockerfile: ./compose/django/Dockerfile-dev
    environment:
      - DJANGO_DEBUG=True
      - DJANGO_DB_HOST=db
      - DJANGO_DB_PORT=5432
      - DJANGO_DB_NAME=sampledb
      - DJANGO_DB_USERNAME=sampleuser
      - DJANGO_DB_PASSWORD=samplesecret
      - DJANGO_SECRET_KEY=dev_secret_key
    ports:
      - "8000:8000"
    command:
      - bash
      - -c
      - |
        /wait-for-it.sh db:5432 -t 10
        python manage.py runserver 0:8000
      - gunicorn sampleuser.wsgi:application --bind 0.0.0.0:8000
    restart: always
    volumes:
      - ./:/app/
    expose:
      - "8000"

2. nginx.conf 파일 만들기

~$ mkdir config
~$ cd config
~$ mkdir nginx
~$ cd nginx
~$ vim nginx.conf

아래 내용을 붙여 넣는다.

uptream web {
    ip_hash;
    server django:8000; # 서버의 컨테이너 명
}

server {
    listen 80;
    server_name localhost;

    location /static/ {
        autoindex on;
        alias /code/static;
    }

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

3. host 추가하기

djangosample/settings.py 수정

ALLOWED_HOSTS = ['django']

4. url 설정

djangosample/urls.py 수정

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('blog.urls')),
]

blog/urls.py 수정

from django.urls import path
from . import views

urlpatterns = [
    path('', views.post_list, name='post_list'),
]

(view설명 추가하기)

5. 실행

docker-compose up -d --build