๐ŸŒฑ 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