I have set up a Docker Django/PostgreSQL app closely following the Django Quick Start instructions on the Docker site.
我在Docker站点上按照Django快速启动指令设置了一个Docker Django/PostgreSQL应用程序。
The first time I run Django's manage.py migrate, using the command sudo docker-compose run web python manage.py migrate
, it works as expected. The database is built inside the Docker PostgreSQL container just fine.
我第一次管理Django的管理。py迁移,使用sudo docker- composer运行web python管理命令。py迁移,它按照预期工作。数据库构建在Docker PostgreSQL容器中。
Changes made to the Django app itself are likewise reflected in the Docker Django container, the moment I save them. It's great!
对Django应用程序本身所做的更改也同样反映在Docker Django容器中,也就是我保存它们的那一刻。太好了!
But if I then change a model in Django, and try to update the Postgres database to match the model, no changes are detected so no migration happens no matter how many times I run makemigrations
or migrate
again.
但是,如果我然后在Django中更改一个模型,并尝试更新Postgres数据库以匹配该模型,则不会检测到任何更改,因此无论我运行了多少次makmigration或再次迁移,都不会发生迁移。
Basically, every time I change the Django model, I have to delete the Docker containers (using sudo docker-compose rm
) and start afresh with a new migration.
基本上,每当我更改Django模型时,我都必须删除Docker容器(使用sudo Docker - composer rm),并重新开始新的迁移。
I'm still trying to get my head around Docker, and there's an awful lot I don't understand about how it works, but this one is driving me nuts. Why doesn't migrate see my changes? What am I doing wrong?
我还在试图说服Docker,我有很多不明白的地方,但是这个让我抓狂。为什么移植看不到我的变化?我做错了什么?
3 个解决方案
#1
40
You just have to log into your running docker container and run your commands.
您只需登录到正在运行的docker容器并运行您的命令。
- Build your stack :
docker-compose build -f path/to/docker-compose.yml
- 构建您的堆栈:docker- composer构建的-f路径/to/docker-com .yml
- Launch your stack :
docker-compose up -f path/to/docker-compose.yml
- 启动堆栈:docker- composition up -f path/to/docker-com .yml
- Display docker running containers :
docker ps
- 显示docker正在运行的容器:docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3fcc49196a84 ex_nginx "nginx -g 'daemon off" 3 days ago Up 32 seconds 0.0.0.0:80->80/tcp, 443/tcp ex_nginx_1
66175bfd6ae6 ex_webapp "/docker-entrypoint.s" 3 days ago Up 32 seconds 0.0.0.0:32768->8000/tcp ex_webapp_1
# postgres docker container ...
- Get the CONTAINER ID of you django app and log into :
- 获取您django app的货柜ID并登录:
docker exec -t -i 66175bfd6ae6 bash
-
Now you are logged into, then go to the right folder :
cd path/to/django_app
现在您已登录,然后转到正确的文件夹:cd路径/to/django_app
-
And now, each time you edit your models, run in your container :
python manage.py makemigrations
andpython manage.py migrate
现在,每次编辑模型时,都在容器中运行:python管理。py移民和python管理。py迁移
I also recommend you to use a docker-entrypoint for your django docker container file to run automatically :
我还建议您使用docker-entrypoint作为django docker容器文件的自动运行:
- collecstatic
- collecstatic
- migrate
- 迁移
- runserver or start it with gunicorn or uWSGI
- 运行服务器或者从gunicorn或uWSGI开始
Here is an example (docker-entrypoint.sh
) :
这里有一个例子(docker-entrypoint.sh):
#!/bin/bash
# Collect static files
echo "Collect static files"
python manage.py collectstatic --noinput
# Apply database migrations
echo "Apply database migrations"
python manage.py migrate
# Start server
echo "Starting server"
python manage.py runserver 0.0.0.0:8000
#2
20
I use these method:
我用这些方法:
services:
web:
build: .
image: uzman
command: python manage.py runserver 0.0.0.0:8000
ports:
- "3000:3000"
- "8000:8000"
volumes:
- .:/code
depends_on:
- migration
- db
migration:
image: uzman
command: python manage.py migrate --noinput
volumes:
- .:/code
depends_on:
- db
Using docker
hierarchy we made, the service migration runs after set up the database and before to run the main service. Now when you run your service docker
will run migrations before runs the server; look that migration
server is applied over the same image that web server, it means that all migrations will be taken from your project, avoiding problems.
使用我们创建的docker层次结构,服务迁移在设置数据库之后和运行主服务之前运行。现在,当您运行服务docker时,将在运行服务器之前运行迁移;请注意,迁移服务器应用于与web服务器相同的映像,这意味着所有迁移都将从项目中获取,从而避免出现问题。
You avoid made entry point or whatever other thing with this way.
你避免了进入点或其他任何东西。
#3
3
Threads a little old. But there another method not listed so far.
线程有点旧。但目前还没有列出另一种方法。
Have your stack running then fire off a one shot docker-compose run command. E.g
让您的堆栈运行,然后触发一个单镜头docker- composer运行命令。如
#assume djsango in container named web
docker-compose run web python3 manage.py migrate
https://docs.docker.com/compose/reference/run/
https://docs.docker.com/compose/reference/run/
#1
40
You just have to log into your running docker container and run your commands.
您只需登录到正在运行的docker容器并运行您的命令。
- Build your stack :
docker-compose build -f path/to/docker-compose.yml
- 构建您的堆栈:docker- composer构建的-f路径/to/docker-com .yml
- Launch your stack :
docker-compose up -f path/to/docker-compose.yml
- 启动堆栈:docker- composition up -f path/to/docker-com .yml
- Display docker running containers :
docker ps
- 显示docker正在运行的容器:docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3fcc49196a84 ex_nginx "nginx -g 'daemon off" 3 days ago Up 32 seconds 0.0.0.0:80->80/tcp, 443/tcp ex_nginx_1
66175bfd6ae6 ex_webapp "/docker-entrypoint.s" 3 days ago Up 32 seconds 0.0.0.0:32768->8000/tcp ex_webapp_1
# postgres docker container ...
- Get the CONTAINER ID of you django app and log into :
- 获取您django app的货柜ID并登录:
docker exec -t -i 66175bfd6ae6 bash
-
Now you are logged into, then go to the right folder :
cd path/to/django_app
现在您已登录,然后转到正确的文件夹:cd路径/to/django_app
-
And now, each time you edit your models, run in your container :
python manage.py makemigrations
andpython manage.py migrate
现在,每次编辑模型时,都在容器中运行:python管理。py移民和python管理。py迁移
I also recommend you to use a docker-entrypoint for your django docker container file to run automatically :
我还建议您使用docker-entrypoint作为django docker容器文件的自动运行:
- collecstatic
- collecstatic
- migrate
- 迁移
- runserver or start it with gunicorn or uWSGI
- 运行服务器或者从gunicorn或uWSGI开始
Here is an example (docker-entrypoint.sh
) :
这里有一个例子(docker-entrypoint.sh):
#!/bin/bash
# Collect static files
echo "Collect static files"
python manage.py collectstatic --noinput
# Apply database migrations
echo "Apply database migrations"
python manage.py migrate
# Start server
echo "Starting server"
python manage.py runserver 0.0.0.0:8000
#2
20
I use these method:
我用这些方法:
services:
web:
build: .
image: uzman
command: python manage.py runserver 0.0.0.0:8000
ports:
- "3000:3000"
- "8000:8000"
volumes:
- .:/code
depends_on:
- migration
- db
migration:
image: uzman
command: python manage.py migrate --noinput
volumes:
- .:/code
depends_on:
- db
Using docker
hierarchy we made, the service migration runs after set up the database and before to run the main service. Now when you run your service docker
will run migrations before runs the server; look that migration
server is applied over the same image that web server, it means that all migrations will be taken from your project, avoiding problems.
使用我们创建的docker层次结构,服务迁移在设置数据库之后和运行主服务之前运行。现在,当您运行服务docker时,将在运行服务器之前运行迁移;请注意,迁移服务器应用于与web服务器相同的映像,这意味着所有迁移都将从项目中获取,从而避免出现问题。
You avoid made entry point or whatever other thing with this way.
你避免了进入点或其他任何东西。
#3
3
Threads a little old. But there another method not listed so far.
线程有点旧。但目前还没有列出另一种方法。
Have your stack running then fire off a one shot docker-compose run command. E.g
让您的堆栈运行,然后触发一个单镜头docker- composer运行命令。如
#assume djsango in container named web
docker-compose run web python3 manage.py migrate
https://docs.docker.com/compose/reference/run/
https://docs.docker.com/compose/reference/run/