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!
docker-compose stop && docker-compose up db && sleep 30 && docker-compose up app-server