2

I am trying to connect MongoDb and Spring Boot in docker-compose.yml.

This is it -

version: "3.7" services: app-server: container_name: spring build: . ports: - "8080:8080" restart: always depends_on: - db db: container_name: mongo image: mongo ports: - "27017:27017" restart: always volumes: - ./datadir:/var/lib/mongo 

My Dockerfile for Spring

FROM openjdk:8-jdk-alpine VOLUME /tmp ARG JAR_FILE COPY ${JAR_FILE} app.jar ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] 

POM.xml plugin to build JAR_FILE -

 <plugin> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin</artifactId> <version>1.3.6</version> <executions> <execution> <id>build-image</id> <phase>package</phase> <goals> <goal>build</goal> </goals> </execution> </executions> <configuration> <repository>maximko/${project.artifactId}</repository> <buildArgs> <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE> </buildArgs> </configuration> </plugin> 

application.properties file

spring.data.mongodb.host=db spring.data.mongodb.port=27017 spring.data.mongodb.database=HotelDB 

My PROBLEM

When i run "maven package" to build my jar I get the following error.My JAR file don't creating

 com.mongodb.MongoSocketException: db at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:188) ~[mongodb-driver-core-3.8.2.jar:na] at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongodb-driver-core-3.8.2.jar:na] at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:62) ~[mongodb-driver-core-3.8.2.jar:na] at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.8.2.jar:na] at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-3.8.2.jar:na] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181] Caused by: java.net.UnknownHostException: db at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) ~[na:1.8.0_181] at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928) ~[na:1.8.0_181] at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323) ~[na:1.8.0_181] at java.net.InetAddress.getAllByName0(InetAddress.java:1276) ~[na:1.8.0_181] at java.net.InetAddress.getAllByName(InetAddress.java:1192) ~[na:1.8.0_181] at java.net.InetAddress.getAllByName(InetAddress.java:1126) ~[na:1.8.0_181] at java.net.InetAddress.getByName(InetAddress.java:1076) ~[na:1.8.0_181] at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:186) ~[mongodb-driver-core-3.8.2.jar:na] ... 5 common frames omitted 

When i run docker-compose.yml I get the following error -

 spring | com.mongodb.MongoSocketOpenException: Exception opening socket spring | at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:67) ~[mongodb-driver-core-3.8.2.jar!/:na] spring | at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.8.2.jar!/:na] spring | at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-3.8.2.jar!/:na] spring | at java.lang.Thread.run(Thread.java:748) [na:1.8.0_212] spring | Caused by: java.net.ConnectException: Connection refused (Connection refused) spring | at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_212] spring | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_212] spring | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_212] spring | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_212] spring | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_212] spring | at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_212] 

MY QUESTIONS

1) Does all these problems come from "application.properties" file, or I misconfigured something in docker-compose file?

2) Can I somehow change the host name of my mongoDB?

3) Do I need to run "mvn package" to build my App.jar file before I run "docker-compose.yml"?

Thanks for the help!

7
  • how you run your containers ? when you start building your app ? Commented Oct 14, 2019 at 14:17
  • @LinPy I run my containers with the command "docker-compose up". I build my app before docker compose up, but always catch this exception Caused by: java.net.UnknownHostException: db. Commented Oct 14, 2019 at 14:23
  • so, how you bild before compose up ? the db container is not there right ? Commented Oct 14, 2019 at 14:24
  • @LinPy Looks like yes. Commented Oct 14, 2019 at 15:03
  • Please try the following: docker-compose stop && docker-compose up db && sleep 30 && docker-compose up app-server Commented Oct 14, 2019 at 23:26

2 Answers 2

0

Finally... I solved my problem. This is my changes:

1)I deleted this plugin from pom.xml

 <plugin> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin</artifactId> <version>1.3.6</version> <executions> <execution> <id>build-image</id> <phase>package</phase> <goals> <goal>build</goal> </goals> </execution> </executions> <configuration> <repository>maximko/${project.artifactId}</repository> <buildArgs> <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE> </buildArgs> </configuration> </plugin> 

and replace this code in Dockerfile. In ENTRYPOINT i added mongodb connection uri to my db service(this is the main change!)

FROM openjdk:8-jdk-alpine VOLUME /tmp COPY target/spring-mongo-demo-0.0.1-SNAPSHOT.jar app.jar RUN sh -c "touch /app.jar" ENTRYPOINT ["java","-Dspring.data.mongodb.uri=mongodb://db:27017/HotelDB","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] 

To build my app.jar I used mavem package, but along with this you should use mavem install if your jar file was not generated

Also in application.properties file i indicated spring.data.mongodb.host=db and caught the following error

Caused by: java.net.UnknownHostException: db 

To avoid this exception when building JAR file I wrote spring.data.mongodb.host=localhost instead

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

Comments

-1

In docker compose file - Use Legacy container link or user-defined bridge network for docker containers.

1 Comment

I did both of your methods, but none of them helped.The same exception

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.