0

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.

  1. Playing around with the ENTRYPOINT and RUN commands inside the dockerfile
  2. Writing a wait script (as described here)
  3. 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.

Marek W
  • 699
  • 4
  • 14
  • 2
    Possible duplicate of [Docker multiple entrypoints](https://stackoverflow.com/questions/18805073/docker-multiple-entrypoints) That should answer all your questions! – Robert Moskal Dec 03 '18 at 20:47
  • 1
    The right answer is to run your application (which will probably get restarted frequently) and your database (which won’t) in separate containers. Do _not_ use `localhost` unless you’re absolutely clear on what it means. This is also hinted in the linked question. – David Maze Dec 03 '18 at 21:29
  • Thank you guys, I will check that post out! – Marek W Dec 04 '18 at 19:13
  • For testing and test automation purpose it can make sense to have both (db and app) in one container, e.g. if you want to start/restart the whole test-environment before each single test. @DavidMaze – Wlad Oct 30 '19 at 12:42

0 Answers0