Reputation: 3495
I have an sbt project which has mongodb
code i am making a custom image of this sbt project and using mongodb:2.6.11
public docker image https://hub.docker.com/_/mongo
here is the docker file of my sbt project
FROM hseeberger/scala-sbt:11.0.2_2.12.8_1.2.8 as stripecommon
MAINTAINER sara <[email protected]>
WORKDIR /myproject
ADD myprojectcode .
CMD ["sbt","reload","clean","compile","run"]
and here is the dockerfile
of the mongodb image mongodb:2.6.11 https://github.com/ky13R/mongo-2.6.11-docker-image/blob/master/Dockerfile
here is how i am running my containers
docker create network mynetwork
to start mongodb container
docker run -p 27017 --network mynetwork mongo:2.6.11
to start my project container
docker run --network mynetwork myproject:v1
and my project is unable to connect to the mongodb server here is the exception log
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:63)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:114)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:128)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
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:591)
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50)
at com.mongodb.connection.SocketStream.open(SocketStream.java:58)
... 3 common frames omitted
i have followed this tutorial to connect to mongodb server https://dzone.com/articles/using-docker-to-shove-an-existing-application-into what i am doing wrong here?
the code for connecting to mongo server is
private val DATABASE:String = config.getString("db.dbname")
private val SERVER:ServerAddress = {
val hostName=config.getString("db.hostname")
val port=config.getString("db.port").toInt
new ServerAddress(hostName,port)
}
val connectionMongo = MongoClient(SERVER)
log.debug("mongo connection created "+connectionMongo)
def collectionMongo(name:String) = connectionMongo(DATABASE)(name)
log.debug("getting collection from mongodb")
}
and here is the config file
db.url="mongodb://Localhost:27017"
db.hostname="Localhost"
db.dbname=testdb
db.port=27017
Upvotes: 3
Views: 4795
Reputation: 669
The Problem is that you are using localhost
for db.url
and db.hostname
. Localhost referred to the Docker Container of your application is inside the container, not your host machine. What you can do is starting the MongoDB container with a concrete name, like this:
docker run -p 27017 --name mongodb --network mynetwork mongo:2.6.11
And use the following config within your application:
db.url="mongodb://mongodb:27017"
db.hostname="mongodb"
db.dbname=testdb
db.port=27017
Within your docker network (mynetwork
), the database container is reachable by it's container name (here mongodb
)
Hope I can help.
Edit:
Another (easier) way to solve this is to "link" the containers. By doing so, you don't need to create a network beforehand.
How to do it?
Start your mongo database:
docker run -p 27017 --name mongodb mongo:2.6.11
Start your application and link to the mongo-db container, previously created:
docker run --link mongodb myproject:v1
Upvotes: 2