15

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?

4 Answers 4

22

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 ;)

...

Sign up to request clarification or add additional context in comments.

3 Comments

I am using docker compose, but not for this case. Let me explain: Here it is part of a CI workflow. Therefore I'm running docker build using a dockerfile: docker build -t $CI_REGISTRY_IMAGE:test -f Dockerfile.test .. So I don't know how to use docker compose in this setting...
Docker compose can also build an image, You can also call run with compose which may help to run tests. There is also --abort-on-container-exit option which is also useful to run tests in CI. for example: github.com/mobilityhouse/prometheus-bridge/blob/master/…
I have posted a docker-compose solution below, i am sure u can use that as reference and figure it out
1

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.

Comments

0

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

Comments

0

I had problem how to connect my server.js to mongodb. And that's how i solved it hope you find it useful. Tap For My Screenshot

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

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.