skip to Main Content

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


  1. docker-compose reads .env and use it in environment 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 use environment as for database to pass variables into container, as example

    environment:
      DB_USERNAME: ${DB_USERNAME}
    

    or mounting .env file into container and read it by you app.

    Login or Signup to reply.
  2. Spring reads properties from many places but it does not read a .env file. It would read an application.properties or application.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 the spring.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

    export SPRING_DATASOURCE_URL=jdbc:postgresql://localhost:5432/employee_db
    export SPRING_DATASOURCE_USERNAME=postgres
    export SPRING_DATASOURCE_PASSWORD=pass
    ./gradlew bootRun
    

    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-container environment: and env_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 a env_file: .env line. But you can use those variables in defining the names they do understand.

    version: '3.8'
    services:
      db:
        image: postgres:14.6-alpine
        environment:
          POSTGRES_DB: ${DB_NAME}
          POSTGRES_USER: ${DB_USERNAME}
          POSTGRES_PASSWORD: ${DB_PASSWORD}
        # as you have it, but no `env_file:` (or `container_name:`)
      app:
        build: .
        environment:
          SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/${DB_NAME}
          SPRING_DATASOURCE_USERNAME: ${DB_USERNAME}
          SPRING_DATASOURCE_PASSWORD: ${DB_PASSWORD}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search