连接 Docker 镜像至本地的 Postgres 数据库。

huangapple go评论91阅读模式
英文:

Connect Docker image to local Postgres

问题

以下是翻译好的内容:

我有一个连接到本地Postgres数据库的Spring应用程序。它运行正常。

然后我从Spring应用程序创建了一个Docker镜像。在运行容器时,它抛出了一个数据库连接错误。

我已经按照这里提供的解决方案进行了操作,但仍然得到相同的错误。

错误信息:

[ERROR] [task-1] c.z.h.p.HikariPool c.z.h.p.HikariPool.throwPoolInitializationException(HikariPool.java:593) – HikariPool-1 - Exception during pool initialization.
org.postgresql.util.PSQLException: 拒绝连接到 localhost:5432。请检查主机名和端口是否正确,并且 postmaster 是否接受 TCP/IP 连接。
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:285)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:217)
    at org.postgresql.Driver.makeConnection(Driver.java:458)
    at org.postgresql.Driver.connect(Driver.java:260)

我以多种方式运行了Docker镜像,但结果相同。

docker run app-image
docker run --network=host app-image

Dockerfile:

FROM java:8-jdk-alpine

COPY ./build/libs/runtime-0.0.1-SNAPSHOT.jar /usr/app/
WORKDIR /usr/app

ENTRYPOINT ["java", "-jar", "runtime-0.0.1-SNAPSHOT.jar"]
# 暴露标准的Tomcat端口
EXPOSE 9888

postgresql.conf:

#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

# - Connection Settings -

listen_addresses = '*'

pg_hba.conf:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

host        postgres        postgres     0.0.0.0/0              trust
host        postgres        postgres     172.17.0.0/16          trust
英文:

I have a Spring app that connects to local Postgres db. It works fine.

I then created a Docker image from the Spring app. When running the container it throws a db connection error.

I have followed the solution suggested here, but still getting the same error.

[ERROR ] [task-1] c.z.h.p.HikariPool c.z.h.p.HikariPool.throwPoolInitializationException(HikariPool.java:593) – HikariPool-1 - Exception during pool initialization.
org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
	at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:285)
	at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
	at org.postgresql.jdbc.PgConnection.&lt;init&gt;(PgConnection.java:217)
	at org.postgresql.Driver.makeConnection(Driver.java:458)
	at org.postgresql.Driver.connect(Driver.java:260)

I have run the docker image in several ways with the same result.

docker run app-image

docker run --network=host app-image

Dockerfile:

FROM java:8-jdk-alpine

COPY ./build/libs/runtime-0.0.1-SNAPSHOT.jar /usr/app/
WORKDIR /usr/app

ENTRYPOINT [&quot;java&quot;, &quot;-jar&quot;, &quot;runtime-0.0.1-SNAPSHOT.jar&quot;]
# Expose standard tomcat port

EXPOSE 9888

postgresql.conf:

#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

# - Connection Settings -

listen_addresses = &#39;*&#39;

pg_hba.conf

# TYPE  DATABASE        USER            ADDRESS                 METHOD

host        postgres        postgres     0.0.0.0/0              trust
host        postgres        postgres     172.17.0.0/16          trust

答案1

得分: 1

The spring application tries to connect to localhost:5432.Since the host and each container are separate network entities, the container tries to connect to the database running on it.Since there is no database running, the connection cannot be established.

To fix this issue, you need to get the IP address of the host machine within the docker-internal network and use this address as the database server for the spring application.

I would recommend running the postgres in a docker container as well. You can use the official postgres image from Docker Hub. Then you can reference the postgres container from within the spring container by the postgres container's name.

英文:

The spring application tries to connect to localhost:5432. Since the host and each container are separate network entities (i.e., the host and each container has its own network address in the docker-internal network), the container tries to connect to the database running on it. Since there is not database running, the connection cannot be established.

To fix this issue, you need to get the IP address of the host machine within the docker-internal network (see, e.g., this article at devilbox) and use this address as database server for the spring application.


I would recommend to run the postgres in a docker container aswell. You can, for example, use the official postgres image from dockerhub. Then you can reference the postgres-container from within the spring-container by the postgres-container's name.

huangapple
  • 本文由 发表于 2020年8月14日 19:36:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63412022.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定