🌱 Infra/Container_Docker

[Docker-Basic (12)] Docker Compose Sample App - Django

mini_world 2020. 10. 5. 15:12

이번 포스팅에서는 아래 URL을 참조하여 Django+postgresql 컨테이너를 실행해보도록 하겠습니다 :)
docs.docker.com/compose/django/

 

Quickstart: Compose and Django

This quick-start guide demonstrates how to use Docker Compose to set up and run a simple Django/PostgreSQL app. Before starting, install Compose. Define the project components For this project, you...

docs.docker.com

 

🐳 Step1. 프로젝트 구성요소 정의

 

1. 프로젝트(어플리케이션) 루트 디렉토리 생성

프로젝트에서 사용할 디렉토리를 생성합니다.
저는, Docker라는 디렉토리 하위에 프로젝트에서 사용할 루트 디렉토리를 생성했습니다.

<!--프로젝트의 루트 디렉토리 생성 -->
[root@master Docker]# mkdir /Docker/Django
[root@master Docker]# cd /Docker/Django/

<!-- 프로젝트의 루트 디렉토리 -->
[root@master Django]# pwd
/Docker/Django

 

2. Dockerfile 생성

도커파일을 생성합니다.

[root@master Django]# vim Dockerfile 
<!-- 여기서부터 -->
FROM python:3
ENV PYTHONUNBUFFERED=1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
<!-- 여기까지 복붙! -->

 

3. requirements.txt 생성

[root@master Django]# vim requirements.txt
<!-- 여기부터 -->
Django>=3.0,<4.0
psycopg2-binary>=2.8
<!-- 여기까지 복붙 -->

 

4. docker-compose.yml 생성

[root@master Django]# cat docker-compose.yml 
<!-- 여기부터 -->
version: "3.8"
   
services:
  db:
    image: postgres
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
<!-- 여기까지 복붙 -->

 

🐳 Step2. Django 프로젝트 만들기

 

1. Django 프로젝트 생성

"docker-compose run web django-admin startproject composeexample . " 명령을 실행합니다.
docker-compose run을 실행하면 한번만 실행되며 특정 명령을 수행할 수 있습니다.

[root@master Django]# docker-compose run web django-admin startproject composeexample .

Creating network "django_default" with the default driver
Pulling db (postgres:)...
latest: Pulling from library/postgres
d121f8d1c412: Pull complete
9f045f1653de: Pull complete
fa0c0f0a5534: Pull complete
54e26c2eb3f1: Pull complete
cede939c738e: Pull complete
69f99b2ba105: Pull complete
218ae2bec541: Pull complete
70a48a74e7cf: Pull complete
c0159b3d9418: Pull complete
353f31fdef75: Pull complete
03d73272c393: Pull complete
8f89a54571bf: Pull complete
4885714928b5: Pull complete
3060b8f258ec: Pull complete
Digest: sha256:0171a93d62342d2ab2461069609175674d2a1809a1ad7ce9ba141e2c09db1156
Status: Downloaded newer image for postgres:latest
Building web
Step 1/7 : FROM python:3
3: Pulling from library/python
57df1a1f1ad8: Pull complete
71e126169501: Pull complete
1af28a55c3f3: Pull complete
03f1c9932170: Pull complete
65b3db15f518: Pull complete
3e3b8947ed83: Pull complete
f156949921a1: Pull complete
1c1931013093: Pull complete
51fff639b6bf: Pull complete
Digest: sha256:1a126607adde46a706e76357c910f36b9f5529fb575d4d86a639a4997daceba7
Status: Downloaded newer image for python:3
 ---> bbf31371d67d
Step 2/7 : ENV PYTHONUNBUFFERED=1
 ---> Running in 6e7be032d4aa
Removing intermediate container 6e7be032d4aa
 ---> 85ba962c8f65
Step 3/7 : RUN mkdir /code
 ---> Running in 39a9df086f4e
Removing intermediate container 39a9df086f4e
 ---> e10ffc81c69a
Step 4/7 : WORKDIR /code
 ---> Running in 040b8ae942ed
Removing intermediate container 040b8ae942ed
 ---> b920418dc417
Step 5/7 : COPY requirements.txt /code/
 ---> 24b749b8a8a9
Step 6/7 : RUN pip install -r requirements.txt
 ---> Running in b56e2869dad3
Collecting Django<4.0,>=3.0
  Downloading Django-3.1.2-py3-none-any.whl (7.8 MB)
