I deploy my Spring Boot app and when I set some variables on .env, docker-compose and application.yml, I am confused. So, could you please clarify me about the following points?
Assume that I have the following files:
.env:
DB_NAME=employee_db
DB_USERNAME=postgres
DB_PASSWORD=pass
JWT_SECRET=mySecretKey
docker-compose:
version: '3.8'
services:
db:
container_name: employee-db
image: postgres:14.6-alpine
env_file: ./.env
environment:
POSTGRES_DB: ${DB_NAME}
POSTGRES_USER: ${DB_USERNAME}
POSTGRES_PASSWORD: ${DB_PASSWORD}
applicatipn.yml:
spring:
... code omitted for brevity
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/${DB_NAME}
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
app:
security:
jwtSecret: ${JWT_SECRET}
After reading the related documentation;
1. As far as I know, .env
file is read by docker-compose
files and the database is created by using the credentials in the .env
file. Right?
2. On the other hand, application.yml
file is used when application is running. If we do not pass environment variables while running the app, these values e.g. DB_USERNAME
is read from the .env
file, is that true?
Just want to be clarified by a simple scenario after reading the related documentation.
2
Answers
docker-compose
reads.env
and use it inenvironment
section as in your docker-compose example.If you need use it in app container with
applicatipn.yml
or in another place inside container, you should useenvironment
as for database to pass variables into container, as exampleor mounting
.env
file into container and read it by you app.Spring reads properties from many places but it does not read a
.env
file. It would read anapplication.properties
orapplication.yml
in the current directory; the Java properties file syntax will be slightly different from the.env
syntax. Based on what you’ve shown I’d expect your Spring application to have some startup trouble resolving thespring.datasource.url
and other configuration properties.For Spring, one of those options is to set properties from environment variables, with specific rules on how the names are mapped. In a Spring context I’d tend to use the environment variable names that match what Spring expects; for example
You do not need to set placeholder values in the
application.yml
bundled into your jar file.In a Compose context, one tricky thing is that there are two different layers of environment variables. Compose reads the ordinary shell environment and merges in the
.env
file, and these values are applied when resolving variable references in the Compose file. Each container has its own separate environment, which is specified by per-containerenvironment:
andenv_file:
settings. A value is not passed from the host into a container unless you’ve set one of these.Let’s say you’re running both parts in Compose, with the
.env
file you show. Those specific variable names aren’t meaningful to either Compose or Spring, so you don’t need to include aenv_file: .env
line. But you can use those variables in defining the names they do understand.