1

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"

1 Answer 1

3

The properties file specifies the host mongo, but containers are discoverable via the container name, which is db.

Either the properties need to be changed to use the right name or the container name changed to what the properties file expects.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.