简体   繁体   English

Docker:将Spring Boot容器与Mongo DB容器链接

[英]Docker: Linking Spring Boot container with Mongo DB container

How can I link my Spring Boot application container with MongoDB container? 如何将Spring Boot应用程序容器与MongoDB容器链接?
Spring Boot app is using MongoDBRespository which by default connects to localhost:27017. Spring Boot应用程序使用的是MongoDBRespository,默认情况下它连接到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. 当两个容器链接在一起时,Docker将在目标容器中设置一些环境变量,以启用以编程方式发现与源容器相关的信息。

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 ). 您的MongoDB的容器应该揭露一些端口(通过EXPOSE在Dockerfile或通过进入-p选项docker run )。
  • your Spring Boot-container should be started with the --link option that points to the MongoDB-container. 您的Spring Boot容器应使用--link选项启动,该选项指向MongoDB容器。
  • The MongoDBRepository should be configured to use the address that is provided in the environment variables by the linking. 应该将MongoDBRepository配置为使用通过链接在环境变量中提供的地址。

Check out this article on how to link containers for more info. 查看有关如何链接容器的本文获取更多信息。

@Himanshu Yadav , you can try this resource. @Himanshu Yadav,您可以尝试此资源。 it did solve my problem. 它确实解决了我的问题。 :-) It has got a full tutorial in that regard :-)在这方面有完整的教程

https://www.jiwhiz.com/blogs/Spring_Boot_Docker_MySQL_Demo 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. 我想知道您是否通过链接容器使它起作用,但对我却不起作用,请尝试在我的Springboot应用程序中使用链接的容器别名作为dbhost名称。 I did not try it as MongoClientURI though. 我没有尝试将其作为MongoClientURI

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 我确实选择了mongodb容器和spring应用程序容器,但必须将mongo容器的主机和端口设置为spring应用程序容器,同时将应用程序容器创建为Containers ENV变量,如下所示

Mongo container Mongo容器

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

SpringBoot Apps containers SpringBoot Apps容器

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 application.properties db.host.port=27017 db.host.name=localhost

MongoConfig 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. 由于不建议使用docker链接 ,因此可以使用用户定义的网络来实现容器之间的通信。

Referring to Docker Networking , docker has 3 different types of networks - Bridge, Host, Network. 提到Docker NetworkingDocker具有3种不同类型的网络-网桥,主机,网络。 Using docker network ls you can list all networks currently defined. 使用docker network ls您可以列出当前定义的所有网络。 By default docker comes with 1 for each type pre-configured. 默认情况下,docker随附每种预配置类型的1。

While running your container, one can specify( --network=abcd ) network the process will join - by default its the docker0 of type bridge. 在运行容器时,可以指定( --network=abcd )该进程将加入的网络-默认情况下,其类型为bridge的docker0。

Now, for the problem statement in the question here, the simplest approach is to use --network=host while launching mongo & spring-boot-app container. 现在,对于此处问题的陈述,最简单的方法是在启动mongo和spring-boot-app容器时使用--network = host。

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

Then in your Spring boot app, you will have something like this:- 然后在您的Spring Boot应用程序中,您将获得以下内容:-

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 :- 使用以下命令从映像运行spring boot应用程序:

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. 然后,您可以使用/到达mongo&app实例,就像它们在没有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) (在Windows上使用docker-toolbox时,您实际上是在具有不同IP的VM内运行docker-我发现它是192.168.99.100。因此请记住使用此ip而不是您的本地ip)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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