如何从 docker-compose 连接到 Host PostgreSQL? [英] How to connect from docker-compose to Host PostgreSQL?

查看:29
本文介绍了如何从 docker-compose 连接到 Host PostgreSQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a server with installed PostgreSQL. All my services work in containers (docker-compose). I want to use my Host PostgreSQL from containers. Buy I have the error:

  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 正在使用 host network_mode,如下所示:

My docker-compose is using host network_mode, like this:

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

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

推荐答案

2018 年 4 月更新

Updated Apri, 2018

根据文档,从 18.03 版开始,使用 host.docker.internal 作为 DNS 对我有用,没有指定 --net=hostnetwork_mode:主机"

According to the document, from version 18.03 onwards, using host.docker.internal as DNS works for me, without specifying neither --net=host or network_mode: "host"

这篇关于如何从 docker-compose 连接到 Host PostgreSQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