Skip to content

spring-boot-docker-compose java.lang.IllegalStateException: No POSTGRES_PASSWORD defined #37188

@derylspielman

Description

@derylspielman

Hi I'm using spring-boot-docker-compose to have a postgres database start when the app starts but getting error at startup

java.lang.IllegalStateException: No POSTGRES_PASSWORD defined

This is due to PostgresEnvironment extracting environment variables but assuming there is a POSTGRES_PASSWORD environment variable.

I am using the supported POSTGRES_PASSWORD_FILE environment variable instead, which is how you can keep secrets out of version control.

Steps to reproduce

  1. Configure pom.xml
...
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-docker-compose</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
...
  1. Add docker-compose.yml in the root project folder
version: '3.8'

services:
  db:
    image: postgres:14.5
    restart: always
    environment:
      POSTGRES_DB: mydb
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD_FILE: /run/secrets/db_password
    ports:
      - 5432:5432
    secrets:
      - db_password
    volumes:
      - postgres-data:/var/lib/postgresql/data
volumes:
  postgres-data:

secrets:
  db_password:
    file: src/main/resources/postgres_password.txt
  1. Save a secrets file
    • Save the postgres password in the Spring Boot app under src/main/resources/postgres_password.txt
  2. Run the app
  3. Expecting the app to start but instead getting java.lang.IllegalStateException: No POSTGRES_PASSWORD defined

Running docker compose up works fine and the container starts.

Spring Boot version: 3.1.1
Java version: openjdk 17.0.8.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions