MongoSocketOpenException and ConnectException when dockerizing the MongoDB with SpringBoot - mongodb

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,

Related

How to connect from spring boot application running in a docker container to localhost mongodb running in localhost:27017?

I have a spring boot application which i have containerized.
I want to use ngnix for reverse proxy and Hashicorp-Consul for service discovery.
Now i need to connect my spring boot application container to mongo db which is NOT RUNNING AS A CONTAINER but runs as a service.
Whenever i try to connect to mongodb from the container i am getting the following exception
2022-10-19 12:35:30.074 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.6.1.jar!/:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:180) ~[mongodb-driver-core-4.6.1.jar!/:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:193) ~[mongodb-driver-core-4.6.1.jar!/:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:157) ~[mongodb-driver-core-4.6.1.jar!/:na]
at java.base/java.lang.Thread.run(Thread.java:829) ~[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(AbstractPlainSocketImpl.java:412) ~[na:na]
at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:255) ~[na:na]
at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:237) ~[na:na]
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:na]
at java.base/java.net.Socket.connect(Socket.java:609) ~[na:na]
at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:107) ~[mongodb-driver-core-4.6.1.jar!/:na]
at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-4.6.1.jar!/:na]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.6.1.jar!/:na]
... 4 common frames omitted
docker-compose.yml:
version: "3.8"
services:
consul:
image: consul:1.6.3
command: agent -server -ui -node=server1 -bootstrap-expect=1 -client=0.0.0.0
ports:
- "8500:8500"
- "8600:8600/udp"
networks:
- consul-net
nginx:
container_name: nginx
image: nginx:latest
restart: always
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
depends_on:
- fc-projects
fc-projects:
image: fc-projects:1.0
container_name: fc-projects
ports:
- "7010:7000"
extra_hosts:
- "host.docker.internal:host-gateway"
bootstrap.yml
spring:
application:
name: fc-projects
cloud:
consul:
enabled: true
host: host.docker.internal
port: 8500
data:
mongodb:
database: firecrowd-dev
port: 27017
host: host.docker.internal
application.yml
server:
port: 7000
spring:
cloud:
consul:
discovery:
instanceId: ${spring.application.name}:${server.port}:${random.value}
management:
endpoints:
web:
exposure:
include: "*"
logging.level:
org.springframework.cloud:
consul: DEBUG

Unable to connect to mongodb as docker compose service from another service

when I launch my application using docker-compose, I get an error that my application cannot connect to the database, although the port is exposed and they are in the same network...
This is my docker-compose.yml file:
version: '3'
volumes:
db-data:
driver: local
mongo-config:
driver: local
services:
pulseq-mongodb:
image: mongo:latest
container_name: server-mongodb
restart: always
networks:
- server-net
ports:
- "27017:27017"
expose:
- 27017
volumes:
- db-data:/data/db
- mongo-config:/data/configdb
server:
image: my-server:0.0.1-pre-alpha.1
container_name: server
restart: always
networks:
- server-net
ports:
- "8080:8080"
depends_on:
- server-mongodb
networks:
server-net:
driver: bridge
I'm getting the following error on startup:
server | 2021-11-01 13:05:10.409 INFO 1 --- [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017
server |
server | com.mongodb.MongoSocketOpenException: Exception opening socket
server | at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.2.3.jar:na]
server | at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:143) ~[mongodb-driver-core-4.2.3.jar:na]
server | at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:188) ~[mongodb-driver-core-4.2.3.jar:na]
server | at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:144) ~[mongodb-driver-core-4.2.3.jar:na]
server | at java.base/java.lang.Thread.run(Unknown Source) ~[na:na]
I tried to use many solutions, but nothing helped me. Any answer will be helpful.
Thanks.
I fixed this by using the container name, instead of localhost in the application configuration.

Mongodb + SpringBoot REST API + Docker Compose not working

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

docker-compose with keycloak & postgres

