Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Servers, nginx and Docker

A Spring Boot container cannot open a socket to the MongoDB container

Your error

com.mongodb.MongoSocketOpenException: Exception opening socket2 more lines it can show

Fix it fast

Most common cause, 1 of 3

The 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.

Fix
services:
  app:
    environment:
      SPRING_MONGODB_URI: mongodb://mongo:27017/appdb
  mongo:
    image: mongo:8
Did this fix it?
Full steps for this cause: check, fix, verify

Short 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.

  • Applies to Spring Boot 4.0 to 4.2, MongoDB Java driver 5.x, mongo 8.0 to 9.0 images, Docker Compose v2 and newer
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
docker compose logs app | grep -E "connecting to server|MongoSocketOpenException"
docker compose exec app env | grep -i mongo

Then find the line you see in this list and open its fix:

  1. 1The connection string says localhost, which is the app's own containerThe driver log names localhost:27017 or 127.0.0.1:27017, and the same jar connects fine when started on the host
  2. 2Spring Boot 4 no longer reads spring.data.mongodb settingsspring.data.mongodb.uri or SPRING_DATA_MONGODB_URI points at mongo, yet the driver still dials localhost:27017 after an upgrade to Boot 4
  3. 3The URI uses the published host port instead of 27017The 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 socket
  • Exception in monitor thread while connecting to server localhost:27017
  • Timed 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.

Check
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.

Fix
services:
  app:
    environment:
      SPRING_MONGODB_URI: mongodb://mongo:27017/appdb
  mongo:
    image: mongo:8

Verify

Recreate the app container; the driver now reports a successful connection to mongo:27017.

Verify
docker compose up -d --force-recreate app
docker compose logs --since 2m app | grep "Monitor thread successfully connected"

Confirmed by docs.docker.com

Did this fix it?

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.

Check
grep -rn -E 'spring\.data\.mongodb\.(uri|host|port|database)|SPRING_DATA_MONGODB' src/main/resources/ compose.yaml

Fix

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.

Fix
# application.properties
spring.mongodb.uri=mongodb://mongo:27017/appdb

# compose.yaml, service app
environment:
  SPRING_MONGODB_URI: mongodb://mongo:27017/appdb

Verify

The driver's monitor line now names mongo:27017 instead of localhost.

Verify
docker compose logs --since 2m app | grep 'mongo:27017'

Confirmed by github.com

Did this fix it?

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.

Check
docker compose ps mongo
grep -n -A2 "ports:" compose.yaml

Fix

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.

Fix
SPRING_MONGODB_URI: mongodb://mongo:27017/appdb

Verify

With Spring Boot Actuator on the classpath, the health endpoint reports UP once MongoDB answers.

Verify
curl -s http://localhost:8080/actuator/health

Confirmed by docs.docker.com

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.