The Dockerfile of my spring-boot app:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
COPY target/media-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]
application.yml
spring:
datasource:
url: jdbc:postgresql://localhost:5432/media
username: postgres
password: postgres
hikari:
connectionTimeout: 30000
and here is the docker-compose.yml
:
version: '3'
services:
db:
image: postgres
ports:
- "5432:5432"
environment:
POSTGRES_DB: media
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
app:
build:
context: ./
dockerfile: Dockerfile
depends_on:
- db
ports:
- "8080:8080"
Running docker-compose up --build
results in:
app_1 | org.postgresql.util.PSQLException: Connection to 0.0.0.0:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. app_1
My guess is that the spring app tries to connect to postgres before postgres is ready, but I get the following log:
db_1 | 2019-05-18 19:05:53.692 UTC [1] LOG: database system is ready to accept connections
0.0.0.0
would work (andlocalhost
definitely won't work, since the database is not in the same container as your app). In Docker Compose, other services are made available via virtual DNS, so you should be able to usedb
instead. – halferjdbc:postgresql://db:5432/media
. – halferspring.datasource.url
tojdbc:postgresql://db:5432/media
, as you suggested, made it work. Thanks. Feel free to make your comment into an answer, so I can accept it. – senjin.hajrulahovic