How to connect container-docker-node nodeJS to mongoDB
I'm having trouble connecting a nodeJS app that works like a docker container in 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 docker-mango container running.
Now I run the nodeJS app host container (which is an assembly from meteorJS):
$ docker run -it 0b422defbd59 /bin/bash
In this docker container, I want to run my application by executing:
$ node main.js
Now I am getting the error
Error: MONGO_URL must be set in environment
I already tried to install 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 the database, which I understand is "outside" the running container. Alternatively, how do I set up a new DB in this container?
source to share
There are several ways to do this.
-
run your app on 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/
-
You can also link two containers:
docker run --link mongo_live:mongo_live you_app_image .. # Now mongodb is accessible via mongo_live
-
use ip address of mongodb container:
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 the hostname of the host in your application
-
You can use
docker network
and run both applications on the same network -
You can pass
--add-host mongo_live:<ip of mongo container>
to run docker for your app and then usemongo_live
mongodb for url -
You can also use docker comp to make your life easier;)
...
source to share
When containers are launched, each container runs on an independent network. Since one container cannot connect to another point to point.
There are three ways to connect containers
- A little fuss with low-level docker network magic
- Connect the container via localhost. Each container should expose ports to localhost (like your mongo_live). But you need to add to host ile on localhost
127.0.0.1 mongo_live
( This is the easiest way ) - Use docker-compose . It is a handy tool for multi-container collaboration. (The right way )
Add mongodb to application container is not docker.
source to share
Please use below snippet of docker-compose.yml file, replace comments with your actions. 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 links below for links:
1) NodeJs Docker
2) MongoDb docker
3) docker compilation tutorial
Best of luck
source to share