3
votes

I have a problem on linking 2 containers in development environment with Docker.

I have already my mongo container running calls db.

Here my Dockerfile for my Node app :

FROM nodesource/node:4.0

ADD package.json package.json  
RUN npm install  
ADD . .
ENV NODE_ENV development

CMD ["node","server.js"]

After building the the image I run it inside a linking container with the following command :

docker run --name myapp --link db:db_1 -p 80:3000 -d myapp

Here my two containers :

CONTAINER ID IMAGE COMMAND
CREATED STATUS PORTS
NAMES 216304df0905 myapp "node server.js"
25 minutes ago Exited (1) 13 minutes ago
myapp 506f9fcfd30b mongo "/entrypoint.sh mongo"
19 hours ago Up 19 hours 27017/tcp db

myapp logs :

development

Server running at http://localhost:3000

/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/server.js:242 process.nextTick(function() { throw err; }) ^ Error: connect ECONNREFUSED 127.0.0.1:27017 at Object.exports._errnoException (util.js:837:11) at exports._exceptionWithHostPort (util.js:860:20) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1060:14)

It seems that the connection between my app and mongo doesn't work.

Someone can help me to deal with that ?

2
It seems that you are trying to establish a local mongodb connection from your logs. You need to use db_1 as the hostname when connecting to the mongodb, when you are using linked containers.JAM

2 Answers

8
votes

By default, mongoose will try to connect to a local mongodb. To be able to use your mongodb container, you should use its hostname (db_1) while you make the connection.

1
votes

Yes Sabmit you are right !

I just had to change this line of code into my config file:

db: 'mongodb://localhost/test'

by,

db: 'mongodb://db_1:27017/test'