Asked 1 month ago by CosmicNomad576
Why isn't spring.config.import overridden by environment variables in my Spring Boot 3 microservices?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by CosmicNomad576
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I have a project composed of several microservices:
In both FE and BE, I configured the property spring.config.import to connect these services to the Config Server. For example, the BE project's (springcloud-fe-thymeleaf-be-springboot-db-sql-mysql-config_BE) application.properties contains:
PROPERTIES# Port server.port=8081 # Service Name spring.application.name=be # Config Server spring.config.import=configserver:http://localhost:8888
I attempted to override this property in the docker-compose.yaml file by using the environment variable (spring.config.import: configserver:http://config:8888):
YAMLbe: image: be-image:0.0.1 container_name: be-container build: context: ./springcloud-fe-thymeleaf-be-springboot-db-sql-mysql-config_BE dockerfile: Dockerfile depends_on: config: condition: service_healthy ports: - 8081:8081 environment: spring.config.import: configserver:http://config:8888 healthcheck: test: ["CMD", "curl", "-f", "http://localhost:8081/actuator/health"] interval: 10s timeout: 10s retries: 5 start_period: 10s networks: - helloworld-network
I start the docker compose with:
BASHdocker-compose up -d --build
However, I encounter the following error in Docker Desktop:
TEXT2025-01-20 17:02:36 Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8888/be/default": Connection refused 2025-01-20 17:02:36 at org.springframework.web.client.RestTemplate.createResourceAccessException(RestTemplate.java:926) 2025-01-20 17:02:36 at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:906)
This error indicates that the BE service is trying to connect to localhost instead of the config service. The same behavior occurs in the FE service under both Docker and Kubernetes environment settings.
Tested on:
GitHub repository: https://github.com/wisniewskikr/chrisblog-it-cloud/tree/main/spring-cloud/config/springcloud-fe-thymeleaf-be-springboot-db-sql-mysql-config
Why is the spring.config.import property not being overwritten by environment variables from Docker, Docker Compose, or Kubernetes?
Comments 0
•Answers 3
•Views 42
Answer by TychoAI Agent • 1 month ago
When using Spring Boot 3 with the new import mechanism (spring.config.import), that property is processed during the bootstrap phase. The bootstrap phase happens before the normal environment properties (like those provided via Docker Compose’s environment block or Kubernetes config) are merged, so your override isn’t taking effect.
-DSPRING_CONFIG_IMPORT=configserver:http://config:8888
). This way, the override is available during the bootstrap phase.Here’s an example of passing the parameter as a command-line argument (or you can set it as an environment variable that the JVM recognizes at startup):
BASHdocker run -e JAVA_OPTS="-Dspring.config.import=configserver:http://config:8888" be-image:0.0.1
Or if using Docker Compose, modify the command section:
YAMLbe: image: be-image:0.0.1 container_name: be-container build: context: ./springcloud-fe-thymeleaf-be-springboot-db-sql-mysql-config_BE dockerfile: Dockerfile depends_on: config: condition: service_healthy ports: - "8081:8081" command: > java -Dspring.config.import=configserver:http://config:8888 -jar your-app.jar healthcheck: test: ["CMD", "curl", "-f", "http://localhost:8081/actuator/health"] interval: 10s timeout: 10s retries: 5 start_period: 10s networks: - helloworld-network
This ensures that the property is available when the bootstrap context is loaded.
The key point is that spring.config.import is handled during the bootstrap phase, which prevents later environment overrides (from Docker Compose or Kubernetes) from taking effect. Use command-line parameters or a bootstrap file to override this property early enough in the application startup process.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
Answer by EclipseTracker490 • 1 month ago
The '.' in an environment variable that should overwrite a matching application.properties value should be replaced by an '_' and convert to uppercase, ie SPRING_CONFIG_IMPORT - see here
No comments yet.
Answer by NebularTracker819 • 1 month ago
I found workaround for this problem. If there is NO property spring.config.import in application.properties file then environment variables from Docker, Docker Compose and Kubernetes work file.
Problem is when I try to run this service manually from command line tool. I have to use command with parameter spring-boot.run.arguments then:
BASHmvn -f ./springcloud-fe-thymeleaf-be-springboot-db-sql-mysql-config_BE spring-boot:run -Dspring-boot.run.arguments="--spring.config.import=configserver:http://localhost:8888"
No comments yet.
No comments yet.