0

I am trying to learn app deployment with Docker. My configurations are as below:

application.properties

####### Mongo Properties ###########
spring.data.mongodb.uri=mongodb://mongo/locationsdb

Dockerfile

FROM openjdk:14-alpine
ARG JAR_FILE=./target/*jar
COPY ${JAR_FILE} jarapp.jar
EXPOSE 8080
ENTRYPOINT ["java", "-Dspring.profiles.active=docker", "-jar", "jarapp.jar"]

docker-compose.yml

version: "3"

services:
  mongodb-container:
    image: mongo:latest
    container_name: "mongodb-container"
    restart: always
    ports:
      - 27017:27017
  server-container:
    image: server_side
    container_name: "server-container"
    restart: always
    ports:
      - 8080:8080
    links:
      - mongodb-container
    depends_on:
      - mongodb-container

After the above then I did the following:

docker-compose config

docker-compose up --build

But I was gething the below error:

server-container   | 2021-09-02 09:44:41.253  INFO 1 --- [localhost:27017] org.mongodb.driver.cluster               : ***Exception in monitor thread while connecting to server localhost:27017***
server-container   |
server-container   | com.mongodb.MongoSocketOpenException: **Exception opening socket**
server-container   |    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.2.3.jar!/:na]
server-container   |    at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:143) ~[mongodb-driver-core-4.2.3.jar!/:na]
server-container   |    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:188) ~[mongodb-driver-core-4.2.3.jar!/:na]
server-container   |    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:144) ~[mongodb-driver-core-4.2.3.jar!/:na]
server-container   |    at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]
server-container   | Caused by: java.net.ConnectException: Connection refused
server-container   |    at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
server-container   |    at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:589) ~[na:na]
server-container   |    at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542) ~[na:na]
server-container   |    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597) ~[na:na]
server-container   |    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:333) ~[na:na]
server-container   |    at java.base/java.net.Socket.connect(Socket.java:648) ~[na:na]
server-container   |    at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:107) ~[mongodb-driver-core-4.2.3.jar!/:na]
server-container   |    at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-4.2.3.jar!/:na]
server-container   |    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.2.3.jar!/:na]
server-container   |    ... 4 common frames omitted
server-container   |
server-container   | 2021-09-02 09:44:43.395  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
server-container   | 2021-09-02 09:44:43.429  INFO 1 --- [           main] c.f.virtuallab.VirtuallabApplication     : Started VirtuallabApplication in 26.943 seconds (JVM running for 28.445)
mongodb-container  | {"t":{"$date":"2021-09-02T09:45:13.967+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"Checkpointer","msg":"WiredTiger message","attr":{"message":"[1630575913:967258][1:0x7fef40740700], WT_SESSION.checkpoint: [WT_VERB_CHECKPOINT_PROGRESS] saving checkpoint snapshot min: 34, snapshot max: 34 snapshot count: 0, oldest timestamp: (0, 0) , meta checkpoint timestamp: (0, 0) base write gen: 1"}}

As it shows in the log, There was a Exception opening socket problem and then it says this: server-container | 2021-09-02 09:44:43.429 INFO 1 --- [ main] c.f.virtuallab.VirtuallabApplication : Started VirtuallabApplication in 26.943 seconds (JVM running for 28.445) afterwards.

When i tried my end point: localhost:8080/api/v1/locations I was only getting Internal Server Error (500).

Could someone guide me on how to properly connect the mongodb and get the application started?

Kent
  • 33
  • 7
  • Does this answer your question? [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) – Noam Yizraeli Sep 02 '21 at 15:23

2 Answers2

1

Try changing

####### Mongo Properties ###########
spring.data.mongodb.uri=mongodb://mongodb-container/locationsdb

You are using mongo as your mongodb host but you have declared mongodb container as mongodb-container in your docker-compose file. So your mongodb instance should be accessed by mongodb-container and not by mongo.

Shawrup
  • 2,478
  • 2
  • 12
  • 21
  • I am still gething the same problem... Could it be as a result of mongo and jdk version? – Kent Sep 02 '21 at 11:05
  • 1
    Do you have another property file ? like application-docker.properties ? Do you have mongodb uri in that ? In the log i see that , server_side trying to connect to localhost. – Shawrup Sep 02 '21 at 12:20
  • NO. I am only having the application.properties file and the content now is this: spring.data.mongodb.uri=mongodb://mongodb-container/locationsdb. So where the localhost:27017 is coming from I don't know. I have even uninstalled the locally install mongodb but still the same problem. – Kent Sep 02 '21 at 15:36
  • I found out that I was getting **Exception opening socket** when I tried to start my spring-boot application locally after uninstalling Mongodb. But as soon as I reinstalled Mongodb, the application runs fine locally. Could it be that docker-compose.yml is not properly installing Mongo? – Kent Sep 02 '21 at 16:19
  • I think spring boot is not reading the docker profile properties. Can you re build the docker image ? Your image might be old. Also can you check what is the actual value of `spring.data.mongodb.uri` when the application starts ? You can add a log to print the value. – Shawrup Sep 02 '21 at 17:38
0

I think no need to expose mongodb port with host machine if in the frontend it's already programmed. But have to mention same username & password alogwith hostname I mean container name to the mongodb container.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 12 '21 at 10:48