์ด๋ฒ ํฌ์คํ
์์๋ ์๋ URL์ ์ฐธ์กฐํ์ฌ Django+postgresql ์ปจํ
์ด๋๋ฅผ ์คํํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค :)
docs.docker.com/compose/django/
๐ณ 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
๊ณต์ํํ์ด์ง์ ๊ฐ๋ฉด ๋ ํด๋ณผ์์๋๋ก ์ฌ๋ฌ๊ฐ์ง ์ํ์ ์ ๊ณตํ๊ณ ์์ผ๋ฏ๋ก, ๋ ๊ณต๋ถํ๊ณ ์ถ๋ค๋ฉด ์๋ ๋งํฌ๋ก ๋ค์ด๊ฐ์ธ์!!
๋!