I have created a simple Spring Boot Rest Application that I have dockerized and is working as expected when I use mongo locally.
However I have now tried to use Docker Compose to run a Mongo container to connect to rather than my local host.
Docker-compose up -d works but when I try to hit my endpoint that queries Monogo I get:
Caused by: java.net.UnknownHostException: mongo: System error
at java.base/java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method) ~[na:na]
at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(Unknown Source) ~[na:na]
at java.base/java.net.InetAddress.getAddressesFromNameService(Unknown Source) ~[na:na]
at java.base/java.net.InetAddress$NameServiceAddresses.get(Unknown Source) ~[na:na]
at java.base/java.net.InetAddress.getAllByName0(Unknown Source) ~[na:na]
at java.base/java.net.InetAddress.getAllByName(Unknown Source) ~[na:na]
at java.base/java.net.InetAddress.getAllByName(Unknown Source) ~[na:na]
at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:203) ~[mongodb-driver-core-3.11.2.jar!/:na]
In my application.properties file I have:
spring.data.mongodb.uri=mongodb://mongo:27017/pokerStats
My Docker file for my spring boot app (called pokerStats):
#
FROM adoptopenjdk/openjdk11:alpine-jre
#
ARG JAR_FILE=/build/libs/pokerStats-0.0.1-SNAPSHOT.jar
#
WORKDIR /opt/app
#
COPY ${JAR_FILE} app.jar
#
ENTRYPOINT ["java","-jar","app.jar"]
My docker-compose.yml file:
version: "3"
services:
pokerStats:
image: pokerStats
ports:
- 8080:8080
depends_on:
- db
db:
image: mongo
volumes:
- ./database:/data
ports:
- "27017:27017"