🐍
【Python】Dockerコマンドを使用して特定のコンテナでDjangoのマイグレーションを実行
はじめに
docker-compose exec <service-name> python manage.py makemigrations
makemigrations
execの後に<container-name>または<container-id>を入力して実行する
docker exec <container-name-or-id> python manage.py makemigrations
実行例
コンテナ名が【test-python-web-1】の場合
docker exec test-python-web-1 python manage.py makemigrations
コンテナIDが【123456de2~】の場合
docker exec 123456de2 python manage.py makemigrations
migrations
execの後に<container-name>または<container-id>を入力して実行する
docker exec <container-name-or-id> python manage.py migrate
実行例
コンテナ名が【test-python-web-1】の場合
docker exec test-python-web-1 python manage.py migrate
コンテナIDが【123456de2~】の場合
docker exec 123456de2 python manage.py migrate
実行結果
$ docker exec test-python-web-1 python manage.py makemigrations
[+] Creating 1/0
✔ Container test-python-db-1 Running 0.0s
Migrations for 'myapp':
myapp/migrations/0005_patient.py
- Create model Patient
$ docker exec test-python-web-1 python manage.py migrate
[+] Creating 1/0
✔ Container test-python-db-1 Running 0.0s
Operations to perform:
Apply all migrations: admin, auth, contenttypes, myapp, 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
おまけ(マイグレーション実行状況の確認)
execの後に<container-name>または<container-id>を入力して実行する
docker exec <container-name-or-id> python manage.py migrate
実行例
コンテナ名が【test-python-web-1】の場合
docker exec test-python-web-1 python manage.py showmigrations
コンテナIDが【123456de2~】の場合
docker exec 123456de2 python manage.py showmigrations
出力結果
$ docker exec test-python-web-1 python manage.py showmigrations
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
[X] 0003_logentry_add_action_flag_choices
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
[X] 0008_alter_user_username_max_length
[X] 0009_alter_user_last_name_max_length
[X] 0010_alter_group_name_max_length
[X] 0011_update_proxy_permissions
[X] 0012_alter_user_first_name_max_length
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
myapp
[X] 0001_initial
[X] 0002_auto_20240620_0431
[X] 0006_auto_20240630_2127
sessions
[X] 0001_initial
Discussion