1

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?

2
  • From the point of view of the app container, the special host name localhost points at the app container, not the containing host or a different container. Try setting a SPRING_DATASOURCE_URL environment variable to use the Compose service name of the database db as the database host name instead. There are a couple of examples of this in the linked question. Commented Jan 29, 2022 at 1:03
  • @David Maze Thank you, it works! The linked question was also very useful. Commented Jan 29, 2022 at 1:43

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.