Mongo db - connection exception from another container in Docker - mongodb

In Jenkins I am trying to run in docker container my backend services, which are dependant on Mongo DB.
Currently I have it like this:
stage('Backend clone and build') {
steps { git ([url : 'https://github...', branch : 'develop', credentialsId : '***'])
sh 'docker pull mongo'
sh 'docker run -d -p 27017:27017 --name mongodb mongo'
sh 'docker build -t myimage2 .'
sh 'docker run myimage2'
}
}
After I run the build, I have this exception:
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.1.2.jar!/:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:143) ~[mongodb-driver-core-4.1.2.jar!/:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:188) ~[mongodb-driver-core-4.1.2.jar!/:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:144) ~[mongodb-driver-core-4.1.2.jar!/:na]
at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
I tried to link the container with mongodb container as : sh 'docker run --link mongodb:27017 myimage2', but no success.
I was also thinking to define the host via mongodb://localhost:27017 but this command is not accepted in docker. I did not find any solution how to get rid of that exception.
If I dive deeper to that exception, I also found there this:
Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect.
So for some reason it is not able to connect to localhost:27017 where the mongodb should be running.

To help others who may have this same problem, you have 2 options to set up correctly:
Create a docker network and start all the containers on this network so that the containers can discover each other, the default is the bridge network type. In this case, the containers are isolated from the host networking interface and can't be accessed via "localhost".
If you want the containers to run on the host network and be accessible via the host network interface aka localhost or a host IP, do as docker suggests in it's official docs here: https://docs.docker.com/network/host/

Related

Can't connect to MongoDB running in docker container

I run MongoDB in a docker container like this
docker run --name mongo -d -p 27017:27107 mongo
Check with docker ps shows
77f1a11295c3 mongo "docker-entrypoint.s…" 20 minutes ago Up 20 minutes 27017/tcp, 0.0.0.0:27017->27107/tcp mongo
so it's running with the port mapped correctly.
When I try to setup a connection via Intellij
it fails with
com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Exception receiving message}, caused by {java.net.SocketException: Connection reset}}].
When I try to connect to connect from a golang web server
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
client, err := mongo.Connect(context.TODO(), clientOptions)
I get this error and it shuts down the web server:
server selection error: server selection timeout, current topology: { Type: Unknown, Servers: [{ Addr: localhost:27017, Type: Unknown, Average RTT: 0, Last error: connection() error occured during connection handshake: connection(localhost:27017[-64]) incomplete read of message header: read tcp 127.0.0.1:40700->127.0.0.1:27017: read: connection reset by peer }, ] }
Is this a bug in the MongoDB docker image, or is there something else I need to do?
I think you have a typo mistake while creating a container. 27017 and 27107
docker run --name mongo -d -p 27017:27017 mongo
This is clearly visible in docker ps command.
Can you try and see if it solves the problem ?
As #fly2matrix said you have a typo, and I believe you have another issue:
you forward the ports 27017:27107, in which the container port is incorrect. Check 27107 instead of 27017
It seems you're connecting to localhost instead of mongo as I see the phrase localhost in your code.

opsmanager application not able to connect to opsmanager database

I have installed opsmanager database in one instance and application in other instance
Taken points from this link
In application database server when i netstat -nltp it shows 127.0.0.1:27017 running mongod
After installing application in other instance I edit the mongo.mongoUri as mongodb://db_instance_publicip:27017 in /opt/mongodb/mms/conf/conf-mms.properties
When I start by sudo service mongodb-mms start is shows below error
uri=mongodb://db_instance_publicip:27017/?maxPoolSize=150} Error: Timed out after 30000 ms while waiting to connect.......
Error: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=db_instance_publicip:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]
Was facing the same issue. The issue in my case was that my Mongo DB was binding to only localhost thanks to this line in /etc/mongod.conf:
bindIp: 127.0.0.1 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
As the commented part in the same line suggests, I had to replace "127.0.0.1" with "0.0.0.0" and restart Mongo DB and it started working!

Not able to acsess existing MongoDb from Spring Boot App Deploy in Docker Container

