试图将数据库与Postgress连接时,如何解决错误?

发布于 2025-02-13 20:11:40 字数 792 浏览 1 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

惯饮孤独 2025-02-20 20:11:42

您显示的错误消息建议您正在尝试连接到主机名数据库。这可能是正确的,但与您显示的配置不一致。

您的撰写设置指定network_mode:host。您几乎不需要此设置。它禁用Docker的网络层,包括正常的跨载体通信。从两个容器中删除此行,然后主机名数据库将从应用程序容器中解析。

在您显示的数据库配置中,您需要“ host”:“ database”,与docker-compose.yml file匹配数据库的服务块名称。

您也通常不需要设置container_name:。特别是设置container_name:localhost可能会带来一些令人困惑的后果,因为主机名localhost通常定义为特殊的IPv4地址127.0.0.1.1容器。我还将删除container_name:设置。

version: '3.8'
services:
  database:
    image: postgres
    restart: always
    ports: # optional unless you're connecting from outside Docker
      - 5432:5432
    environment: { unmodified: from original question }
    volumes:
      - pgdata:/var/lib/postgresql/data # <-- note corrected filesystem path
    # no network_mode: or container_name:
  app:
    build: .
    ports:
      - 3333:3333
    # no network_mode:, container_name:, or volumes:
volumes:
  pgdata: # empty

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 name database 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 the docker-compose.yml file.

You also do not normally need to set container_name:. In particular setting container_name: localhost could have some confusing consequences, since the host name localhost 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 the container_name: settings.

version: '3.8'
services:
  database:
    image: postgres
    restart: always
    ports: # optional unless you're connecting from outside Docker
      - 5432:5432
    environment: { unmodified: from original question }
    volumes:
      - pgdata:/var/lib/postgresql/data # <-- note corrected filesystem path
    # no network_mode: or container_name:
  app:
    build: .
    ports:
      - 3333:3333
    # no network_mode:, container_name:, or volumes:
volumes:
  pgdata: # empty
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文