试图将数据库与Postgress连接时,如何解决错误?
我正在尝试使用Windows 10与WSL上的Docker连接邮政数据库。 但是,当我尝试连接以下错误时出现:getAddrinfo enotfound数据库。 我该如何解决这个问题???
Docker-Compose
version: "3.7"
services:
database:
image: postgres
container_name: localhost
restart: always
ports:
- 5432:5432
environment:
- POSTGRES_USER=docker
- POSTGRES_PASSWORD=ignite
- POSTGRES_DB=rentx
volumes:
- pgdata:/data/postgres
network_mode: host
app:
build: .
container_name: rentx
ports:
- 3333:3333
volumes:
- .:/usr/app
network_mode: host
volumes:
pgdata:
driver: local
{
"type":"postgres",
"port": 5432,
"host":"localhost",
"username":"docker",
"password":"ignite",
"database":"rentx"
}
I'm trying to connect a postgress database using docker on windows 10 with wsl.
But when I try to connect the following error appears: getaddrinfo ENOTFOUND database.
How can I solve this problem???
docker-compose
version: "3.7"
services:
database:
image: postgres
container_name: localhost
restart: always
ports:
- 5432:5432
environment:
- POSTGRES_USER=docker
- POSTGRES_PASSWORD=ignite
- POSTGRES_DB=rentx
volumes:
- pgdata:/data/postgres
network_mode: host
app:
build: .
container_name: rentx
ports:
- 3333:3333
volumes:
- .:/usr/app
network_mode: host
volumes:
pgdata:
driver: local
{
"type":"postgres",
"port": 5432,
"host":"localhost",
"username":"docker",
"password":"ignite",
"database":"rentx"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您显示的错误消息建议您正在尝试连接到主机名
数据库
。这可能是正确的,但与您显示的配置不一致。您的撰写设置指定
network_mode:host
。您几乎不需要此设置。它禁用Docker的网络层,包括正常的跨载体通信。从两个容器中删除此行,然后主机名数据库
将从应用程序容器中解析。在您显示的数据库配置中,您需要
“ host”:“ database”
,与docker-compose.yml
file匹配数据库的服务块名称。您也通常不需要设置
container_name:
。特别是设置container_name:localhost
可能会带来一些令人困惑的后果,因为主机名localhost
通常定义为特殊的IPv4地址127.0.0.1.1容器。我还将删除container_name:
设置。The error message you show suggests you're trying to connect to a hostname
database
. That is probably correct, but inconsistent with the configuration you show.Your Compose setup specifies
network_mode: host
. You should almost never need this setting. It disables Docker's network layer, including normal inter-container communication. Delete this line from both containers, and then the host namedatabase
will resolve from the application container.In the database configuration you show, correspondingly, you need
"host": "database"
, matching the database's service block name in thedocker-compose.yml
file.You also do not normally need to set
container_name:
. In particular settingcontainer_name: localhost
could have some confusing consequences, since the host namelocalhost
is usually defined as the special IPv4 address 127.0.0.1 which in Docker usually resolves to the current container. I'd also delete thecontainer_name:
settings.