2

I am running nodejs application in docker. In that application I am trying to connect my system database. But It is not working

In my environment file:

**MONGODB_URI=mongodb://192.168.1.174:27017/sampleDB**
SESSION_SECRET=sample
App_PORT = 8088

But I am getting error and unable to access the db.

My application is running on docker machine ip 192.168.99.100:8088

Here, I attached my docker running command statement: enter image description here How to connect my system db into that application

N15
  • 305
  • 2
  • 4
  • 17

1 Answers1

0

The IP depends how the containers are run. If using docker-compose, it creates a network for you in which containers are accessible to themselves using service name (eg. db should you use it). If not, and you did not specify any network, the default bridge network is used (called docker0 on your docker machine).

Since you are running containers separately (using docker run), you have to either give specific IP address to the container (you can get one from inside the container using docker exec container_name ip a) or connect to it via the gateway (your docker machine). However, in order to do that, the database port has to be exposed (eg. 27017:27017 when running).

I recommend you start using docker-compose from now on, many things will get easier when running a stack of linked containers.

Mike Doe
  • 16,349
  • 11
  • 65
  • 88
  • I am running my container using docker run -d 8088:8088 nitikishu/samplenodejs.. application is running on that docker machine default ip address 192.168.99.100:8088 from this link my application is running. But I can not able to login to the application system. In my application I try to connect another system database that system ip 192.168.1.174 . It is not connecting to that sytem – N15 Jan 22 '18 at 10:40
  • That's a different network so it looks like a firewall issue inside the docker machine. Like I said, start using `docker-compose` or create a docker network and make these containers join that network. Or start using Linux, Docker on Windows sucks like hell. – Mike Doe Jan 22 '18 at 10:47
  • But If I run the nodejs application in my localhost:8088 it will run and connecting with others system db(192.168.1.174). Samething I am trying to do it in docker. It is not happening. I have built image for the application with environment variable :mongodb-uri=monogdb://192.168.1.174:27017/sampleDB port 8088. when I run the image in docker like docker run -d 8088:8088 nitikishu/samplenodejs i am getting error like as I shared in screenshot image. Docker machine ip 192.168.99.100:8088 from this url only that image is running. it is not running in my localhost after pulling and run in docker – N15 Jan 22 '18 at 10:55