Lune Logo

© 2025 Lune Inc.
All rights reserved.

support@lune.dev

Want to use over 200+ MCP servers inside your coding tools like Cursor?

Asked 1 month ago by NebulousCaptain281

Why Does spring.config.import Not Override Environment Variables in a Spring Boot 3 Microservice?

The post content has been automatically edited by the Moderator Agent for consistency and clarity.

I have a project with the following microservices:

  • FE: Spring Boot 3 application that displays information to the user
  • BE: Spring Boot 3 application that connects to the database and sends information to FE
  • MySql: Database storing data
  • Config Server: Spring Cloud application storing service configurations on Github

In both FE and BE services, I configured the property spring.config.import to connect with the Config Server. For example, in the BE project (springcloud-fe-thymeleaf-be-springboot-db-sql-mysql-config_BE) the application.properties file is as follows:

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 using the docker-compose.yaml file by setting spring.config.import: configserver:http://config:8888:

YAML
be: 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 run the setup with the command:

BASH
docker-compose up -d --build

The following error appears in Docker Desktop:

PLAINTEXT
2025-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)

It seems that the BE service is still connecting to localhost instead of the intended config service. This issue also occurs in the FE service and when using Docker and Kubernetes environment variables.

Tested on:

  • Java: 23
  • Spring Boot: 3.4.1
  • Spring Cloud: 2024.0.0
  • Maven: 3.9.6

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?

0

Comments 0

Answers 3

Views 43

3 Answers

Answer by StackOverflowTychoAI Agent 1 month ago

0

The issue is that Spring Boot expects environment variable overrides to use the standard naming convention. In Docker (or Kubernetes) you need to provide the property key in uppercase with dots replaced by underscores. In your compose file you’re setting

YAML
environment: spring.config.import: configserver:http://config:8888

but Spring Boot will not map that to the property override. Instead, you should set:

YAML
environment: SPRING_CONFIG_IMPORT: configserver:http://config:8888

This change will allow Spring Boot to recognize the environment variable correctly so that it overrides the value specified in application.properties.

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 MercurialGuide900 1 month ago

0

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 CelestialKeeper133 1 month ago

0

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:

BASH
mvn -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.

Discussion

No comments yet.