I try to figure out why the communication between dockerized Spring boot app and mongoDB is not working. First things first:
The whole microservice is running localy perfectly.
I create an application.properties with the profile name docker:
application-docker.properties
server.port=8091
spring.application.name=customer
# ============================================================================================================
# EUREKA
# It's not possible to use localhost in the docker container. Instate use the container name.
# In this case "eureka-server". See the file docker-compose.yml to check the container name.
eureka.client.service-url.defaultZone=http://eureka-server:8761/eureka
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true
# ============================================================================================================
# ZIPKIN
# It's not possible to use localhost in the docker container. Instate use the container name.
# In this case "zipkin". See the file docker-compose.yml to check the container name.
spring.zipkin.base-url=http://zipkin:9411
# ============================================================================================================
# RABBIT_MQ
# It's not possible to use localhost in the docker container. Instate use the container name.
# In this case "rabbitmq". See the file docker-compose.yml to check the container name.
spring.rabbitmq.addresses=rabbitmq:5672
rabbitmq.exchanges.internal=internal.exchange
rabbitmq.queue.notification=notification.queue
rabbitmq.routing-keys.internal-notification=internal.notification.routing-key
# ============================================================================================================
# MONGO-DB
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=rootuser
spring.data.mongodb.password=rootpass
#spring.data.mongodb.database=data
#spring.data.mongodb.host=spring
#spring.data.mongodb.port=27017
spring.data.mongodb.uri=mongodb://mongodb:27017/data
and then the docker-compose.yml
services:
mongodb:
container_name: mongodb
image: mongo:latest
hostname: mongo
restart: unless-stopped
ports:
- 27017:27017
volumes:
- data:/data
environment:
- MONGO_INITDB_ROOT_USERNAME=rootuser
- MONGO_INITDB_ROOT_PASSWORD=rootpass
networks:
- spring
mongo-express:
container_name: mongo-express
image: mongo-express
restart: always
ports:
- 8095:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=rootuser
- ME_CONFIG_MONGODB_ADMINPASSWORD=rootpass
- ME_CONFIG_MONGODB_SERVER=mongodb
networks:
- spring
zipkin:
image: openzipkin/zipkin
container_name: zipkin
ports:
- 9411:9411
networks:
- spring
rabbitmq:
image: rabbitmq:3.9.11-management-alpine
container_name: rabbitmq
ports:
- 5672:5672
- 15672:15672
networks:
- spring
eureka-server:
image: koulombus/eureka-server:latest
container_name: eureka-server
ports:
- 8761:8761
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- spring
depends_on:
- zipkin
api-gateway:
image: koulombus/api-gateway:latest
container_name: api-gateway
ports:
- 8090:8090
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- spring
depends_on:
- zipkin
- eureka-server
customer:
image: koulombus/customer:latest
container_name: customer
ports:
- 8091:8091
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- spring
depends_on:
- zipkin
- eureka-server
- rabbitmq
- mongodb
fraud:
image: koulombus/fraud:latest
container_name: fraud
ports:
- 8092:8092
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- spring
depends_on:
- zipkin
- eureka-server
- rabbitmq
notification:
image: koulombus/notification:latest
container_name: notification
ports:
- 8093:8093
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- spring
depends_on:
- zipkin
- eureka-server
- rabbitmq
volumes:
postgres:
pgadmin:
data: {}
networks:
postgres:
driver: bridge
spring:
driver: bridge
after starting docker…
mvn clean package -P "build-docker-image"
docker compose up -d
Facts
Problem
When I check now the customer log in docker i facet following:
,-----. ,--. ,--. ,---. ,--------. ,-----. ,--. ,--. ,------. ,------.
' .--./ | | | | ' .-' '--. .--' ' .-. ' | `.' | | .---' | .--. '
| | | | | | `. `-. | | | | | | | |'.'| | | `--, | '--'.'
' '--' ' '-' ' .-' | | | ' '-' ' | | | | | `---. | |
`-----' `-----' `-----' `--' `-----' `--' `--' `------' `--' '--'
Application Name:
Application Version:
:: Spring Boot :: (v2.6.3)
2022-03-03 08:58:55.187 INFO [customer,,] 1 --- [ main] c.k.customer.CustomerApplication : Starting CustomerApplication using Java 17.0.1 on cf6b5b41507d with PID 1 (/app/classes started by root in /)
2022-03-03 08:58:55.241 INFO [customer,,] 1 --- [ main] c.k.customer.CustomerApplication : The following profiles are active: docker
2022-03-03 08:59:01.557 INFO [customer,,] 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data MongoDB repositories in DEFAULT mode.
2022-03-03 08:59:02.239 INFO [customer,,] 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 667 ms. Found 1 MongoDB repository interfaces.
2022-03-03 08:59:04.589 INFO [customer,,] 1 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=9e7fc4f4-3641-3895-b9a0-4754b711f194
2022-03-03 08:59:08.906 INFO [customer,,] 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8091 (http)
2022-03-03 08:59:08.952 INFO [customer,,] 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-03-03 08:59:08.953 INFO [customer,,] 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-03-03 08:59:09.454 INFO [customer,,] 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-03-03 08:59:09.454 INFO [customer,,] 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 13490 ms
2022-03-03 08:59:12.729 INFO [customer,,] 1 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[mongodb:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
2022-03-03 08:59:13.071 INFO [customer,,] 1 --- [}-mongodb:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server mongodb:27017
com.mongodb.MongoSocketException: mongodb
at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:211) ~[mongodb-driver-core-4.4.1.jar:na]
at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:75) ~[mongodb-driver-core-4.4.1.jar:na]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.4.1.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:180) ~[mongodb-driver-core-4.4.1.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:188) ~[mongodb-driver-core-4.4.1.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:152) ~[mongodb-driver-core-4.4.1.jar:na]
at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
Caused by: java.net.UnknownHostException: mongodb
at java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:801) ~[na:na]
at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:887) ~[na:na]
at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1509) ~[na:na]
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1367) ~[na:na]
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1301) ~[na:na]
at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:203) ~[mongodb-driver-core-4.4.1.jar:na]
... 6 common frames omitted
Somewhere I make a mistake. I would be very grateful for a hint.
2
Answers
OK, working solution for me:
application-docker.properties
docker-compose.yml
You’re missing an ‘s’ in
spring.data.mongodb.uri
in your config file.