データベースの設定
mysite/mysite/settings.py
を追加/編集します。
from pathlib import Path
import os # 追加
# SECRET_KEY = 'django-insecure-397uw9vefl&p92vxz72l+7zxk8nkd(33vlwcg1y_dihdjz0hfo'
SECRET_KEY = os.environ.get("SECRET_KEY") # 変更
# SECURITY WARNING: don't run with debug turned on in production!
# DEBUG = True
DEBUG = os.environ.get("DEBUG") == "True" # 変更
# ALLOWED_HOSTS = []
ALLOWED_HOSTS = ["localhost", "127.0.0.1", "[::1]"] # 変更
DATABASES = {
"default": {
"ENGINE": "django.db.backends.mysql",
# コンテナ内の環境変数をDATABASESのパラメータに反映
"NAME": os.environ.get("MYSQL_DATABASE"),
"USER": os.environ.get("MYSQL_USER"),
"PASSWORD": os.environ.get("MYSQL_PASSWORD"),
"HOST": "db",
"PORT": 3306,
"OPTIONS": {
"init_command": "SET sql_mode='STRICT_TRANS_TABLES'",
},
}
}
docker-compose.dev.yml
を編集し、データベース起動後にアプリケーションが起動するようにします。
1version: '3.9'
2services:
3 app:
4 container_name: django
5 build:
6 dockerfile: ./docker/app/Dockerfile
7 ports:
8 - 8000:8000
9 env_file: ./docker/.env.dev
10 command: poetry run python manage.py runserver 0.0.0.0:8000
11 depends_on:
12 db:
13 condition: service_healthy
14 db:
15 container_name: mysql
16 image: mysql:8
17 volumes:
18 - ./docker/db/data:/var/lib/mysql
19 - ./docker/db/conf.d:/etc/mysql/conf.d
20 env_file: ./docker/.env.dev
21 ports:
22 - "3306:3306"
23 privileged: true
24 healthcheck:
25 test: mysqladmin ping -h 127.0.0.1 -u$$MYSQL_USER -p$$MYSQL_PASSWORD
26 interval: 10s
27 timeout: 10s
28 retries: 3
29 start_period: 30s
コンテナを起動します。
docker compose -f docker-compose.dev.yml up -d
コンテナが起動していることを確認します。
docker compose -f docker-compose.dev.yml ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
django django-docker-sample-app "poetry run python m…" app 6 minutes ago Up 6 minutes 0.0.0.0:8000->8000/tcp, :::8000->8000/tcp
mysql mysql:8 "docker-entrypoint.s…" db 6 minutes ago Up 6 minutes (healthy) 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp
ログにエラーがないことを確認します。
docker compose -f docker-compose.dev.yml logs
マイグレーションファイルを生成します。
docker compose -f docker-compose.dev.yml exec app poetry run python manage.py makemigrations
Migrations for 'polls':
polls/migrations/0001_initial.py
- Create model Question
- Create model Choice
マイグレーションを実行します。
docker compose -f docker-compose.dev.yml exec app poetry run python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, polls, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying auth.0012_alter_user_first_name_max_length... OK
Applying polls.0001_initial... OK
Applying sessions.0001_initial... OK
コンテナを終了します。
docker compose -f docker-compose.dev.yml down