8

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

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

This is my node docker compose:

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

2
  • Could you show your attempt to use docker network? Also, what do you mean by it didn't work? Commented Nov 24, 2018 at 9:42
  • i updated it. under the first answer you can read what does not work Commented Nov 24, 2018 at 12:42

3 Answers 3

6
+25

You can try something like this.

Create a docker network as follows.

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

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.

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

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-compose file as the sharelatex. But that's a whole another discussion
Sign up to request clarification or add additional context in comments.

3 Comments

this does not work... I updated the code my question how tried it: 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'm assuming you are running nginx as a separate service and not in a docker container. For the first point, I think you should run the nginx as a container as well. Just add it in your sharelatex compose fle. For the second point, could you please confirm whether you created a docker network separately using the docker network create <NETWORK_NAME> command?
Also, could you please confirm if you are running these containers on the same host or different hosts?
1

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

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

node 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 (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 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.

Comments

-1

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.com/compose/compose-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 recommend that you use user-defined networks to facilitate communication between two containers instead of using --link.

2 Comments

i think you dont understand my problem... Sharelatex mongo and redis works fine. Problem is i dont know how to connect mongo to node
i need a link in my node docker container to running mongo container

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.