在dockerized项目上运行Django迁移 [英] Running Django migrations on dockerized project
问题描述
我有一个 Django
项目在多个 Docker
容器中运行,并借助 docker-撰写
。源代码是从本地计算机上的目录附加的。这是撰写的配置文件:
I have a Django
project running in multiple Docker
containers with help of docker-compose
. The source code is attached from directory on my local machine. Here's the compose configuration file:
version: '3'
services:
db:
image: 'postgres'
ports:
- '5432:5432'
core:
build:
context: .
dockerfile: Dockerfile
command: python3 manage.py runserver 0.0.0.0:8000
ports:
- '8001:8000'
volumes:
- .:/code
depends_on:
- db
尽管应用程序启动为应该,我不能运行迁移,因为每次执行 manage.py makemigrations ...
我都会收到:
Although the application starts as it should, I can't run migrations, because every time I do manage.py makemigrations ...
I receive:
django.db.utils.OperationalError: could not translate host name "db" to address: nodename nor servname provided, or not known
很明显,我可以在核心$ c $内打开
bash
c>容器并从那里运行 makemigrations
,但是随后在容器内部创建了迁移文件,这非常不舒服。
Obviously I can open bash
inside the core
container and run makemigrations
from there, but then the migration files are created inside the container which is very uncomfortable.
在我的项目设置中,数据库配置为:
In my project's settings the database is configured as:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': '5432',
}
}
由于docker postgres映像可在 localhost:5432
我尝试将数据库主机的设置更改为:
As docker postgres image is accessible at localhost:5432
I tried changing database host in settings to:
'HOST': '0.0.0.0'
但是然后用 docker-compose up
启动容器时接收:
But then when firing up the containers with docker-compose up
I'm receiving:
...
django.db.utils.OperationalError: could not connect to server:
Connection refused
Is the server running on host "0.0.0.0" and accepting
TCP/IP connections on port 5432?
...
如何在设置中配置数据库。 py
以便 Django
可以访问它来创建迁移?
How should I configure database in settings.py
so that Django
can access it to create migrations?
推荐答案
您的docker-compose配置不正确。您忘记了链接服务
Your docker-compose configurations are not correct. You forgot to link services
version: '3'
services:
db:
image: 'postgres'
ports:
- '5432:5432'
core:
build:
context: .
dockerfile: Dockerfile
command: python3 manage.py runserver 0.0.0.0:8000
ports:
- '8001:8000'
volumes:
- .:/code
depends_on:
- db
links: # <- here
- db
这篇关于在dockerized项目上运行Django迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!