如何从docker-compose连接到Host PostgreSQL? [英] How to connect from docker-compose to Host PostgreSQL?
本文介绍了如何从docker-compose连接到Host PostgreSQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一台装有PostgreSQL的服务器.我所有的服务都在容器中工作(docker-compose).我想从容器中使用我的Host 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"
我的数据库连接UR L是: jdbc:postgresql://localhost:5432/shop-bd
推荐答案
2018年4月更新
根据文档,从版本18.03开始,使用host.docker.internal
作为DNS对我有效,而无需指定--net=host
或network_mode: "host"
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屋!
查看全文