How to connect nodeJS docker container to mongoDB

2019-03-21 10:44发布

I have problems to connect a nodeJS application which is running as a docker container to a mongoDB. Let me explain what I have done so far:

$ docker ps

CONTAINER ID    IMAGE        COMMAND                  CREATED        STATUS        PORTS        NAMES
3a3732cc1d90    mongo:3.4    "docker-entrypoint..."   3 weeks ago    Up 3 weeks    27017/tcp    mongo_live

As you can see, there is already a mongo docker container running.

Now I'm running my nodeJS application docker container (which is a build from meteorJS):

$ docker run -it 0b422defbd59 /bin/bash

In this docker container I want to run the application by running:

$ node main.js

Now I'm getting the error

Error: MONGO_URL must be set in environment

I already tried to set MONGO_URL by setting:

ENV MONGO_URL mongodb://mongo_live:27017/

But this doesn't work:

MongoError: failed to connect to server [mongo_live:27017] on first connect

So my question is how to connect to a DB, which is - as far as I understand - 'outside' of the running container. Alternativly how do I set up a new DB to this container?

3条回答
孤傲高冷的网名
2楼-- · 2019-03-21 11:02

There are couple of ways to do it.

  • run your app in the same network as your mongodb:

    docker run --net container:mongo_live your_app_docker_image
    
    # then you can use mongodb in your localhost
    $ ENV MONGO_URL mongodb://localhost:27017/
    
  • Also you can link two containers:

    docker run --link mongo_live:mongo_live you_app_image ..
    # Now mongodb is accessible via mongo_live
    
  • use mongodb container ip address:

    docker inspect -f '{{.NetworkSettings.IPAddress}}' mongo_live
    # you will get you container ip here
    
    $ docker run -it 0b422defbd59 /bin/bash
    # ENV MONGO_URL mongodb://[ip from previous command]:27017/
    
  • You can bind your mongodb port to your host and use host's hostname in your app

  • You can use docker network and run both apps in the same network

  • You could pass --add-host mongo_live:<ip of mongo container> to docker run for your application and then use mongo_live for mongodb url

  • You can also use docker compose to make your life easier ;)

...

查看更多
smile是对你的礼貌
3楼-- · 2019-03-21 11:08

Please use below snippet for your docker-compose.yml file, replace comments with your actuals. Should solve your problem.

version: '2'
    services:
      db:
        build: <image for mongoDB>
        ports:
          - "27017:27017"  # whatever port u r using
        environment:
          #you can specify mondo db username and stuff here
        volumes:
          - #load default config for mondodb from here
          - "db-data-store:/data/db" # path depends on which image you use
        networks:
          - network
     nodejs:
        build: #image for node js
        expose:
          - # mention port for nodejs
        volumes:
          - #mount project code on container
        networks:
          - network
        depends_on:
          - db

networks:
  network:
    driver: bridge

Please use the below links for references :
1) NodeJs Docker
2) MongoDb docker
3) docker-compose tutorial

Best of Luck

查看更多
Luminary・发光体
4楼-- · 2019-03-21 11:10

When you run containers each container works in independent network. Because one container cant connect to other point to point.

The are 3 ways to connect containers

  1. Have a little fuss with low-level docker network magic
  2. Connect container through localhost. Each container must expose ports on localhost (as your mongo_live). But you need add to host ile on localhost 127.0.0.1 mongo_live (This is the simplest way)
  3. Use docker-compose. It convenient tool for working many containers together. (This is right way)

Add mongodb to application container is not docker way.

查看更多
登录 后发表回答