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/
Related
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
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.
When I run my docker-compose, I face this error:
Error message
app | 2021-09-23 11:52:51.860 ERROR 1 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
app |
app | org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
app | at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:280) ~[postgresql-42.2.5.jar!/:42.2.5]
app | at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~[postgresql-42.2.5.jar!/:42.2.5]
app | at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195) ~[postgresql-42.2.5.jar!/:42.2.5]
I tried to change the url in my application.properties file and the error is the same.
This is my application.properties file:
application.properties
spring.datasource.url=jdbc:postgresql://postgres:5432/employees
spring.datasource.username=postgres
spring.datasource.password=root
spring.jpa.show-sql=true
server.port=8086
## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
`
This is my Dockerfile:
Dockerfile
from openjdk:8
copy ./target/springboot2-postgresql-jpa-hibernate-crud-example-0.0.1-SNAPSHOT.jar employee-jdbc-0.0.1-SNAPSHOT.jar
CMD ["java","-jar","employee-jdbc-0.0.1-SNAPSHOT.jar"]
And this is my docker-compose:
docker-compose
version: "3"
services:
postgres:
image: postgres:latest
network_mode: bridge
container_name: postgres
volumes:
- postgres-data:/var/lib/postgresql/data
expose:
- 5430
ports:
- 5430:5430
environment:
- POSTGRES_PASSWORD=root
- POSTGRES_USER=postgres
- POSTGRES_DB=employees
# APP*****************************************
app:
image: app
network_mode: bridge
container_name: app
expose:
- 8081
ports:
- 8081:8081
depends_on:
- postgres
links:
- postgres
environment:
- POSTGRES_PASSWORD=root
- POSTGRES_USER=postgres
- POSTGRES_DB=employees
- POSTGRES_URL=jdbc:postgresql://postgres:5432/employees
volumes:
postgres-data:
Can you run this command to check
sudo lsof -n -u postgres |grep LISTEN or sudo netstat -ltnp | grep postgres
should show the TCP/IP addresses and ports PostgreSQL is listening on
It can be postgres is already running and your image is not running using docker. Maybe killing the process will help you if it is already running.
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
---For Docker desktop on windows (on linux containers)---
Postman Error -"message": "java.net.ConnectException: Connection refused (Connection refused)".
Docker compose log- productservice | com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused (Connection refused)
Spring properties -------
spring.data.mongodb.database=product
#spring.data.mongodb.username=root
#spring.data.mongodb.password=root
spring.data.mongodb.port=27017
#spring.data.mongodb.host=localhost
spring.data.mongodb.host=api-database
#spring.data.mongodb.host=host.docker.internal
Dockerfile ---------
FROM openjdk:8-jre-slim
COPY ./target/ProductService-0.0.1-SNAPSHOT.jar /usr/src/ProductService/
WORKDIR /usr/src/ProductService
EXPOSE 8105
CMD ["java", "-Dspring.data.mongodb.uri=mongodb://api-database:27017/api-database","-Djava.security.egd=file:/dev/./urandom","-jar","./ProductService-0.0.1-SNAPSHOT.jar"]
Docker-compose.yml -----
version: '3'
services:
product-server:
container_name: productservice
image: productservice
expose:
- 8105
ports:
- 8105:8105
links:
- api-database
depends_on:
- api-database
networks:
- backend
api-database:
image:mongo
container_name: api-database
restart: always
hostname: api-database
networks: - backend
networks:
backend:
driver: "bridge"