postgresql Docker-compose with django 无法将主机名“db”转换为地址:名称或服务未知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41573313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Docker-compose with django could not translate host name "db" to address: Name or service not known
提问by Matheus Campello
I have currently a system built with docker-compose, it creates a Django application.
我目前有一个用 docker-compose 构建的系统,它创建了一个 Django 应用程序。
Up until now I've used a database inside a container (postgresql) in my testing build. Now I've changed the database from this container to an RDS instance in AWS.
到目前为止,我在我的测试版本中使用了容器 (postgresql) 中的数据库。现在我已将数据库从此容器更改为 AWS 中的 RDS 实例。
Using Pg_dump I have recreated the database inside RDS and changed the settings.py, everything was supposedly normal. I have accessed the data from the database inside my webapp without any problems.
使用 Pg_dump 我在 RDS 中重新创建了数据库并更改了 settings.py,一切都应该是正常的。我已经从我的 web 应用程序中的数据库中访问了数据,没有任何问题。
Everything was ok until I had to make a migration. Without the database container the Django container gives me this message:
一切都很好,直到我不得不进行迁移。如果没有数据库容器,Django 容器会给我这条消息:
django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known
django.db.utils.OperationalError:无法将主机名“db”转换为地址:名称或服务未知
My Docker-compose.yml file before the changes:
更改前我的 Docker-compose.yml 文件:
version: '2'
services:
db:
image: postgres:9.5
restart: always
environment:
POSTGRES_USER: testing
POSTGRES_PASSWORD: tests
POSTGRES_DB: test
volumes:
- /dbdata:/var/lib/postgresql/data
django:
build: ./django
command: gunicorn contactto.wsgi:application -b 0.0.0.0:8000
restart: always
volumes:
- ./django:/usr/src/app
- ./django/static:/usr/src/app/contactto/static
ports:
- "8000:8000"
depends_on:
- db
Now after the changes:
现在更改后:
version: '2'
services:
django:
build: ./django
command: gunicorn contactto.wsgi:application -b 0.0.0.0:8000
restart: always
volumes:
- ./django:/usr/src/app
- ./django/static:/usr/src/app/contactto/static
ports:
- "8000:8000"
And the DATABASES from settings.py . Before:
以及来自 settings.py 的数据库。前:
DATABASES = {
'default': {
'ENGINE': 'tenant_schemas.postgresql_backend',
'NAME': 'testing',
'USER': 'test',
'PASSWORD': 'test',
'HOST': 'db',
'PORT': '5432',
}
}
After:
后:
DATABASES = {
'default': {
'ENGINE': 'tenant_schemas.postgresql_backend',
'NAME': 'testing',
'USER': 'test',
'PASSWORD': 'test',
'HOST': 'xxx.rds.amazonaws.com',
'PORT': '5432',
}
}
The weird thing is, I can use the aws database inside my app... I can create users and do things inside the database and the changes appear. Now in the CLI I can't even use manage.py shell without the message.
奇怪的是,我可以在我的应用程序中使用 aws 数据库......我可以创建用户并在数据库中执行操作,然后就会出现更改。现在在 CLI 中,我什至不能在没有消息的情况下使用 manage.py shell。
I am completely lost.
我完全迷失了。
回答by Matheus Campello
Answering my question, this was a stupid one...
回答我的问题,这是一个愚蠢的......
My manage.py was selecting only the base.py settings file, and was not taking into account the staging.py settings file. So it was breaking in the CLI and was not breaking inside the app.
我的 manage.py 只选择了 base.py 设置文件,并没有考虑 staging.py 设置文件。所以它在 CLI 中被破坏,而在应用程序内部没有被破坏。
回答by Octatron
In the "django" part of your docker-compose.yml under "depends_on" section try adding links: - db:db
after it, or even replace the depends_on: db with that.
在“depends_on”部分下的 docker-compose.yml 的“django”部分中,尝试links: - db:db
在其后添加,或者甚至将depends_on: db 替换为它。
I'll bet if you typed in docker logs (container name)
you'd see it's obviously having trouble working out where "db" points to.
我敢打赌,如果您输入,docker logs (container name)
您会发现它显然无法确定“db”指向的位置。
I think they're looking to end support for links in docker-compose in new iterations at least I read that somewhere..
我认为他们希望在新的迭代中结束对 docker-compose 中链接的支持,至少我在某处读到过。
回答by tech
Add network, link and depends_onconfiguration in docker compose file.
在dockercompose 文件中添加网络、链接和depends_on配置。
example:
例子:
services:
db:
build: .
container_name: db
networks:
- djangonetwork
web:
build: .
depends_on:
- db
links:
- db:db
networks:
- djangonetwork
networks:
djangonetwork:
driver: bridge
回答by Mik Goldwyn
To the others experiencing this.
对于其他经历过这种情况的人。
The following command (which removes all unused containers, networks, images, and optionally, volumes) solve my problem:
以下命令(删除所有未使用的容器、网络、图像和可选的卷)解决了我的问题:
docker system prune
See docker documentfor more information
有关更多信息,请参阅docker 文档