Docker在django web应用+ postgres数据库上编写运行迁移

您好,我有问题在Django的postgres数据库容器上运行迁移。

这是我的docker-compose文件

web: restart: always build: ./web expose: - "8000" links: - postgres:postgres volumes: - /usr/src/app - /usr/src/app/static env_file: - ./.env environment: - DEBUG=1 command: /usr/local/bin/gunicorn mysite.wsgi:application -w 2 -b :8000 nginx: restart: always build: ./nginx/ ports: - "80:80" volumes: - /www/static volumes_from: - web links: - web:web postgres: restart: always build: ./postgres env_file: .env ports: - "5432:5432" volumes: - pgdata:/var/lib/postgresql/data/ 

目录结构如下。 在这里输入图像说明

.env文件定义了Postgres DB,用户名和密码

 DB_NAME=test DB_USER=test DB_PASS=test! DB_SERVICE=postgres DB_PORT=5432 POSTGRES_USER=test POSTGRES_DB=test POSTGRES_PASSWORD=test! 

当我运行docker-compose builddocker-compose up -d nginx时,postgres和web容器启动。 postgres启动(默认)创build数据库,用户和密码。 django启动容器安装requirements.txt并启动django服务器(一切看起来不错)。

在运行makemigrations

docker-compose run web /usr/local/bin/python manage.py makemigrations polls我得到以下输出

Migrations for 'polls': 0001_initial.py: - Create model Choice - Create model Question - Add field question to choice

但是当我跑步

docker-compose run web /usr/local/bin/python manage.py showmigrations polls查询输出结果。

polls (no migrations)

运行docker-compose run web /usr/local/bin/python manage.py migrate --fake polls我看到的输出

Operations to perform: Apply all migrations: (none) Running migrations: No migrations to apply. Your models have changes that are not yet reflected in a migration, and so won't be applied. Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.

这些表格不是在postgres中创build的。 我究竟做错了什么 ? 对不起,这个post很长,但是我想把这里的所有细节都放在这里。