我在一个用于开发的docker-compose设置中使用PostgreSQL运行Django。每次重新启动应用程序容器时,数据库都是空的,即使我既不重新启动DBMS容器,也不删除DBMS的数据卷。看起来Django在重启时删除了所有的表。为什么?
我的设置紧跟here的描述。也就是说,我的撰写文件如下所示(简化):
version: '3.8'
services:
db:
image: postgres
environment:
- POSTGRES_DB=db_dev
- POSTGRES_USER=dev
- POSTGRES_PASSWORD=postgres
volumes:
- type: volume
source: app-data
target: /var/lib/postgresql/data
app:
build: .
command: python manage.py runserver 0.0.0.0:8888
container_name: app
environment:
- DATABASE_URL
- PYTHONDONTWRITEBYTECODE=1
- PYTHONUNBUFFERED=1
volumes:
# Mount the local source code folder for quick iterations.
# See: https://www.docker.com/blog/containerized-python-development-part-3/
- type: bind
source: .
target: /code
ports:
- target: 8888
published: 8888
depends_on:
- db
env_file:
- ./dev.env
volumes:
app-data:
external: trueDjango应用程序通过entrypoint.sh启动
#! /bin/sh
if [ "$DATABASE" = "postgresql" ]
then
echo "Waiting for postgres..."
while ! nc -z $SQL_HOST $SQL_PORT; do
sleep 0.1
done
echo "PostgreSQL started"
fi
doMigrate=${DB_MIGRATE:-false}
if [ "$doMigrate" = true ] ;
then
python manage.py flush --no-input
python manage.py migrate
fi
exec "$@"在开发设置中,我设置了DB_MIGRATE=true和DEBUG=1。
发布于 2020-10-09 15:27:52
Django flush命令从数据库中删除所有数据,如documentation中所述。
因此,要解决上面的问题,我只需要删除行
python manage.py flush --no-input从我的entrypoint.sh脚本。
说明:我错误地认为flush将提交任何挂起的事务,这些事务可能会从其他可能使用该数据库的应用程序中打开。事实并非如此。相反,flush只是删除所有数据。
https://stackoverflow.com/questions/64263583
复制相似问题