I try to solve following task. I have Django app and I want to start it using Docker. As 'db' I add a container with PostgreSQL or MySQL. In my case I need run "python manage.py migrate" before "python manage.py runserver", so I wrote in docker-compose.yml file in fiels "command" folloving string:sh -c "sleep 20 && python manage.py migrate --noinput && python manage.py runserver 0.0.0.0:8000 --noreload". But after applying all migrations command "python manage.py migrate" doesn't stop and doesn't give to run "python manage.py runserver".
some lines....
web_1 | Applying datapage.0001_initial... OK
web_1 | Applying datapage.0002_auto_20190709_1955... OK
web_1 | Applying datapage.0003_auto_20190713_1358... OK
web_1 | Applying datapage.0004_auto_20190720_1107... OK
web_1 | Applying sessions.0001_initial... OK
If I use:
docker-compose exec web python manage.py migrate
we have same result:
some lines....
web_1 | Applying datapage.0001_initial... OK
web_1 | Applying datapage.0002_auto_20190709_1955... OK
web_1 | Applying datapage.0003_auto_20190713_1358... OK
web_1 | Applying datapage.0004_auto_20190720_1107... OK
web_1 | Applying sessions.0001_initial... OK
After pressing Ctrl + C, we get the following error:
^CException ignored in: <module 'threading' from '/usr/local/lib/python3.7/threading.py'>
Traceback (most recent call last):
File "/usr/local/lib/python3.7/threading.py", line 1307, in _shutdown
lock.acquire()
KeyboardInterrupt
Settings of docker-compose.yml:
version: "3.7"
services:
db:
image: postgres
restart: always
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
web:
build: .
command: >
sh -c "sleep 20 &&
python manage.py migrate --noinput &&
python manage.py runserver 0.0.0.0:8000 --noreload"
ports:
- "8000:8000"
depends_on:
- db
In this case we don't have to run "python manage.py makemigrations", because it is first initialization of 'db', but if try to do this we will get:
No changes detected
(pressing Ctrl + C)
^CException ignored in: <module 'threading' from '/usr/local/lib/python3.7/threading.py'>
Traceback (most recent call last):
File "/usr/local/lib/python3.7/threading.py", line 1307, in _shutdown
lock.acquire()
KeyboardInterrupt
Such behavior I get in case working with PostgreSQL и MySQL (by Docker and local server), but with SQLite "python manage.py migrate" work correct and come back to console. Also I checked that problem is not in long work of command "python manage.py migrate", I stayed it to works at night, but it didn't help.
I tried to change versions in range python 3.5-3.9 и Django 2.0-2.2, but I got same result. Also I checked it in clear project that was created with "django-admin startproject", eventually result same.
Google didn't help me with finding answers for my case. But it is strange that I use same way for using "python manage.py migrate" in Docker as here and in similar sources, so I'm very confused, where I have made mistake.
So I want to know, why "python manage.py migrate" gives such behavior, might it is Django ORM bug? Maybe you met this misstake and know how solve it, in order to this command works from docker-compose.yml?
Also give link for project code on GitHub