Collecting psycopg2-binary>=2.8
  Downloading psycopg2_binary-2.8.6-cp38-cp38-manylinux1_x86_64.whl (3.0 MB)
Collecting asgiref~=3.2.10
  Downloading asgiref-3.2.10-py3-none-any.whl (19 kB)
Collecting pytz
  Downloading pytz-2020.1-py2.py3-none-any.whl (510 kB)
Collecting sqlparse>=0.2.2
  Downloading sqlparse-0.3.1-py2.py3-none-any.whl (40 kB)
Installing collected packages: asgiref, pytz, sqlparse, Django, psycopg2-binary
Successfully installed Django-3.1.2 asgiref-3.2.10 psycopg2-binary-2.8.6 pytz-2020.1 sqlparse-0.3.1
Removing intermediate container b56e2869dad3
 ---> 633386adb88f
Step 7/7 : COPY . /code/
 ---> abb703e28c74

Successfully built abb703e28c74
Successfully tagged django_web:latest
WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating django_db_1 ... done
Creating django_web_run ... done

<!-- 새로 생성된 파일들 확인 -->
[root@master Django]# tree /Docker/Django/
/Docker/Django/
├── Dockerfile
├── composeexample       <!-- NEW -->
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── docker-compose.yml
├── manage.py             <!-- NEW -->
└── requirements.txt

 

2. Database 연결

데이터베이스를 연결할 수 있도록 연결 정보를 넣어줍니다.
./composeexample/settings.py 파일에 DATABASE 부분에 아래와 같이 넣어줍니다.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 'db',
        'PORT': 5432,
    }}

하나더! ALLOWED_HOSTS = ['*'] 으로 바꿔줍니다 (access Deny떠요!)

[root@master Django]# vim composeexample/settings.py 

"""
Django settings for composeexample project.

Generated by 'django-admin startproject' using Django 3.1.2.

For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'l-b2yv-*7xfuv3wd=&_cw8r&fr6m87hz%z&%7x2r9(!-yjp9e9'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

<!-- 이부분 변경 -->
ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'composeexample.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'composeexample.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

<!-- 여기 부분 추가 -->
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 'db',
        'PORT': 5432,
    }
}
<!-- 여기 까지 추가 -->
<!-- 아래 부분 주석처리 -->
#DATABASES = {
#    'default': {
#        'ENGINE': 'django.db.backends.sqlite3',
#        'NAME': BASE_DIR / 'db.sqlite3',
#    }
#}


# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

STATIC_URL = '/static/'

 

🐳 Step3. Docker-compose로 앱 시작하기

자, 이제 모든 준비가 완료되었습니다.
docker-compose up -d 명령으로 백그라운드로 컨테이너를 실행해보겠습니다.

<!-- Docker compuse 시작 -->
[root@master Django]# docker-compose up -d
Starting django_db_1 ... done
Starting django_web_1 ... done

<!-- Docker 컨테이너 실행 확인 -->
[root@master Django]# docker container ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
b696123f7e9a        django_web          "python manage.py ru…"   32 minutes ago      Up 2 minutes        0.0.0.0:8000->8000/tcp   django_web_1
6083622112a1        postgres            "docker-entrypoint.s…"   33 minutes ago      Up 2 minutes        5432/tcp                 django_db_1

 

앱 실행이 되었는지, 웹브라우저로 들어가봅시다!

잘 동작됩니다! 🥳🥳

 

🐳 Step4. Docker-compose container 삭제

자, 이제 모든 테스트가 완료되었으니 컨테이너를 삭제해줍니다.

<!-- 도커 컨테이너 전체 종료 및 삭제 --> 
[root@master Django]# docker-compose down
Stopping django_web_1 ... done
Stopping django_db_1  ... done
Removing django_web_1                ... done
Removing django_web_run_361710b9732d ... done
Removing django_db_1                 ... done
Removing network django_default

<!-- 도커 컨테이너 목록 확인 -->
[root@master Django]# docker container ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

 

공식홈페이지에 가면 더 해볼수있도록 여러가지 샘플을 제공하고 있으므로, 더 공부하고 싶다면 아래 링크로 들어가세요!!

docs.docker.com/samples/

 

Samples

Tutorial labs Learn how to develop and ship containerized applications, by walking through a sample that exhibits canonical practices. These labs are from the Docker Labs repository. Sample Description Docker...

docs.docker.com

끝!

728x90