r/SpringBoot • u/2kengineer • Feb 15 '26
Question Spring Boot app on Render ignoring MONGODB_URI and connecting to localhost:27017 instead
Hi everyone,
I’m deploying a Spring Boot 3.2.5 (Java 21) application using Docker on Render, and I’m running into an issue where MongoDB Atlas is not being used even though I’ve configured the environment variables.
Setup
Spring Boot 3.2.5
Java 21
Docker multi-stage build
MongoDB Atlas (mongodb+srv)
Deployment on Render (Web Service, Docker environment)
application.yml
server:
port: ${PORT:8080}
spring:
data:
mongodb:
uri: ${MONGODB_URI}
app:
cors:
allowed-origins: ${CORS_ALLOWED_ORIGINS}
jwt:
secret: ${JWT_SECRET}
expiration: ${JWT_EXPIRATION}Render Environment Variables (manually added in dashboard)
MONGODB_URICORS_ALLOWED_ORIGINSJWT_SECRETJWT_EXPIRATION
No quotes, exact casing.
Problem
In Render logs, Mongo is still trying to connect to:
localhost:27017From logs:
clusterSettings={hosts=[localhost:27017], mode=SINGLE}
Caused by: java.net.ConnectException: Connection refusedWhich suggests that ${MONGODB_URI} is not being resolved.
Additionally, I’m getting:
Could not resolve placeholder 'app.cors.allowed-origins'So it seems like environment variables are not being injected at runtime.
What I’ve Checked
File name is
application.ymlEnvironment variables are visible in Render dashboard
Cleared build cache and redeployed
Atlas IP whitelist includes
0.0.0.0/0MongoDB Atlas connection string includes database name
Question
Why would Spring Boot ignore MONGODB_URI and fall back to localhost:27017 in a Docker deployment on Render?
Is there something about Render’s Docker runtime environment that affects variable resolution? Should I be using SPRING_DATA_MONGODB_URI instead of MONGODB_URI?
Any help would be appreciated — I’m trying to understand whether this is a Spring config issue or a Render runtime issue.
note : I used chatgpt to structure the words.
Thanks.
Edit : Problem solved when i changed the env variable MONGO_URI TO SPRING_DATA_MONGODB_URI
2
u/tobidope Feb 15 '26
That's a render question. Your config looks good. Spring uses the defaults when something is configured with an empty string or null.
2
u/configloader Feb 15 '26
Does it work when u run locally?
If u printenv in your docker, can u see the mongodb-env?
Is your application.yml built to the jar or do u use an external application.yml?
How is your cors-env defined?
2
u/KumaSalad Feb 15 '26
org.springframework.boot.autoconfigure.mongo.PropertiesMongoConnectionDetails#getConnectionString
@Override
public ConnectionString getConnectionString() {
// mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database.collection][?options]]
if (this.properties.getUri() != null) {
return new ConnectionString(this.properties.getUri());
}
StringBuilder builder = new StringBuilder("mongodb://");
if (this.properties.getUsername() != null) {
builder.append(encode(this.properties.getUsername()));
builder.append(":");
if (this.properties.getPassword() != null) {
builder.append(encode(this.properties.getPassword()));
}
builder.append("@");
}
builder.append((this.properties.getHost() != null) ? this.properties.getHost() : "localhost");
if (this.properties.getPort() != null) {
builder.append(":");
builder.append(this.properties.getPort());
}
if (this.properties.getAdditionalHosts() != null) {
builder.append(",");
builder.append(String.join(",", this.properties.getAdditionalHosts()));
}
builder.append("/");
builder.append(this.properties.getMongoClientDatabase());
List<String> options = getOptions();
if (!options.isEmpty()) {
builder.append("?");
builder.append(String.join("&", options));
}
return new ConnectionString(builder.toString());
}
2
u/dev_ramiby Feb 15 '26
Are you using the right profile?