How do you configure environment variables in a Dockerized Spring Boot application?
Table of Contents
- Introduction
- Methods to Configure Environment Variables in Dockerized Spring Boot
- Conclusion
Introduction
Configuring environment variables in a Dockerized Spring Boot application allows you to manage dynamic settings and configurations, such as database credentials, API keys, or application-specific properties. Environment variables offer flexibility, especially when deploying the application across different environments (development, testing, production) or when configuring the app in a cloud-based or containerized setup like Docker.
In this guide, we'll show you how to configure environment variables in a Spring Boot application running inside a Docker container, enabling dynamic and environment-specific configurations.
Methods to Configure Environment Variables in Dockerized Spring Boot
1. Use Docker -e
Flag to Set Environment Variables
The simplest way to configure environment variables for your Spring Boot application inside Docker is by using the -e
flag when running the Docker container. This flag allows you to define environment variables directly in the docker run
command.
Example: Setting Environment Variables
Explanation:
-e SPRING_DATASOURCE_URL=jdbc:mysql://localhost:3306/mydb
: Sets theSPRING_DATASOURCE_URL
environment variable for the Spring Boot application.-e SPRING_DATASOURCE_USERNAME=root
: Sets theSPRING_DATASOURCE_USERNAME
variable.-e SPRING_DATASOURCE_PASSWORD=rootpassword
: Sets theSPRING_DATASOURCE_PASSWORD
variable.spring-boot-app
: Specifies the name of the Docker image.
Spring Boot automatically maps environment variables like SPRING_DATASOURCE_URL
, SPRING_DATASOURCE_USERNAME
, and SPRING_DATASOURCE_PASSWORD
to its internal configuration properties.
2. Pass Environment Variables through a Docker Compose File
If you're using Docker Compose to manage multi-container environments, you can pass environment variables using the environment
directive in the docker-compose.yml
file.
Example: Docker Compose Configuration
In this configuration:
- The
environment
section in thespring-boot-app
service defines environment variables. SPRING_DATASOURCE_URL
,SPRING_DATASOURCE_USERNAME*
, andSPRING_DATASOURCE_PASSWORD
are passed to the Spring Boot application.- The MySQL service is also configured with the required root password and database name.
Run the application with:
This command starts both the Spring Boot application and MySQL container, passing environment variables to the Spring Boot app at runtime.
3. Use .env
File for Environment Variables
Docker Compose supports loading environment variables from a .env
file, which can be useful for organizing environment-specific configurations. You can store variables in a .env
file and reference them in the docker-compose.yml
file.
Example: .env
File
Create a .env
file in the same directory as your docker-compose.yml
:
Then reference these variables in the docker-compose.yml
file:
Docker Compose will automatically load the variables from the .env
file when you run docker-compose up
.
4. Access Environment Variables in Spring Boot
Spring Boot has built-in support for environment variables, and you can access them directly in your application configuration.
Example: application.properties
Spring Boot will automatically bind environment variables to properties in your application.properties
or application.yml
file.
Here, SPRING_DATASOURCE_URL
, SPRING_DATASOURCE_USERNAME
, and SPRING_DATASOURCE_PASSWORD
will be replaced by the environment variables set either in the docker run
command or in the docker-compose.yml
file.
5. Using Dockerfile
to Set Default Environment Variables (Optional)
You can also set default environment variables in the Dockerfile
for your Spring Boot application. However, these variables can be overridden by values passed during container runtime.
Example: Dockerfile with ENV
In this example, the default values for SPRING_DATASOURCE_URL
, SPRING_DATASOURCE_USERNAME
, and SPRING_DATASOURCE_PASSWORD
are set. These defaults can still be overridden when running the container using the docker run -e
flag or via a docker-compose.yml
file.
Conclusion
Configuring environment variables for a Dockerized Spring Boot application provides flexibility in managing dynamic configurations across different environments. You can set environment variables at runtime using the docker run -e
flag, manage them in a docker-compose.yml
file, or use a .env
file for easier configuration management. Additionally, Spring Boot natively supports environment variable injection, allowing you to access these values seamlessly in your application’s configuration.
By properly managing environment variables, you can ensure your Spring Boot application behaves consistently and securely across various deployment environments. Whether you're working locally or in production, Docker makes it easy to configure and manage your application's settings.