Video in progress
How do you pass settings to a Spring Boot application through environment variables?
Spring Boot reads env vars automatically: an uppercase name with underscores such as SPRING_DATASOURCE_URL maps to the spring.datasource.url property. An env var overrides the value in application.yml.
📖 Answer
The rule is simple: replace dots with _, uppercase everything, and drop the dashes.
| application.yml | env var |
|---|---|
spring.datasource.url |
SPRING_DATASOURCE_URL |
server.port |
SERVER_PORT |
app.payment.api-key |
APP_PAYMENT_APIKEY |
docker run -e SPRING_PROFILES_ACTIVE=prod \
-e SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/shop \
-e SPRING_DATASOURCE_PASSWORD=secret \
shop-api:0.1
Why this is good: one image is identical in dev, staging and prod, and only the environment variables differ. If you bake environment-specific values into the image, you need a separate build for each environment, and that is a source of mistakes.
Sign up to mark studied questions and save your progress.