django + docker + Azure(SQL)๋ฐฐํฌ


๐Ÿ“Œ ์˜ค๋Š˜ ๋ฐฐ์šฐ๊ณ  ์ •๋ฆฌํ•œ ๋ชฉ์ฐจ


๏ผ‘. django + docker + Azure(SQL)๋ฐฐํฌ



๐Ÿ”Ž ๋„์ปค๋ฅผ ํ†ตํ•œ ๋ฐฐํฌ


๐Ÿ“Œ Azure๋ฐฐํฌ๋ฅผ ์œ„ํ•œ staticํŒŒ์ผ ์„ค์ •

๏ผ‘. ๋ฉ”์ธ ํ”„๋กœ์ ํŠธ ํŒŒ์ผ์— storages.py๋ฅผ ๋งŒ๋“ค์–ด ์ค€๋‹ค.

  • django-storage[azure] installํ•˜๊ธฐ
  • requirements.txt์— ์ ์–ด์ฃผ๊ธฐ
  • INSTALLED_APP์—๋Š” ์•ˆ์ ์–ด์ค˜๋„ ๋œ๋‹ค.
#storages.py

from storages.backends.azure_storage import AzureStorage

class StaticAzureStorage(AzureStorage):
    azure_container = "static"


๏ผ’. settings/prod.py์—์„œ STATICFILES_STORAGE ์„ค์ •ํ•œ๋‹ค.

-DEBUG = Fasle์ผ ๊ฒฝ์šฐ ALLOWED_HOSTS๋ฅผ ๊ผญ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค.

# prod.py
import os

DEBUG = os.environ.get("DEBUG") in ["1", "t", "true", "T", "True"]
ALLOWED_HOSTS = os.environ.get("ALLOWED_HOSTS", "").split(",")

STATICFILES_STORAGE = "gstore.storages.StaticAzureStorage"


๏ผ“. Azure Storages Accounts์—์„œ staticํด๋” ๋งŒ๋“ค๊ธฐ

์ด ๋ถ€๋ถ„์—์„œ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด์ค˜์•ผ ํ•˜๋Š”๋ฐ ์ฒ˜์Œ์—๋Š” ๋ชจ๋“  key๋“ค์„ .jsonํŒŒ์ผ์— ๊ด€๋ฆฌํ•ด์„œ

์‚ฌ์šฉํ–ˆ๋Š”๋ฐ ์ข‹์ง€ ์•Š์€ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ํ–ˆ๋‹ค.

ํด๋”๋ฅผ ๋งŒ๋“ค๊ณ  python manage.py collacticstatic --settings=project_name.settings.prod

์œ„ ๋ช…๋ น์–ด๋กœ Azure Storages Accounts์— ํ˜„์žฌ ํ”„๋กœ์ ํŠธ ๋‚ด ๋ชจ๋“  static ํŒŒ์ผ ๋ณต์‚ฌํ•ด์„œ ๋„ฃ๊ธฐ.




๐Ÿ“Œ Docker Image Build

๏ผ‘. Dockerfile ์ƒ์„ฑ

  • manage.py์™€ ๊ฐ™์€ ๊ฒฝ๋กœ์— ์ƒ์„ฑํ•ด์ค€๋‹ค.
FROM ubuntu:18.04

RUN apt-get update && \
    apt-get install -y python3-pip && \
    apt-get clean

RUN apt-get install -y libpq-dev

WORKDIR /code
ADD . /code
RUN pip3 install -r requirements.txt

ENV PYTHONUNBUFFERED=1

EXPOSE 80
CMD ["gunicorn", "gstore.wsgi:application", "--bind", "0.0.0.0:80"]

๋’ค์—์„œ ๋„์ปค ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ๋ถ€๋ถ„์—์„œ psycopg2 ๋ชจ๋“ˆ์„ ์ฐพ์ง€ ๋ชปํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒผ๋‹ค.

๊ตฌ๊ธ€๋ง ๊ฒฐ๊ณผ apt-get install -y libpq-dev๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋‹ค.


