简体   繁体   中英

Docker: Linking Spring Boot container with Mongo DB container

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.

You should use container linking . From the docs :

When you set up a link, you create a conduit between a source container and a recipient container. The recipient can then access select data about the source

When two containers are linked, Docker will set some environment variables in the target container to enable programmatic discovery of information related to the source container.

Basically what this means is the following

  • your MongoDB-container should expose some ports (either via the EXPOSE entry in the Dockerfile or via the -p option to docker run ).
  • your Spring Boot-container should be started with the --link option that points to the MongoDB-container.
  • The 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

docker run -p 8080:8080 --name demo-app --link demo-mysql:mysql -d jiwhiz/spring-boot-docker-mysql

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

docker run -d -p 27027:27017 -p 28027:28017  --name mongodb  --volumes-from dbdata iamiddy/mongodb 

SpringBoot Apps containers

docker run -d -p 8000:8080 --name AppDockerContainer 
-e db.host.name=EC2-HOSTING-MONGO-CONTAINER
-e db.host.port=DB-HOST-PORT AppDockerImage 

application.properties db.host.port=27017 db.host.name=localhost

MongoConfig

public class MongoRepositoryConfig extends AbstractMongoConfiguration { 

    @Value("${db.host.port}")
    private int port;

    @Value("${db.host.name}")
    private String host;

    @Value("${db.name}")
    private String dbname;

    @Override
    public Mongo mongo() throws Exception {
        ServerAddress serverAdress = new ServerAddress(host,port);
        Mongo mongo = new MongoClient(serverAdress);
        mongo.setWriteConcern(WriteConcern.ACKNOWLEDGED);
        return mongo;
    }

    public @Bean MongoTemplate mongoTemplate() throws Exception {
        return new MongoTemplate(mongo(), getDatabaseName());
    }

    @Override
    protected String getDatabaseName() {
        return dbname;
    }
}

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.

docker run -d -P --name=my-mongo --network=host mongo:latest

Then in your Spring boot app, you will have something like this:-

spring:
  application:
    name: my-app
  data:
    mongodb:
      database: app
      host: 192.168.99.100 // your machine private ip.
      port: 27017

Run you spring boot app from image using :-

docker run -d -P --name=my-boot-app --network=host my-app-image

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM