How can I link my Spring Boot application container with MongoDB container?
Spring Boot app is using MongoDBRespository which by default connects to localhost:27017.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- MongoDB can not create unique sparse index (duplic
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
I wonder if you got this to work by linking containers, it didn't work for me, tried using a linked container alias as a dbhost name in my Springboot app. I did not try it as
MongoClientURI
though.I did opt a work around, with mongodb containers and spring apps containers, I had to set up mongo containers host and port to spring app containers while creating the apps containers as Containers ENV variables , as shown below
Mongo container
SpringBoot Apps containers
application.properties
db.host.port=27017 db.host.name=localhost
MongoConfig
You should use container linking. From the docs:
Basically what this means is the following
EXPOSE
entry in the Dockerfile or via the-p
option todocker run
).--link
option that points to the MongoDB-container.MongoDBRepository
should be configured to use the address that is provided in the environment variables by the linking.Check out this article on how to link containers for more info.
@Himanshu Yadav , you can try this resource. it did solve my problem. :-) It has got a full tutorial in that regard
https://www.jiwhiz.com/blogs/Spring_Boot_Docker_MySQL_Demo
Quote
As docker links are now deprecated, one can use user defined networks to achieve communication between containers.
Referring to Docker Networking, docker has 3 different types of networks - Bridge, Host, Network. Using
docker network ls
you can list all networks currently defined. By default docker comes with 1 for each type pre-configured.While running your container, one can specify(
--network=abcd
) network the process will join - by default its the docker0 of type bridge.Now, for the problem statement in the question here, the simplest approach is to use --network=host while launching mongo & spring-boot-app container.
Then in your Spring boot app, you will have something like this:-
Run you spring boot app from image using :-
You can then use/reach mongo & app instances as if they were run without docker. (While using docker-toolbox on Windows, you are actually running docker inside a VM that has different IP - I found it to be 192.168.99.100 . So remember to use this ip instead of your local ip)