libpq์— ๋Œ€ํ•ด์„œ postgreSQL Docs์—์„œ ๊ฒ€์ƒ‰ํ•ด ๋ดค๋‹ค.

โ€œlibpq๋Š” PostgreSQL์— ๋Œ€ํ•œ C ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ”„๋กœ๊ทธ๋ž˜๋จธ์˜ ์ธํ„ฐํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค.

libpq๋Š” ํด๋ผ์ด์–ธํŠธ ํ”„๋กœ๊ทธ๋žจ์ด PostgreSQL ๋ฐฑ์—”๋“œ ์„œ๋ฒ„์— ์ฟผ๋ฆฌ๋ฅผ ์ „๋‹ฌํ•˜๊ณ 

์ด๋Ÿฌํ•œ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์ˆ˜์‹ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ•จ์ˆ˜ ์„ธํŠธโ€ ๋ผ๊ณ  ํ•œ๋‹ค.


๊ฒฐ๊ตญ ์˜ค๋ฅ˜์˜ ์›์ธ์€ ๋นŒ๋“œ ๊ณผ์ • ์ค‘์— postgresql library ํŒจํ‚ค์ง€๊ฐ€ ํ•„์š”ํ•œ๋ฐ,

์ด ํŒฉํ‚ค์ง€๊ฐ€ ์—†์–ด์„œ ๋นŒ๋“œ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐ๋œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  libpq๋ฅผ

์„ค์น˜ํ•จ์œผ๋กœ์จ psycopg2๋ชจ๋“ˆ ๋นŒ๋“œ์‹œ ํ•„์š”ํ•œ library๋ฅผ ๋‹ค์šด ๋ฐ›์€ ๊ฒƒ์ด์—ˆ๋‹ค.




๐Ÿ“Œ Azure PosetgreSQL + Docker migration

๏ผ‘. Azure Database for PostgreSQL servers

  • ์—ฌ๊ธฐ์— ๋“ค์–ด๊ฐ€์„œ DB ์ƒ์„ฑ


๏ผ’. settings/prod.py์—์„œ DB์„ค์ •

DATABASES = {
    "default": {
        "ENGINE": os.environ["DB_ENGINE"],
        "HOST": os.environ["DB_HOST"],
        "USER": os.environ["DB_USER"],
        "PASSWORD": os.environ["DB_PASSWORD"],
        "NAME": os.environ["DB_NAME"],
    },
}


๏ผ’. Docker migration

docker run --rm --publish 9998:80 
        -e DJANGO_SETTINGS_MODULE= 
        -e AZURE_ACCOUNT_NAME=
        -e AZURE_ACCOUNT_KEY=" " 
        -e ALLOWED_HOSTS=
        -e DB_ENGINE=
        -e DB_HOST=
        -e DB_USER=
        -e DB_PASSWORD=
        -e DB_NAME=
        -it name sh
  • docker shell์— ์ง„์ž…ํ•œ๋‹ค.

  • python3 manage.py showmigrations๋กœ ํ˜„์žฌ ์ƒํƒœ ํ™•์ธ

  • python3 manage.py migarte ์ง„ํ–‰

  • superuser ์ƒ์„ฑ




๐Ÿ“Œ Azure PaaS ์„œ๋น„์Šค์— ๋ฐฐํฌ

๏ผ‘. Docker Hub์— Push

> docker login

> docker push username/imagename:1.0


๏ผ’. Azure App Service

# Azure ๋‚ด๋ถ€์—์„œ VM์„ ์‚ฌ์šฉํ•œ๋‹ค.
> Publish : Doker Container

> OS : Linux

> Image Source : Docker Hub


๏ผ“. ์ถ”๊ฐ€ ์„ค์ •์‚ฌํ•ญ

> Properties์— ๋“ค์–ด๊ฐ„๋‹ค.

> Virtual IP address์™€ Outbound IP address๋ฅผ ๋ณต์‚ฌํ•œ๋‹ค.

> Connection security์— ๋“ค์–ด๊ฐ€์„œ ๋ถ™ํ˜€๋„ฃ๊ธฐ.

> Allow access to Azure services : YES