Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker compose connecting mongodb container with node js container

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

like image 699
otto Avatar asked Nov 21 '18 13:11

otto


People also ask

How do I connect to a MongoDB Docker container?

For connecting to your local MongoDB instance from a Container you must first allow to accept connections from the Docker bridge gateway. To do so, simply add the respective gateway IP in the MongoDB config file /etc/mongod. conf under bindIp in the network interface section.

Can we connect MongoDB with node js?

To connect a Node. js application to MongoDB, we have to use a library called Mongoose. mongoose. connect("mongodb://localhost:27017/collectionName", { useNewUrlParser: true, useUnifiedTopology: true });

How do you communicate between containers in Docker compose?

For containers to communicate with other, they need to be part of the same “network”. Docker creates a virtual network called bridge by default, and connects your containers to it. In the network, containers are assigned an IP address, which they can use to address each other.


1 Answers

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
like image 172
greenPadawan Avatar answered Oct 26 '22 07:10

greenPadawan