skip to Main Content

I have built three docker images of node, elastic search, and mongo. Then pushed those to docker hub. after pulling those images, I am running them using the command docker run. They are running successfully. But the node app is not being able to connect to mongo or elastic. I have tried creating a network and adding the containers to it, but it doesn’t help. Can someone help regarding the issue?

2

Answers


  1. It depends on where the individual containers are running. If they are running on the same host, you can connect them to the same bridge network.
    Refer here: https://docs.docker.com/network/bridge/

    If they are running on different host, you would have to use host networking and expose the relevant ports. Refer here: https://docs.docker.com/network/host/

    Login or Signup to reply.
  2. when working with multiple containers it’s better to use Docker compose.
    you can create a docker-compose.yml file defining services(your containers) there.

    Here is an example docker-compose.yml for node,mongo and elasticsearch

    version: "3"
    services:
    backend:
        container_name: nodejs
        restart: always
        build: node
        ports:
        - "3010:3010"
    
        volumes:
        - .:/app
        - ./error.log:/usr/src/app/error.log
        links:
        - mongo
        - elasticsearch
    mongo:
        container_name: mongo
        image: mongo
        ports:
        - "27017:27017"
        volumes:
        - ./data:/data/db
    elasticsearch:
        container_name: elasticsearch
        image: docker.elastic.co/elasticsearch/elasticsearch:7.0.0
        environment:
        - node.name=es01
        - discovery.type=single-node
        - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
        volumes:
        -  esdata:/usr/share/elasticsearch/data
        ports:
        - "9200:9200"    
    volumes:
    esdata:
        driver: local
    
    

    Your question is not clear so I cant answer more than that

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search