I have been reading all sort of answers to connect my local MongoDB from Spring boot app which is deployed in Docker Container.
But I am getting below error:
2020-04-24 06:39:29.425 INFO 1 --- [ main] org.mongodb.driver.cluster : Adding discovered server 127.0.0.1:27017 to client view of cluster
2020-04-24 06:39:29.542 INFO 1 --- [127.0.0.1:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server 127.0.0.1:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:67) ~[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_252]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_252]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_252]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_252]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_252]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_252]
at java.net.Socket.connect(Socket.java:607) ~[na:1.8.0_252]
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]
... 3 common frames omitted
2020-04-24 06:39:30.833 INFO 1 --- [ main] pertySourcedRequestMappingHandlerMapping : Mapped URL path [/v2/api-docs] onto method [public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)]
If I run this spring boot app out of docker container, it works fine.
But from inside docker container its not able to access my mongoDB which is outside of docker container.
This how I am running docker container:
docker build -f .\Dockerfile -t dockerspringboot .
docker run -p 8085:8080 dockerspringboot
This is my Dockerfile:
FROM openjdk:8
ADD build/libs/SpringBoot-1.0.jar SpringBoot-1.0.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "SpringBoot-1.0.jar"]
-I tried changing ipaddress to 0.0.0.0 and 127.0.0.1, but of no use.
-I tried changing mondod.conf with below commented/uncommented options:
# network interfaces
net:
#port: 27017
bindIp: 0.0.0.0
# bindIpAll: true
# bindIp: 127.0.0.1
-I tried changing firewall settings to allow all programs access to docker backend process as well.
Also tried changing local ipaddresses by using ipconfig command but still no luck.
You should specify the authorizations to access the database if they exist, try this :
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.database=app1
spring.data.mongodb.host=host.docker.internal
spring.data.mongodb.port=27017
spring.data.mongodb.password=PASSWORD
spring.data.mongodb.username=USERNAME
Add --network="host" to your docker run command. Then your docker container is able to access localhost:27017

Mongodb 3.4 - fail to start mongodb

How can I start mongodb. The child process fail.
-bash-4.1$ mongod --config /mongodb/etc/mongod-ctr.conf
about to fork child process, waiting until server is ready for connections.
forked process: 22965
ERROR: child process failed, exited with error number 48
-bash-4.1$ mongo
MongoDB shell version v3.4.17
connecting to: mongodb://127.0.0.1:27017
2018-11-27T13:52:13.826-0500 W NETWORK [thread1] Failed to
connect to 127.0.0.1:27017, in(checking socket for error after
poll), reason: Connection refused
According to the MongoDB error code manual, and your error status, there seems to be another process either running on port 27017, or you just don't have the right permissions.
You can use the command lsof -i -P -n to see a list of all applications, and the ports they are using.

Shutdown mongos for upgrade

I am upgrading a mongo sharded cluster, and am in the first step of replacing the mongos process. Can I just kill this process or is there a safer way to shut it down before replacing it?
It is the answer. But may be you misunderstood. You cannot shutdown mongos from command line, but you have to logon to the shell. Here is the result I am trying on my own:
$ mongo --port 27077 (please replace with your own port on mongos instance)
**mongos**> db.shutdownServer({timeoutSecs:30})
shutdown command only works with the admin database; try 'use admin'
mongos> use admin
switched to db admin
mongos> db.shutdownServer({timeoutSecs:30})
2015-02-27T12:53:54.408+0800 DBClientCursor::init call() failed
**server should be down...**
2015-02-27T12:53:54.410+0800 trying reconnect to 127.0.0.1:27077 (127.0.0.1) failed
2015-02-27T12:53:54.410+0800 warning: Failed to connect to 127.0.0.1:27077, reason: errno:111 Connection refused
2015-02-27T12:53:54.410+0800 reconnect 127.0.0.1:27077 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:27077 (127.0.0.1), connection attempt failed
2015-02-27T12:53:54.413+0800 trying reconnect to 127.0.0.1:27077 (127.0.0.1) failed
2015-02-27T12:53:54.413+0800 warning: Failed to connect to 127.0.0.1:27077, reason: errno:111 Connection refused
2015-02-27T12:53:54.413+0800 reconnect 127.0.0.1:27077 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:27077 (127.0.0.1), connection attempt failed
>
Try to run "top" or "htop" to display all processes running on your computer. You shall see the "mongos" process has gone. Read the log file. Here is the last two lines of my log:
2015-02-27T12:53:54.406+0800 [conn1] terminating, shutdown command received
2015-02-27T12:53:54.406+0800 [conn1] dbexit: shutdown called rc:0 shutdown called
And here is the link of my testing script:
https://github.com/babycaseny/QuickStart/blob/master/StartShard.sh
Note that you have to replace the "localhost" in the mongod/mongos command with the hostname of your computer, or you will not be able to config your shards.
See also this one:
https://groups.google.com/forum/#!topic/mongodb-user/TQLlRI6HG1M
In case you need a command line to do the work, here is one:
mongo admin --port portnumber --eval "db.shutdownServer()"
Notice that you have to run the command in localhost.