admin管理员组

文章数量:1405553

i have a sharelatex container running. This docker pose file contains a mongo and redis container.

Here is the sharelatex docker pose:

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: 

   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. How can i connect these two container? I read about network and tried out docker network but without success.

This is my node docker pose:

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:

// 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. But I dont know what is the best or the easiest.

Update 1:

First Problem: Sharelatex does not work anymore --> sharelatex is now in another network. My nginx reverse proxy does not find sharelatex container anymore. 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

i have a sharelatex container running. This docker pose file contains a mongo and redis container.

Here is the sharelatex docker pose:

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. How can i connect these two container? I read about network and tried out docker network but without success.

This is my node docker pose:

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:

// 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. But I dont know what is the best or the easiest.

Update 1:

First Problem: Sharelatex does not work anymore --> sharelatex is now in another network. My nginx reverse proxy does not find sharelatex container anymore. 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

Share Improve this question edited Nov 24, 2018 at 12:41 otto asked Nov 21, 2018 at 13:40 ottootto 2,0638 gold badges42 silver badges69 bronze badges 2
  • Could you show your attempt to use docker network? Also, what do you mean by it didn't work? – greenPadawan Commented Nov 24, 2018 at 9:42
  • i updated it. under the first answer you can read what does not work – otto Commented Nov 24, 2018 at 12:42
Add a ment  | 

3 Answers 3

Reset to default 6 +25

You can try something like this.

Create a docker network as follows.

docker network create <NETWORK_NAME>

In your sharelatex docker pose, you can add this network (test-network is the name of the network) like this

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 pose file for your node application.

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 mand

docker network <NETWORK_NAME> inspect

P.S.

  1. You should use environment variables to pass the mongo host name and port to your node application
  2. You can put your node application in the same docker-pose file as the sharelatex. 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. You're effectively creating two different networks with [projectname]_test-network which is why it is trying to create a "dockernodemongo_test-network" network.

On your node docker-pose you can try:

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-pose:

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

node docker-pose:

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 (e.g. multi-hosting) to your network such as bridge, overlay and macvlan. You can also download other custom plug-ins. I don't believe you need these for what you're trying to acplish 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-pose and wish to isolate/denominate networks for specific services within it.

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.

Source: https://docs.docker./pose/pose-file/#links

Do note that the official docs state that: The --link flag is a legacy feature of Docker. It may eventually be removed. Unless you absolutely need to continue using it, we remend that you use user-defined networks to facilitate munication between two containers instead of using --link.

本文标签: javascriptdocker compose connecting mongodb container with node js containerStack Overflow