Skip to content

Commit

Permalink
add entrypoint.sh (auto migrate)
Browse files Browse the repository at this point in the history
  • Loading branch information
twtrubiks committed Mar 26, 2022
1 parent a049ae0 commit 398cb2f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,8 @@ docker-compose up

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

:exclamation: 已經更新為自動 migrate:exclamation:

但你仔細看上圖,你會發現他說你還沒 migrate

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

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

:exclamation: 已經更新為自動建立:exclamation:

請參考 [docker-compose.yml](https://github.com/twtrubiks/docker-tutorial/blob/master/docker-compose.yml) 中的 environment ( 如下 ),

`DJANGO_SUPERUSER_USERNAME` `DJANGO_SUPERUSER_PASSWORD` `DJANGO_SUPERUSER_EMAIL`

順便在建立一個 superuser

```cmd
Expand Down
2 changes: 2 additions & 0 deletions api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ WORKDIR /docker_api
COPY . /docker_api/
RUN pip install -r requirements.txt

# for entry point
RUN chmod +x /docker_api/docker-entrypoint.sh
7 changes: 7 additions & 0 deletions api/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh
echo 'Run migration'
python3 manage.py makemigrations musics
python3 manage.py migrate
echo 'Create Super User'
python3 manage.py createsuperuser --noinput || echo "Super user already created"
exec "$@"
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,17 @@ services:

web:
build: ./api
entrypoint: /docker_api/docker-entrypoint.sh
command: python manage.py runserver 0.0.0.0:8000
restart: always

# ref.
# https://docs.djangoproject.com/en/3.0/ref/django-admin/#django-admin-createsuperuser
environment:
- DJANGO_SUPERUSER_USERNAME=admin
- DJANGO_SUPERUSER_PASSWORD=admin1234
- DJANGO_SUPERUSER_EMAIL=admin@twtrubiks.com

volumes:
- api_data:/docker_api
# (HOST:CONTAINER)
Expand Down

0 comments on commit 398cb2f

Please sign in to comment.