【问题标题】:Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections连接到 localhost:5432 被拒绝。检查主机名和端口是否正确以及 postmaster 是否接受 TCP/IP 连接
【发布时间】:2019-05-30 07:53:53
【问题描述】:

这里是我的 DockerFile:-

FROM openjdk:10
ENV AQUILA_HOME /data/config
#USER root
#VOLUME /tmp
ADD a2i-web-1.0.0-SNAPSHOT.jar app.jar
#RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-jar","app.jar"]

我的 jar 是需要 postgres 的 spring boot 应用程序。 我在本地安装了 portgres,它在我的本地主机上成功运行。 我正在通过命令成功构建 DockerFile

sudo docker build -t djtijare/a2i-web:v1 .

但是在通过命令运行时

sudo docker run -p 8080:8080 -t djtijare/a2i-web:v1

给出例外:

Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

我正在从包含 DockerFile 和我的 jar 的目录中运行这个 docker 命令 我是否需要设置任何配置才能运行 postgres?

【问题讨论】:

标签: docker dockerfile


【解决方案1】:

你不能从容器中使用localhost访问主机上的服务,你必须使用你主机的ip地址才能访问。

这是因为:默认 docker 会使用桥接器,它会为你的容器设置一个内部网络,所以当容器使用 localhost 时,它并不意味着主机,它意味着容器自身的网络。

如果坚持,一个丑陋的解决方案是使用--net=host

类似下一个:

sudo docker run --net=host -p 8080:8080 -t djtijare/a2i-web:v1

那么你就可以使用localhost从容器中访问主机的服务了。

【讨论】:

  • 这很有帮助,因为它解决了我的问题,但正如你所说,这确实是一个丑陋的解决方案。
  • 使用IP访问它的其他方式是什么
  • 如我所说,需要获取主机的ip,直接使用主机ip访问postgres。当您不使用容器时,如果 postgres 不在您的应用程序的同一台机器上,您也可以使用这种方式。
  • 没有k8s经验,只知道不使用docker bridge驱动,定制一个可以跨机器的网络
猜你喜欢
  • 1970-01-01
  • 2020-08-19
  • 2020-08-09
  • 1970-01-01
  • 2017-01-01
  • 2020-12-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多