簡體   English   中英

如何從 docker-compose 連接到 Host PostgreSQL?

[英]How to connect from docker-compose to Host PostgreSQL?

我有一台安裝了 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)

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

version: '3'
services:
  shop:  
    container_name: shop
    build: ./shop
    hostname: shop
    restart: always   
    ports:
      - 8084:8084
    network_mode: "host"

我的數據庫連接UR L是: jdbc:postgresql://localhost:5432/shop-bd

更新於2018年4月

根據該文檔 ,從版本18.03開始,使用host.docker.internal作為DNS對我有效,而無需同時指定--net=hostnetwork_mode: "host"

如果您使用的是Linux操作系統,則該方法會起作用。 但這在Mac或Windows上不起作用。 問題是,當您使用--net=host ,您仍不在主機網絡上。 那是Windows的docker工作方式的局限性

您需要使用docker.for.win.localhost而不是localhost作為主機。 這是一個特殊的DNS名稱,當您要引用主機localhost時,在Windows的Docker上可用

警告說明:使用--net = host或network_mode:“ host”將阻止您的容器向Eureka注冊,並且除非所有容器都在同一網絡上運行,否則它將破壞您擁有的任何API網關。

如果您是 mac 用戶,請使用“host.docker.internal:5432”連接到 localhost Postgres, docs

1)確保您的postgresql實例正在偵聽所有地址,而不僅是本地主機。

在postgresql.conf中,如下設置listen_addresses

listen_addresses='*'

2)您正在嘗試從容器連接到localhost:5432 那不是您主機的IP地址,而是您容器的loopback device的地址。 您需要改用docker bridge IP地址。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM