spring - java.lang.IllegalStateException: No host port mapping found for container port 27017 in Docker Compose setup with Mongo

admin2025-04-17  3

I'm working on a Spring Boot application that connects to MongoDB running in a Docker container using Docker Compose. However, I'm encountering the following error:

java.lang.IllegalStateException: No host port mapping found for container port 27017

Here's the relevant part of my docker-compose.yml file:

services:
  mongo:
    image: mongo:latest
    container_name: character-media
    command: mongod --port 27018
    volumes:
      - mongo-data:/data/db
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=secret
    ports:
      - "27018:27018"
    restart: unless-stopped

volumes:
  mongo-data:

application.yml config:

spring:
  data:
    mongodb:
      uri: mongodb://admin:secret@localhost:27018/character_data

Problem: In my docker-compose.yml, I expose MongoDB on port 27018 (27018:27018), but the Spring Boot app is trying to connect to MongoDB on port 27017, which is causing the error No host port mapping found for container port 27017.

Steps I have taken:

*I've already configured MongoDB to use port 27018 in the Docker Compose file.

*I checked the Spring Boot configuration and confirmed it points to localhost:27018.

*I tried restarting the Docker containers, but the issue persists.

I'm working on a Spring Boot application that connects to MongoDB running in a Docker container using Docker Compose. However, I'm encountering the following error:

java.lang.IllegalStateException: No host port mapping found for container port 27017

Here's the relevant part of my docker-compose.yml file:

services:
  mongo:
    image: mongo:latest
    container_name: character-media
    command: mongod --port 27018
    volumes:
      - mongo-data:/data/db
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=secret
    ports:
      - "27018:27018"
    restart: unless-stopped

volumes:
  mongo-data:

application.yml config:

spring:
  data:
    mongodb:
      uri: mongodb://admin:secret@localhost:27018/character_data

Problem: In my docker-compose.yml, I expose MongoDB on port 27018 (27018:27018), but the Spring Boot app is trying to connect to MongoDB on port 27017, which is causing the error No host port mapping found for container port 27017.

Steps I have taken:

*I've already configured MongoDB to use port 27018 in the Docker Compose file.

*I checked the Spring Boot configuration and confirmed it points to localhost:27018.

*I tried restarting the Docker containers, but the issue persists.

Share Improve this question asked Jan 30 at 17:33 MaxwellMaxwell 155 bronze badges 2
  • Did you try setting individual properties spring.data.mongodb.*? – silentsudo Commented Jan 30 at 17:43
  • You shouldn't normally need to change the port inside the container, and it looks like you're using some sort of wrapper that either inspects the image and finds its exposed port or has specific knowledge of MongoDB. Does it work to delete the command: setting and then make the second ports: number the standard MongoDB port, ports: ['27018:27017']? – David Maze Commented Jan 30 at 18:33
Add a comment  | 

1 Answer 1

Reset to default 0

Your docker-compose seems to be broken for port configuration: It should be:

command: --port 27018

and not command: mongod --port 27018.

Complete docker-compose.yml as bewlow:

name: mongodb
services:
  mongodb:
    image: mongo:8.0.3    
    command: --port 27018
    ports:
      - "27018:27018"
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=secret
转载请注明原文地址:http://anycun.com/QandA/1744901529a89227.html