I am currently trying to build a Spring Boot application inside a Alpine/PostgreSQL container, which will (in later stages) communicate with other docker containers via Kafka messages.
After looking for solutions the last few days I am posting my question here. My current problem is, that my container fails when I am trying to run it - I am currently getting the following error message:
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)
So it seems like, that the Spring Boot application is starting before the database, because running the image without an ENTRYPOINT command and starting the .jar file by hand works fine. In order to fix this, I've tried several solutions, e.g.
- Playing around with the ENTRYPOINT and RUN commands inside the dockerfile
- Writing a wait script (as described here)
- Writing a custom shell script (see box below)
#!/bin/bash
sleep 3
sudo -u postgres postgres -D /var/lib/postgresql/data
sleep 3
java -jar persistence.jar
I get the following exception when trying the third solution:
postgres cannot access the server configuration file "/var/lib/postgresql/data/postgresql.conf": No such file or directory
So in the end, none of the solutions worked.
SETUP
Here is my 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"]
And here the build and run commands:
docker build -t persistence .
docker run --name persistence -p 2002:2002 -d persistence
If anyone knows a proper solution I would be very grateful!
Thank you.