Reggie Rughani
Reggie Rughani

Reputation: 1

Docker Containers Java Spring MVC and MongoDb

I have created two docker containers, one running MongodDb and one running Java Spring MVC on Tomcat 8. I have successfully linked the containers, but what I cannot understand is how you configure the java web app in one container to talk to mongodb in the other app.

Where do the connection string settings have to be setup in the java web app?

my Java Spring MVC app has the following applicationContext.xml settings

<mongo:mongo id="mongo" host="127.0.0.1" port="27017" />
<mongo:db-factory dbname="segmentation" />
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
       <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    </bean>
<mongo:repositories base-package="com.qantas.repository" />

when i run env in my web container I get

CON1MDB_PORT_27017_TCP_ADDR=172.17.0.10

CON1MDB_PORT_27017_TCP=tcp://172.17.0.10:27017

my DB container is running and has data that I can access from within the terminal and my web app is up and running. I just can't connect to the db from the web app.

Any help would be much appreciated

Upvotes: 0

Views: 1202

Answers (1)

Ervis Zyka
Ervis Zyka

Reputation: 506

In your applicationContext.xml file you need to set the host to host=con1mdb (the alias you provided when you used the --link flag).The database container has a different IP from the server container so you can't use localhost or 127.0.0.1.

If you run:

docker run -d --name mongodb mongo
docker run -d --name tomcatserver --link mongodb:database tomcat

and inspect the /etc/hosts in the tomcat container you will see something like:

172.17.0.2  database 0e504e1d7f79 mongodb

In the above scenario you should set host=database

Upvotes: 2

Related Questions