A Spring Boot container cannot open a socket to the MongoDB container
Fix it fast
Most common cause, 1 of 3The connection string says localhost, which is the app's own container
Give the app the URI through an environment variable that uses the Compose service name.
services:
app:
environment:
SPRING_MONGODB_URI: mongodb://mongo:27017/appdb
mongo:
image: mongo:8Short answer
com.mongodb.MongoSocketOpenException: Exception opening socket means the Java driver could not reach the address it was given. In Docker Compose that address is usually wrong: localhost is the app container itself, Spring Boot 4 ignores the old spring.data.mongodb.uri and falls back to localhost, or the URI carries the published host port. Point it at mongodb://mongo:27017.
Which cause do you have?
Find the host and port the driver is dialling, then the Mongo settings the container really received. A mismatch between the two points straight at the cause.
docker compose logs app | grep -E "connecting to server|MongoSocketOpenException"
docker compose exec app env | grep -i mongoThen find the line you see in this list and open its fix:
- 1The connection string says localhost, which is the app's own container
The driver log names localhost:27017 or 127.0.0.1:27017, and the same jar connects fine when started on the host - 2Spring Boot 4 no longer reads spring.data.mongodb settings
spring.data.mongodb.uri or SPRING_DATA_MONGODB_URI points at mongo, yet the driver still dials localhost:27017 after an upgrade to Boot 4 - 3The URI uses the published host port instead of 27017
The log names mongo:27018 (or whatever stands left of the colon under ports), and every attempt is refused
What does the error look like?
com.mongodb.MongoSocketOpenException: Exception opening socketException in monitor thread while connecting to server localhost:27017Timed out while waiting for a server that matches
Cause 1 of 3
The connection string says localhost, which is the app's own container
You seeThe driver log names localhost:27017 or 127.0.0.1:27017, and the same jar connects fine when started on the host
Check
A Spring Boot app in Compose that throws MongoSocketOpenException for localhost:27017 is knocking on its own container. Compose registers every service name in an internal DNS server, and the mongo image's own docs connect to it over a Docker network exactly as to a remote mongod: by that name, on port 27017.
grep -rn 'mongodb://' src/main/resources/Fix
Give the app the URI through an environment variable that uses the Compose service name. Operating-system variables override application.properties inside the jar.
services:
app:
environment:
SPRING_MONGODB_URI: mongodb://mongo:27017/appdb
mongo:
image: mongo:8Verify
Recreate the app container; the driver now reports a successful connection to mongo:27017.
docker compose up -d --force-recreate app
docker compose logs --since 2m app | grep "Monitor thread successfully connected"Confirmed by docs.docker.com
Cause 2 of 3
Spring Boot 4 no longer reads spring.data.mongodb settings
You seespring.data.mongodb.uri or SPRING_DATA_MONGODB_URI points at mongo, yet the driver still dials localhost:27017 after an upgrade to Boot 4
Check
Spring Boot 4.0 moved the MongoDB connection keys from the spring.data.mongodb prefix to spring.mongodb. Keys under the old name are simply not bound any more, so Boot builds the client from its default URI, mongodb://localhost/test, and the socket error follows although your value looks correct.
grep -rn -E 'spring\.data\.mongodb\.(uri|host|port|database)|SPRING_DATA_MONGODB' src/main/resources/ compose.yamlFix
Rename the keys and the matching environment variables. Settings that need Spring Data itself, such as spring.data.mongodb.auto-index-creation, keep the old prefix. While you upgrade, spring-boot-properties-migrator lists renamed keys at start-up.
# application.properties
spring.mongodb.uri=mongodb://mongo:27017/appdb
# compose.yaml, service app
environment:
SPRING_MONGODB_URI: mongodb://mongo:27017/appdbVerify
The driver's monitor line now names mongo:27017 instead of localhost.
docker compose logs --since 2m app | grep 'mongo:27017'Confirmed by github.com
Cause 3 of 3
The URI uses the published host port instead of 27017
You seeThe log names mongo:27018 (or whatever stands left of the colon under ports), and every attempt is refused
Check
Service-to-service traffic inside a Compose project always uses the port the container itself listens on. A ports entry such as 27018:27017 opens 27018 on your own machine only, so from inside the app container nothing listens on mongo:27018.
docker compose ps mongo
grep -n -A2 "ports:" compose.yamlFix
Keep 27017 in the URI the app reads, whatever the host-side port is. The host-side port stays useful for mongosh or Compass on your machine.
SPRING_MONGODB_URI: mongodb://mongo:27017/appdbVerify
With Spring Boot Actuator on the classpath, the health endpoint reports UP once MongoDB answers.
curl -s http://localhost:8080/actuator/healthConfirmed by docs.docker.com
How do I stop it coming back?
- Keep one Mongo URI per environment in an environment variable (SPRING_MONGODB_URI) and leave localhost out of the packaged application.properties.
- Run spring-boot-properties-migrator for one start when moving to Spring Boot 4, then remove it once the renamed keys are fixed.
- Write the container port, never the host-side port, in every service-to-service URL in compose.yaml.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: Dispatch Pro, TaxiDex.