I am working with docker-compose and I am trying to deploy it on a VPS Centos 8 but keycloak can't access to the postgres database.
However I can connect to the database with pgAdmin.
I have those following errors :
Caused by: java.net.NoRouteToHostException: No route to host (Host unreachable)
at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
at java.base/java.net.Socket.connect(Socket.java:609)
at org.postgresql.jdbc#42.2.5//org.postgresql.core.PGStream.<init>(PGStream.java:70)
at org.postgresql.jdbc#42.2.5//org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91)
at org.postgresql.jdbc#42.2.5//org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192)
... 62 more
13:34:55,281 FATAL [org.keycloak.services] (ServerService Thread Pool -- 65) java.lang.RuntimeException: Failed to connect to database
and
13:34:56,307 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0013: Operation ("add") failed - address: ([("subsyste m" => "microprofile-metrics-smallrye")]): java.lang.NullPointerException
at org.wildfly.extension.microprofile.metrics-smallrye#19.1.0.Final//org.wildfly.extension.microprofile.metrics.MicroProfileMetricsSubsystemAdd$ 2.execute(MicroProfileMetricsSubsystemAdd.java:86)
at org.jboss.as.controller#11.1.1.Final//org.jboss.as.controller.AbstractOperationContext.executeStep(AbstractOperationContext.java:999)
at org.jboss.as.controller#11.1.1.Final//org.jboss.as.controller.AbstractOperationContext.processStages(AbstractOperationContext.java:743)
at org.jboss.as.controller#11.1.1.Final//org.jboss.as.controller.AbstractOperationContext.executeOperation(AbstractOperationContext.java:467)
at org.jboss.as.controller#11.1.1.Final//org.jboss.as.controller.OperationContextImpl.executeOperation(OperationContextImpl.java:1413)
at org.jboss.as.controller#11.1.1.Final//org.jboss.as.controller.ModelControllerImpl.boot(ModelControllerImpl.java:527)
at org.jboss.as.controller#11.1.1.Final//org.jboss.as.controller.AbstractControllerService.boot(AbstractControllerService.java:515)
at org.jboss.as.controller#11.1.1.Final//org.jboss.as.controller.AbstractControllerService.boot(AbstractControllerService.java:477)
at org.jboss.as.server#11.1.1.Final//org.jboss.as.server.ServerService.boot(ServerService.java:448)
at org.jboss.as.server#11.1.1.Final//org.jboss.as.server.ServerService.boot(ServerService.java:401)
at org.jboss.as.controller#11.1.1.Final//org.jboss.as.controller.AbstractControllerService$1.run(AbstractControllerService.java:416)
at java.base/java.lang.Thread.run(Thread.java:834)
This is my docker-compose.yml :
version: "3.7"
services:
keycloak:
container_name: app-keycloak
depends_on:
- postgres
environment:
# Admin account
KEYCLOAK_USER: foo
KEYCLOAK_PASSWORD: foo
#KEYCLOAK_IMPORT: /tmp/realm-export.json
# PostgreSQL DB settings
DB_VENDOR: POSTGRES
DB_ADDR: postgres
DB_PORT: 5432
DB_DATABASE: foo
DB_SCHEMA: public
DB_USER: foo
DB_PASSWORD: foo
image: "jboss/keycloak:10.0.2"
ports:
- "9999:8080"
restart: always
networks:
- auth-network
postgres:
container_name: app-postgres
environment:
POSTGRES_DB: foo
POSTGRES_PASSWORD: foo
POSTGRES_USER: foo
image: "postgres:13-alpine"
ports:
- "5432:5432"
restart: always
volumes:
- "./volumes/postgres-data:/var/lib/postgresql/data"
networks:
- auth-network
networks: auth-network:
driver: bridge
Do you have any ideas of how I can resolve this ?
With my my Windows 10 PC, that solution work but I still have thoses errors on my VPS
Centos 8. Do you have others ideas ?
firewall-cmd --zone=public --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources:
services: cockpit dhcpv6-client https ssh
ports: 8077/tcp 27017/tcp 8081/tcp 9999/tcp 5432/tcp 8443/tcp 8080/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
Working example for me
version: '3'
volumes:
postgres_data:
driver: local
services:
postgres:
image: postgres
volumes:
- postgres_data:/var/lib/postgresql/data
environment:
POSTGRES_DB: keycloak
POSTGRES_USER: keycloak
POSTGRES_PASSWORD: password
keycloak:
image: quay.io/keycloak/keycloak:latest
environment:
DB_VENDOR: POSTGRES
DB_ADDR: postgres
DB_DATABASE: keycloak
DB_USER: keycloak
DB_SCHEMA: public
DB_PASSWORD: password
KEYCLOAK_USER: admin
KEYCLOAK_PASSWORD: Pa55w0rd
# Uncomment the line below if you want to specify JDBC parameters. The parameter below is just an example, and it shouldn't be used in production without knowledge. It is highly recommended that you read the PostgreSQL JDBC driver documentation in order to use it.
#JDBC_PARAMS: "ssl=true"
ports:
- 8080:8080
depends_on:
- postgres

Docker container not connecting mongodb host

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/