I have a Spring Boot application that uses PostgreSQL. I want to deploy it using Docker.
Dockerfile
FROM openjdk:11
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]
Build image command
docker build --build-arg JAR_FILE=build/libs/\*.jar -t demo-app .
docker-compose.yml
version: '2'
services:
app:
image: 'demo-app:latest'
build:
context: .
container_name: app
depends_on:
- db
db:
image: 'postgres:13.1-alpine'
container_name: db
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
I'm getting connection refused error message. Also, I can't connect using pgadmin. So I guess that PostgreSQL isn't deployed correctly. I suspect something to do with the ports. Which I haven't configured. They are supposed to be the default. Spring Boot uses 8080 and Postgres 5432.
Also, this is my application properties file
# PostgreSQL
spring:
datasource:
initialization-mode: always
url: ${DB_URL:jdbc:postgresql://localhost:5432/postgres}
username: postgres
password: password
jpa:
show-sql: true
hibernate:
ddl-auto: create-drop
What am I missing?
appcontainer, the special host namelocalhostpoints at theappcontainer, not the containing host or a different container. Try setting aSPRING_DATASOURCE_URLenvironment variable to use the Compose service name of the databasedbas the database host name instead. There are a couple of examples of this in the linked question.