我目前正在尝试在Alpine / PostgreSQL容器中构建一个Spring Boot应用程序,该容器将在以后的阶段中通过Kafka消息与其他Docker容器进行通信。
最近几天寻找解决方案后,我在这里发表问题。我当前的问题是,当我尝试运行它时,我的容器失败了-我目前收到以下错误消息:
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.
[...]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
[...]
org.springframework.jdbc.support.MetaDataAccessException: Could not get Connection for extracting meta-data; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is 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.
[...]
Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is 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.
[...]
Caused by: 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.
[...]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
Spring Boot应用程序似乎在数据库之前启动,因为在没有ENTRYPOINT命令的情况下运行映像并手动启动.jar文件是可以的。为了解决这个问题,我尝试了几种解决方案,例如
#!/bin/bash
sleep 3
sudo -u postgres postgres -D /var/lib/postgresql/data
sleep 3
java -jar persistence.jar
尝试第三个解决方案时出现以下异常:
postgres cannot access the server configuration file "/var/lib/postgresql/data/postgresql.conf": No such file or directory
最后,所有解决方案都无效。
设置
这是我的dockerfile:
FROM postgres:9.3-alpine
ADD target/persistence-0.0.1.jar persistence.jar
ADD init-container.sh init-container.sh
ENV POSTGRES_DB test
ENV POSTGRES_USER test
ENV POSTGRES_PASSWORD password
RUN apk add --no-cache bash
RUN apk add sudo
RUN apk add openjdk8
ENTRYPOINT ["bash", "init-container.sh"]
这里是构建和运行命令:
docker build -t persistence .
docker run --name persistence -p 2002:2002 -d persistence
如果有人知道合适的解决方案,我将不胜感激!
谢谢。