Skip to content

Commit 398cb2f

Browse files
committed
add entrypoint.sh (auto migrate)
1 parent a049ae0 commit 398cb2f

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,8 @@ docker-compose up
13121312

13131313
![](https://i.imgur.com/iuCxLMY.png)
13141314

1315+
:exclamation: 已經更新為自動 migrate:exclamation:
1316+
13151317
但你仔細看上圖,你會發現他說你還沒 migrate
13161318

13171319
接下來我們開啟另一個 cmd 進入 web 的 service,
@@ -1334,6 +1336,12 @@ python manage.py migrate
13341336

13351337
![](https://i.imgur.com/zMmZKuL.png)
13361338

1339+
:exclamation: 已經更新為自動建立:exclamation:
1340+
1341+
請參考 [docker-compose.yml](https://github.com/twtrubiks/docker-tutorial/blob/master/docker-compose.yml) 中的 environment ( 如下 ),
1342+
1343+
`DJANGO_SUPERUSER_USERNAME` `DJANGO_SUPERUSER_PASSWORD` `DJANGO_SUPERUSER_EMAIL`
1344+
13371345
順便在建立一個 superuser
13381346

13391347
```cmd

api/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ WORKDIR /docker_api
66
COPY . /docker_api/
77
RUN pip install -r requirements.txt
88

9+
# for entry point
10+
RUN chmod +x /docker_api/docker-entrypoint.sh

api/docker-entrypoint.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
echo 'Run migration'
3+
python3 manage.py makemigrations musics
4+
python3 manage.py migrate
5+
echo 'Create Super User'
6+
python3 manage.py createsuperuser --noinput || echo "Super user already created"
7+
exec "$@"

docker-compose.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,17 @@ services:
2121

2222
web:
2323
build: ./api
24+
entrypoint: /docker_api/docker-entrypoint.sh
2425
command: python manage.py runserver 0.0.0.0:8000
2526
restart: always
27+
28+
# ref.
29+
# https://docs.djangoproject.com/en/3.0/ref/django-admin/#django-admin-createsuperuser
30+
environment:
31+
- DJANGO_SUPERUSER_USERNAME=admin
32+
- DJANGO_SUPERUSER_PASSWORD=admin1234
33+
- DJANGO_SUPERUSER_EMAIL=admin@twtrubiks.com
34+
2635
volumes:
2736
- api_data:/docker_api
2837
# (HOST:CONTAINER)

0 commit comments

Comments
 (0)