Docker Community Forums

Share and learn in the Docker community.

Mongodb container runs on docker on port 27017 but connexion is refused when executing a web service

Hello everyone ;
I’am very new to the devops environement , this is my first project .I actually have an angular/spring boot web applciation where I used angular as a front end in which I consumed spring boot web services (backend) and all the database connexion was made in spring boot and I used mongodb as a database .
I’am using windows 10 on which i downloaded docker .
As a first step I have created in my angular project a docker file and a docker-compose file and created an image then a container for my angular project and it runs perfectly. Next step was to create my pring boot image on docker and i did and it works perfectly (the image and the container for spring boot ) after that i have added in the same docker file in my spring boot app a container for my mongodb application , the conf for which i added thewse lignes in docker file :
FROM adoptopenjdk/openjdk11:alpine-jre
ADD target/spring-boot-security-jwt-mongodb-0.0.1-SNAPSHOT.jar app.jar

EXPOSE 8080

CMD [“java”,"-Dspring.data.mongodb.uri=mongodb://mongodb:27017/ParkInDB","-jar",“app.jar”]

My docker-compose file :
version: “3.1”

services:

mongodb:
image: mongo
ports:
- 27017:27017

springboot-docker-container:
image: springboot-docker-container
container_name: springboot-docker-container
ports:
- 8080:8080
build:
context: ./
dockerfile: Dockerfile
depends_on:
- mongodb

my app.properties file :
spring.data.mongodb.database=ParkInDB
spring.data.mongodb.uri=spring.data.mongodb.uri=mongodb://mongodb:27017/ParkInDB

Now , all the containers are running fine on docker but when i try to execute any function like add employee or whatever to add it in the database i get this error :
{

"timestamp": "2020-11-10T09:55:02.098+0000",

"status": 500,

"error": "Internal Server Error",

"message": "Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]",

"path": "............."

}

I’m sorry that i made this very long but i have been stuck here for days with no help whatsover , please if anyone has any idea , help me .
Thank you

If you specified the correct port and still not able to connect to mongodb running in docker (like me), make sure you are using the service name (or container name) in your connection URL, e.g. mongodb://mongodb_service:27017/mydb, which is defined in your docker-compose.yml :

services:
mongodb_service:
image: mongo
I was using the hostname value and that’s not the correct thing to do. You could verify this by looking at docker inspect mongodb_service in the Aliases section.