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
ルートフォルダに上記のフォルダを作成します.
# /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
Reference
この問題について(Django Docker設定), 我々は、より多くの情報をここで見つけました https://velog.io/@ksk7584/Django-Docker-설정テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol