简体   繁体   English

docker用节点js容器组合连接mongodb容器

[英]docker compose connecting mongodb container with node js container

i have a sharelatex container running. 我有一个运行的sharelatex容器。 This docker compose file contains a mongo and redis container. 此docker撰写文件包含mongo和redis容器。

Here is the sharelatex docker compose : 这是sharelatex docker compose

version: '2'
services:
    sharelatex:
        restart: always
        image: rigon/sharelatex-full
        #image: sharelatex/sharelatex
        container_name: sharelatex
        networks:
            - test-network
        depends_on:
            - mongo
            - redis
        privileged: true
        links:
            - mongo
            - redis
        volumes:
            - ./sharelatex_data:/var/lib/sharelatex
            - /var/run/docker.sock:/var/run/docker.sock
        environment:
            SHARELATEX_MONGO_URL: mongodb://mongo/sharelatex
            SHARELATEX_REDIS_HOST: redis
            SHARELATEX_APP_NAME: ShareLaTeX
            SHARELATEX_SITE_URL: https://latex.tkwant.de

   mongo:
        restart: always
        image: mongo
        container_name: mongo

        expose:
           - 27017
        volumes:
            - ./mongo_data:/data/db
        networks:
            - test-network
    redis:
        restart: always
        image: redis
        container_name: redis

        networks:
            - test-network
        expose:
            - 6379
        volumes:
            - ./redis_data:/data

networks:
    test-network:
      external: true

I want to create a node application which needs mongodb, too. 我想创建一个需要mongodb的节点应用程序。 How can i connect these two container? 我该如何连接这两个容器? I read about network and tried out docker network but without success. 我读了关于网络并尝试了docker网络但没有成功。

This is my node docker compose : 这是我的节点docker撰写

version: '3.5'
services:
  app:
    container_name: app
    restart: always
    build: .
    ports:
      - '3001:3000'
    networks:
      - test-network
networks:
    test-network:
      driver: external

and here my index.js : 在这里我的index.js

// Connect to MongoDB
mongoose
  .connect(
    'mongodb://mongo:27017/test2',
    { useNewUrlParser: true }
  )
  .then(() => console.log('MongoDB Connected'))
  .catch(err => console.log("errorErrorError"));

I am open for all answers... running mongo in an own container or create a docker network. 我对所有答案持开放态度......在自己的容器中运行mongo或创建一个docker网络。 But I dont know what is the best or the easiest. 但我不知道什么是最好的或最简单的。

Update 1: 更新1:

First Problem: Sharelatex does not work anymore --> sharelatex is now in another network. 第一个问题:Sharelatex不再起作用了 - > sharelatex现在在另一个网络中。 My nginx reverse proxy does not find sharelatex container anymore. 我的nginx反向代理不再找到sharelatex容器了。 Second Problem is: when i want to start node docker i get this error (dont know why but it want to create a new network): Creating network "dockernodemongo_test-network" with driver "external" ERROR: plugin "external" not found 第二个问题是:当我想启动节点docker我得到这个错误(不知道为什么但它想要创建一个新网络):用驱动程序“external”创建网络“dockernodemongo_test-network”错误:找不到插件“external”

You can try something like this. 你可以尝试这样的事情。

Create a docker network as follows. 按如下方式创建docker网络。

docker network create <NETWORK_NAME>

In your sharelatex docker compose , you can add this network (test-network is the name of the network) like this 在您的sharelatex docker compose中 ,您可以像这样添加此网络(test-network是网络的名称)

services:
    mongo:
        restart: always
        image: mongo
        container_name: mongo
        networks:
           - test-network
        expose:
           - 27017
        volumes:
            - ./mongo_data:/data/db

networks:
    test-network:
      external: true

Similarly, you can do the same (use same network name) in the docker compose file for your node application. 同样,您可以在节点应用程序的docker compose文件中执行相同的操作(使用相同的网络名称)。

Once both the containers are in the same network, you can use the container name as the host name to connect to it. 一旦两个容器位于同一网络中,您就可以使用容器名称作为主机名来连接它。

Additionally, you can verify if the containers are running in the same network using the following command 此外,您可以使用以下命令验证容器是否在同一网络中运行

docker network <NETWORK_NAME> inspect

PS PS

  1. You should use environment variables to pass the mongo host name and port to your node application 您应该使用环境变量将mongo主机名和端口传递给节点应用程序
  2. You can put your node application in the same docker-compose file as the sharelatex. 您可以将节点应用程序放在与sharelatex相同的docker-compose文件中。 But that's a whole another discussion 但这是另一个讨论

Ok so it seems you're specifying a custom network on both instances but you're not actually naming them. 好的,似乎你在两个实例上都指定了一个自定义网络,但实际上并没有命名它们。 The title test-network can only be used to reference it from within that same file. 标题测试网络只能用于从同一文件中引用它。

networks:
  test-network:
    external: true

This will effectively create a custom network but with no specified name it creates a default name of [projectname]_test-network. 这将有效地创建自定义网络,但没有指定名称,它会创建默认名称[projectname] _test-network。 You're effectively creating two different networks with [projectname]_test-network which is why it is trying to create a "dockernodemongo_test-network" network. 您正在使用[projectname] _test-network有效地创建两个不同的网络,这就是它尝试创建“dockernodemongo_test-network”网络的原因。

On your node docker-compose you can try: 在您的节点docker-compose上,您可以尝试:

networks:
  test-network:
    external:
      name: [sharelatexname]_test-network

This will effectively search for a pre-existing network with that name. 这将有效地搜索具有该名称的预先存在的网络。 Alternatively you can name the network from the first instance it is created and that should save you the trouble of trying to figure out the name. 或者,您可以从创建它的第一个实例命名网络,这样可以省去尝试找出名称的麻烦。

sharelatex docker-compose: sharelatex docker-compose:

networks:
  test-network:
    name: test-network
    external: true

node docker-compose: 节点docker-compose:

networks:
  test-network:
    external:
      name: test-network

As for why it is not creating the node network; 至于为什么它没有创建节点网络; "driver:" you have no existing plug-in called 'external' there a few built in drivers that add a number of capabilities (eg multi-hosting) to your network such as bridge, overlay and macvlan. “驱动程序:”你没有一个名为“外部”的现有插件,其中有一些内置驱动程序可以为你的网络添加许多功能(例如多主机),例如bridge,overlay和macvlan。 You can also download other custom plug-ins. 您还可以下载其他自定义插件。 I don't believe you need these for what you're trying to accomplish however. 我不相信你需要这些才能达到你想要完成的目标。 Also since you're only using one network, all instances of "networks:" within the services are unnecessary. 此外,由于您只使用一个网络,因此服务中的所有“网络:”实例都是不必要的。 They will all be a part of the only specified network. 它们都将成为唯一指定网络的一部分。 The "networks:" will be useful if you have multiple networks on the same docker-compose and wish to isolate/denominate networks for specific services within it. 如果您在同一个docker-compose上有多个网络并希望隔离/命名网络中的特定服务,那么“networks:”将非常有用。

You can try this. 你可以试试这个。

version: '2'
services:
    mongo: // declare first
        restart: always
        image: mongo
        container_name: mongo
        expose:
           - 27017
        volumes:
            - ./mongo_data:/data/db

    redis: // declare first
        restart: always
        image: redis
        container_name: redis
        expose:
            - 6379
        volumes:
            - ./redis_data:/data

    sharelatex:
        restart: always
        image: rigon/sharelatex-full
        #image: sharelatex/sharelatex
        container_name: sharelatex
        depends_on:
            - mongo
            - redis
        privileged: true
        links:
            - mongo:mongo // added :mongo
            - redis:redis // added :redis
        volumes:
            - ./sharelatex_data:/var/lib/sharelatex
            - /var/run/docker.sock:/var/run/docker.sock
        environment:
            SHARELATEX_MONGO_URL: mongodb://mongo/sharelatex
            SHARELATEX_REDIS_HOST: redis
            SHARELATEX_APP_NAME: ShareLaTeX
            SHARELATEX_SITE_URL: https://latex.tkwant.de

If you declare mongo and redis after you try linking to them, docker won't know it exists. 如果在尝试链接后声明mongo和redis,则docker将不知道它存在。

Source: https://docs.docker.com/compose/compose-file/#links 资料来源: https//docs.docker.com/compose/compose-file/#links

Do note that the official docs state that: The --link flag is a legacy feature of Docker. 请注意 ,官方文档声明:-- --link标志是Docker的遗留功能。 It may eventually be removed. 它最终可能被删除。 Unless you absolutely need to continue using it, we recommend that you use user-defined networks to facilitate communication between two containers instead of using --link . 除非您绝对需要继续使用它,否则我们建议您使用用户定义的网络来促进两个容器之间的通信,而不是使用--link

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

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