如何从docker-compose连接到Host PostgreSQL?

Max*_*Max 1 postgresql networking localhost docker docker-compose

我有一台安装了PostgreSQL的服务器.我的所有服务都在容器中工作(docker-compose).我想从容器中使用我的Host PostgreSQL.买我有错误:

  Unable to obtain Jdbc connection from DataSource (jdbc:postgresql://localhost:5432/shop-bd) for user 'shop-bd-user': Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
  ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  SQL State  : 08001
  Error Code : 0
  Message    : Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

  Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
  Connection refused (Connection refused)
Run Code Online (Sandbox Code Playgroud)

我的docker-compose正在使用主机network_mode,如下所示:

version: '3'
services:
  shop:  
    container_name: shop
    build: ./shop
    hostname: shop
    restart: always   
    ports:
      - 8084:8084
    network_mode: "host"
Run Code Online (Sandbox Code Playgroud)

我的数据库连接UR L是:jdbc:postgresql:// localhost:5432/shop-bd

Sat*_*lai 10

更新了Apri,2018年

根据该文档,从版本18.03起,使用host.docker.internalDNS作为我的工作,没有指定任何--net=hostnetwork_mode: "host"

  • `这是出于开发目的,不适用于Mac的Docker Desktop以外的生产环境。 (2认同)

Tar*_*ani 6

如果您使用的是 linux 操作系统,这会起作用。但这不适用于 Mac 或 Windows。问题是,当您使用 时--net=host,您仍然不在主机网络上。这是 docker for windows 工作方式的限制

您需要将其docker.for.win.localhost用作主机而不是 localhost 。这是一个特殊的 DNS 名称,当您想引用主机 localhost 时,它可以在 Docker for Windows 上使用