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.
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
command:
setting and then make the secondports:
number the standard MongoDB port,ports: ['27018:27017']
? – David Maze Commented Jan 30 at 18:33