Django Docker 설정

Django

# /Dockerfile
FROM python:3.9.12-bullseye
WORKDIR /app
COPY . /app

ENV DOCKERIZE_VERSION v0.6.1
RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
    && tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
    && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz

ENTRYPOINT ["dockerize", "-wait", "tcp://sagi_db:3306", "-timeout", "30s"]

RUN pip install Django djangorestframework djangorestframework-simplejwt mysqlclient django-cors-headers
RUN pip install pyJWT==1.7.1

RUN apt-get update
RUN apt-get install tzdata
RUN export TZ=Asia/Seoul

RUN python manage.py makemigrations
CMD [ "python", "manage.py", "runserver", "0.0.0.0:8000" ]
EXPOSE 8000
# /docker-compose.yml
version: '3'

services:
    apiserver:
        build:
            context: .
            dockerfile: Dockerfile
        container_name: sagi_api
        volumes:
            - ./:/app/
        depends_on:
            - db
        ports: 
            - 8000:8000
        restart: always

MySQL


root 폴더에 위와 같이 폴더를 만들어줍니다.

# /docker-compose.yml
version: '3'

services:
    db:
        image: mysql:5.7
        container_name: sagi_db
        volumes:
            - ./db/conf.d:/etc/mysql/conf.d
            - ./db/data:/var/lib/mysql
            - ./db/initdb.d:/docker-entrypoint-initdb.d
        env_file: .env
        environment:
            TZ: Asia/Seoul
        ports:
            - 3306:3306
        restart: always
# /.env
MYSQL_ROOT_PASSWORD = root_pwd
MYSQL_USER          = user_id
MYSQL_PASSWORD      = user_pwd
MYSQL_DATABASE      = table_name

Ignore

# /.dockerignore
.git
.prettierrc
venv
resources
db
.dockerignore
docker-compose.yml
Dockerfile

위의 설정이 모두 끝나면..

# /mysite/settings.py

# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases
DATABASES = {
    'default': {
        'ENGINE':   'django.db.backends.mysql',
        'NAME':     table_name,
        'USER':     user_id,
        'PASSWORD': user_pwd,
        'HOST':     "docker_db",
        'PORT':     post
    }
}
# [Bash]
> docker-compose up -d

좋은 웹페이지 즐겨찾기