I am learning docker and trying to create a docker-compose.yml file for a springboot db application(jdk 10). Spring boot picks up 27017 as a default port for mongodb, so if I start a mongo container with below command:
docker run -d -p 27017:27017 mongo
And then start my application in intellij, everything works fine. When I try to use a docker-compose.yml, i get a connection exception.
This is my Dockerfile
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
This is my docker-compose.yml:
services:
java:
image: adoptopenjdk/openjdk10:latest
mongo:
image: mongo
expose:
- "27017"
ports:
- "0.0.0.0:27017:27017"
spring_boot_mongo:
build: .
ports:
- "8080:8080"
links:
- java
version: "2"
Error while running docker-compose up command:
localhost:27017
spring_boot_mongo_1 |
spring_boot_mongo_1 | com.mongodb.MongoSocketOpenException: Exception opening socket
spring_boot_mongo_1 | at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:67) ~[mongodb-driver-core-3.8.2.jar!/:na]
spring_boot_mongo_1 | at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.8.2.jar!/:na]
spring_boot_mongo_1 | at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-3.8.2.jar!/:na]
spring_boot_mongo_1 | at java.lang.Thread.run(Thread.java:748) [na:1.8.0_212]
spring_boot_mongo_1 | Caused by: java.net.ConnectException: Connection refused (Connection refused)
spring_boot_mongo_1 | at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_212]
spring_boot_mongo_1 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_212]
spring_boot_mongo_1 | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_212]
spring_boot_mongo_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_212]
spring_boot_mongo_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_212]
spring_boot_mongo_1 | at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_212]
spring_boot_mongo_1 | at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongodb-driver-core-3.8.2.jar!/:na]
spring_boot_mongo_1 | at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:62) ~[mongodb-driver-core-3.8.2.jar!/:na]
spring_boot_mongo_1 | ... 3 common frames omitted
spring_boot_mongo_1 |
This is what I see when i run docker ps:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4da4948d04d9 spring-boot-mongodb-master_spring_boot_mongo "java -jar /app.jar" 11 seconds ago Up 10 seconds 0.0.0.0:8080->8080/tcp spring-boot-mongodb-master_spring_boot_mongo_1
e9a79f3ba8ab mongo "docker-entrypoint.s…" 12 seconds ago Up 10 seconds 0.0.0.0:27017->27017/tcp spring-boot-mongodb-master_mongo_1
This means the mongo mapping works as expected(I see the same o/p when i start mongodb container with the above mentioned docker run command), but the application port mapping was not achieved. Can someone please help me??
Thanks!
Services from the same docker-compose are connected to same default network. You should use service name in your url when you want to access another container. Container name will be resolved to container IP automatically. You cannot access another container by using localhost - use service name instead. In your case you can set environment variable for your spring_boot_mongo service :
spring_boot_mongo:
build: .
ports:
- "8080:8080"
environment:
- SPRING_DATA_MONGODB_.HOST=mongo
if you are using spring data mongo. Otherwise set the environment variable to override your uri in your application container.
Related
I used Spring initializer to bootstrap the SpringBoot Application. Then I am using Docker Desktop also. I need to dockerize the integration of MongoDB with SpringBoot. My docker-compose.yml file is this,
version: '4.14.2'
services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: nim #<my docker desktop username>
MONGO_INITDB_ROOT_PASSWORD: 123 #<my docker desktop password>
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: nim
ME_CONFIG_MONGODB_ADMINPASSWORD: 123
ME_CONFIG_MONGODB_URL: mongodb://nim:123#mongo:27017/
application.properties :
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=nimasha #<my MongoDB username>
spring.data.mongodb.password=1234 #<my MongoDB password>
spring.data.mongodb.database=StudentDBDocker
spring.data.mongodb.port=27017
spring.data.mongodb.host=localhost
Exception:
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:73) ~[mongodb-driver-core-4.8.2.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:183) ~[mongodb-driver-core-4.8.2.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:198) ~[mongodb-driver-core-4.8.2.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:158) ~[mongodb-driver-core-4.8.2.jar:na]
at java.base/java.lang.Thread.run(Thread.java:1589) ~[na:na]
Caused by: java.net.ConnectException: Connection refused: no further information
at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) ~[na:na]
at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:535) ~[na:na]
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:585) ~[na:na]
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) ~[na:na]
at java.base/java.net.Socket.connect(Socket.java:666) ~[na:na]
at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:107) ~[mongodb-driver-core-4.8.2.jar:na]
at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:82) ~[mongodb-driver-core-4.8.2.jar:na]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:68) ~[mongodb-driver-core-4.8.2.jar:na]
... 4 common frames omitted
mongo-express GUI is running properly as below,
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?
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.
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.
I'm trying to use docker-compose to run my Springboot REST API, which uses Mongo as a database, but I'm receiving the following error. Could someone please assist me in resolving this?
When my spring-boot application tries to connect to MongoDB via the Docker container, it gives me problems. Spring-boot appears to be unable to connect to MongoDB on port 27017.
Below is the part of the code and exception.
DockerFile
FROM adoptopenjdk/openjdk11:alpine-jre
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]
Docker-Compose.yml
version: '3.8'
services:
sprintpoker-springboot-container:
image: sprintpoker-springboot
build:
context: ./
dockerfile: Dockerfile
volumes:
- /data/sprintpoker-springboot-container
ports:
- 8080:8080
depends_on:
- mongo
mongo:
image: mongo
container_name: mongodb
ports:
- 27017:27017
volumes:
- data:/data
environment:
MONGO_INITDB_ROOT_USERNAME: rootuser
MONGO_INITDB_ROOT_PASSWORD: rootpass
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: rootuser
ME_CONFIG_MONGODB_ADMINPASSWORD: rootpass
ME_CONFIG_MONGODB_SERVER: mongo
volumes:
data: {}
networks:
default:
name: mongodb_network
Command I am use to run docker:
docker-compose up -d --build --force-recreate --renew-anon-volumes
Exception on spring-boot docker container:
2021-08-16 03:16:02.453 INFO 1 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
2021-08-16 03:16:02.545 INFO 1 --- [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.2.3.jar!/:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:143) ~[mongodb-driver-core-4.2.3.jar!/:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:188) ~[mongodb-driver-core-4.2.3.jar!/:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:144) ~[mongodb-driver-core-4.2.3.jar!/:na]
at java.base/java.lang.Thread.run(Unknown Source) ~[na:na]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
at java.base/java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[na:na]
at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[na:na]
at java.base/java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[na:na]
at java.base/java.net.SocksSocketImpl.connect(Unknown Source) ~[na:na]
at java.base/java.net.Socket.connect(Unknown Source) ~[na:na]
at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:107) ~[mongodb-driver-core-4.2.3.jar!/:na]
at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-4.2.3.jar!/:na]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.2.3.jar!/:na]
... 4 common frames omitted
application.properties
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=rootuser
spring.data.mongodb.password=rootpass
spring.data.mongodb.database=sprint
spring.data.mongodb.port=27017
spring.data.mongodb.host=localhost
spring.data.mongodb.auto-index-creation=true
Thanks!
Your error is here
2021-08-16 03:16:02.545 INFO 1 --- [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017
The Spring app container isn't running anything on port 27017, let alone a MongoDB server.
Change
spring.data.mongodb.host=localhost
to
spring.data.mongodb.host=mongo
I am able to resolve the issue Thanks to #OneCricketeer and #Michał Krzywański (Mongodb connection error though docker in springboot).
Solution:
Added below line in docker-compose file:
environment:
- SPRING_DATA_MONGODB_.HOST=mongo
expose:
- 27017
Application.yaml / properties file
spring:
data:
mongodb:
database: test
host: mymongodb
port: 27017
spring.data.mongodb.port=27017
spring.data.mongodb.host=<mongo-image-name>
spring.data.mongodb.database=<any-database-name>
Create mongo image :-
docker pull mongo:latest
Stop Local Mongo Service as it runs on the same port
Run docker image :-
docker run -d -p 27017:27017 --name mymongodb mongo:latest --mymongodb
is the container name and will be the host name in yaml or properties
file
DockerFile Example :-
FROM openjdk:11
ADD target/springboot-mongo-docker.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]
Build the jar :- mvn clean install
Build Docker Image :- docker build -t springboot-mongodb:1.0 .
Run The Linked Container as follows :-
docker run -p 8080:8080 --name springboot-mongodb --link
mymongodb:mongo -d springboot-mongodb:1.0
docker-compose.yml :-
docker-compose up
> version: "3"
services: mymongodb:
> image: mongo:latest
> container_name: "mymongodb"
> ports:
> - 27017:27017 springboot-mongodb:
> image: springboot-mongodb:1.0
> container_name: springboot-mongodb
> ports:
> - 8080:8080
> links:
> - mymongodb
Trying to start up a Spring Boot application that connects to two PostgreSQL databases using docker-compose and I am getting a connection refused when Spring tries to connect to either of these databases.
My configuration is as follows:
docker-compose.yml
version: '3.2'
services:
mydb-1:
container_name: mydb-1
image: mydb-1
ports:
- '5432:5432'
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
- POSTGRES_DB=testdb1
mydb-2:
container_name: mydb-2
image: mydb-2
ports:
- '5433:5432'
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
- POSTGRES_DB=testdb2
my-server:
image: my-server-spring
restart: on-failure
depends_on:
- mydb-1
- mydb-2
environment:
- SPRING_DB1-DATASOURCE_JDBC-URL=jdbc:postgresql://mydb-1:5432/testdb1
- SPRING_DB2-DATASOURCE_JDBC-URL=jdbc:postgresql://mydb-2:5433/testdb2
expose:
- '8080'
ports:
- '8080:8080'
Spring application.properties
spring.db1-datasource.jdbc-url= jdbc:postgresql://localhost:5432/testdb1
spring.db1-datasource.username= postgres
spring.db1-datasource.password= postgres
spring.db1-datasource.driverClassName= org.postgresql.Driver
spring.db2-datasource.jdbc-url= jdbc:postgresql://localhost:5433/testdb2
spring.db2-datasource.username= postgres
spring.db2-datasource.password= postgres
spring.db2-datasource.driverClassName= org.postgresql.Driver
Stacktrace (Part of)
org.postgresql.util.PSQLException: Connection to testdb2:5433 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
advidi-server_1 | at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:285) ~[postgresql-42.2.14.jar!/:42.2.14]
advidi-server_1 | at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~[postgresql-42.2.14.jar!/:42.2.14]
advidi-server_1 | at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:217) ~[postgresql-42.2.14.jar!/:42.2.14]
advidi-server_1 | at org.postgresql.Driver.makeConnection(Driver.java:458) ~[postgresql-42.2.14.jar!/:42.2.14]
advidi-server_1 | at org.postgresql.Driver.connect(Driver.java:260) ~[postgresql-42.2.14.jar!/:42.2.14]
advidi-server_1 | at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.5.jar!/:na]
advidi-server_1 | at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358) ~[HikariCP-3.4.5.jar!/:na]
advidi-server_1 | at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-3.4.5.jar!/:na]
advidi-server_1 | at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477) ~[HikariCP-3.4.5.jar!/:na]
advidi-server_1 | at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560) ~[HikariCP-3.4.5.jar!/:na]
advidi-server_1 | at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-3.4.5.jar!/:na]
advidi-server_1 | at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-3.4.5.jar!/:na]
advidi-server_1 | at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.4.18.Final.jar!/:5.4.18.Final]
advidi-server_1 | at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180) ~[hibernate-core-5.4.18.Final.jar!/:5.4.18.Final]
Initially I thought that the problem was that the Spring application was trying to connect to the dbs before their bootstrapping is complete, but that doesn't seem to be the case since with restart: on-failure it should at some point manage to connect.
The default localhost values should also not be a problem since these are replaced by the environment variables in my docker-compose file.
Any ideas?
You're not showing the mydb-1 and mydb-2 image configurations, so I'm practically guessing here.
No need to map the ports to the host. Remove the ports: entries.
The spring container should use port 5432 in order to connect to both DB containers. Each DB container has its own IP so there's no problem with that. Don't connect to localhost because that's the container itself.
No need to specify :5432 in the connection strings because that's the default.
Seems like you have connection settings both in application.properties and the environment: entry in the server configuration. Which is the application using? Anyway, your connection strings should end with mydb-1/testdb1 and mydb-2/testdb2 - nothing more complicated than that.
You're passing SPRING_DB1-DATASOURCE_JDBC-URL in your environment, and not using it in the application.properties. So the variable are not overriding.
You need to either in application.properties use something like
spring.db1-datasource.jdbc-url=$SPRING_DB1-DATASOURCE_JDBC-URL
OR
in environment set the exact name of the variables in your application. properties
- spring.db1-datasource.jdbc-url=jdbc:postgresql://mydb-1:5432/testdb1
I would suggest the second option, which will still allow you to use the application.properties as default values for running locally.
I'm facing MongoDB connection issue from docker container, i read few stackoverflow answers but not helped me to solve my issue.
My application.yml file has the following,
spring:
application:
name: web-ui-service
data:
mongodb:
uri: mongodb://<username>:<pwd>#172.17.0.1:27017/database
and My docker-compose file has the following,
version: '3.5'
services:
sp-app-web-ui:
container_name: sp-app-web-ui
image: swimming-pool/web-ui-service
environment:
- DOCKER_HOST_IP=mongodb://172.17.0.1:27017
- SPRING_DATA_MONGODB_DATABASE=swimming-pool
ports:
- 8080:8080
networks:
- swimming-pool-app
restart: unless-stopped
networks:
swimming-pool-app:
default:
driver: bridge
I read this blog From inside of a Docker container, how do I connect to the localhost of the machine?
but still am facing the following issue,
sp-app-web-ui | com.mongodb.MongoSocketOpenException: Exception opening socket
sp-app-web-ui | at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[mongodb-driver-core-3.4.2.jar!/:na]
sp-app-web-ui | at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[mongodb-driver-core-3.4.2.jar!/:na]
sp-app-web-ui | at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) ~[mongodb-driver-core-3.4.2.jar!/:na]
sp-app-web-ui | at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111]
sp-app-web-ui | Caused by: java.net.ConnectException: Connection refused (Connection refused)
sp-app-web-ui | at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_111]
sp-app-web-ui | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_111]
sp-app-web-ui | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_111]
sp-app-web-ui | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_111]
sp-app-web-ui | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_111]
sp-app-web-ui | at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_111]
sp-app-web-ui | at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57) ~[mongodb-driver-core-3.4.2.jar!/:na]
sp-app-web-ui | at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[mongodb-driver-core-3.4.2.jar!/:na]
I'm using Debian and Docker 18.06. It would be great if you anyone help me out. Thanks in advance!
I have solved the question, my docker-compose file looks as below,
version: '3.5'
services:
sp-app-web-ui:
container_name: sp-app-web-ui
image: swimming-pool/web-ui-service
environment:
- SPRING_PROFILES_ACTIVE=dev
ports:
- 8080:8080
network_mode: host
and add "127.0.0.1" in mongo.conf file in the bindIp variable.
My application.yml has the following,
spring:
application:
name: web-ui-service
data:
mongodb:
uri: mongodb://<username>:<pwd>#172.0.0.1:27017/database
This IP 127.0.0.1 got it from docker0 when you ping ip a on linux.
What you may be looking for is to add network_mode: "host" to your docker compose service:
sp-app-web-ui:
container_name: sp-app-web-ui
image: swimming-pool/web-ui-service
environment:
- DOCKER_HOST_IP=mongodb://172.17.0.1:27017
- SPRING_DATA_MONGODB_DATABASE=swimming-pool
ports:
- 8080:8080
# https://docs.docker.com/compose/compose-file/#network_mode
network_mode: "host"
You can read more about in the docs
Use the docker-compose.yml below. Then run mongodb and mongo-express (UI to access mongodb)
version: '3.1'
services:
mongo:
image: mongo:4.4.5
restart: always
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
ME_CONFIG_MONGODB_URL: mongodb://root:example#mongo:27017/