I would like to build a native image for an app which using R2DBC to access PostgreSQL. Not sure why but with the following docker compose file the SPRING_R2DBC_URL is not recognised, I receive:
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a ConnectionFactory: 'url' attribute is not specified and no embedded database could be configured.
Reason: Failed to determine a suitable R2DBC Connection URL
services:
app:
image: 'docker.io/library/postgresql-r2dbc:0.0.1-SNAPSHOT'
depends_on:
db:
condition: service_healthy
# Not recognised configuration
# environment:
# - 'SPRING_R2DBC_URL=r2dbc:postgresql://db:5432/postgres'
# - 'SPRING_R2DBC_USERNAME=postgres'
# - 'SPRING_R2DBC_PASSWORD=secret'
ports:
- '8080:8080'
db:
image: 'postgres:16'
ports:
- '5432:5432'
environment:
- 'POSTGRES_DB=postgres'
- 'POSTGRES_PASSWORD=secret'
- 'POSTGRES_USER=postgres'
healthcheck:
test: [ "CMD-SHELL", "pg_isready" ]
interval: 2s
timeout: 2s
retries: 3
If I bake the configuration into the image then the connection to PostgreSQL works
spring:
application:
name: postgresql-r2dbc
sql.init.mode: always
# If we bake the configuration into the image then the connection to PostgreSQL works
r2dbc:
url: r2dbc:postgresql://db:5432/postgres
username: postgres
password: secret
Interestingly with JDBC the external configuration works, for R2DBC do I need any specific configuration ?
I build the image using:
$ mvn clean install
$ mvn -Pnative spring-boot:build-image
I created an example here: https://github.com/altfatterz/learning-spring/tree/master/postgresql-r2dbc
Any feedback appreciated.
Thank you.
The following did the trick in the application.yml
spring:
r2dbc:
url: tbd
username: tbd
password: tbd
and then in the Docker file re-define:
services:
app:
image: 'docker.io/library/postgresql-r2dbc:0.0.1-SNAPSHOT'
depends_on:
db:
condition: service_healthy
environment:
- 'SPRING_R2DBC_URL=r2dbc:postgresql://db:5432/postgres'
- 'SPRING_R2DBC_USERNAME=postgres'
- 'SPRING_R2DBC_PASSWORD=secret'