Using Docker to create Node.js + Mongo containers

SketchMaster

Diamond Member
Feb 23, 2005
3,100
149
116
I'm trying to teach myself Docker, and have been trying to get this boilerplate to connect to a Mongo DB container but can't get them to play nice. I'm running windows 10 and have docker for windows and docker toolbox, both latest stable.

I've got a Docker network bridge setup and both containers are in there, mongo is happy (as far as I can tell..) and is waiting for a connection, but when I run the command to run the node container I get a big fat error:

Node Run Command:
Code:
docker run -d --net=Sketchs_network --name nodeapp -p 3000:3000 SketchMaster/node

Node Error:
Code:
npm info it worked if it ends with ok
npm info using npm@4.1.2
npm info using node@v7.5.0
npm info lifecycle nodejs-express-mongoose-demo@4.0.0~prestart: nodejs-express-mongoose-demo@4.0.0
npm info lifecycle nodejs-express-mongoose-demo@4.0.0~start: nodejs-express-mongoose-demo@4.0.0
2017-02-04T01:07:45.009009900Z
> nodejs-express-mongoose-demo@4.0.0 start /home/mean
> cross-env NODE_ENV=development ./node_modules/.bin/nodemon server.js
2017-02-04T01:07:45.009032500Z
[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node server.js`
2017-02-04T01:07:47.005292300Z
/home/mean/node_modules/mongodb/lib/mongo_client.js:224
          throw err
          ^
MongoError: failed to connect to server [localhost:27017] on first connect
    at Pool.<anonymous> (/home/mean/node_modules/mongodb-core/lib/topologies/server.js:326:35)
    at emitOne (events.js:96:13)
    at Pool.emit (events.js:189:7)
    at Connection.<anonymous> (/home/mean/node_modules/mongodb-core/lib/connection/pool.js:270:12)
    at Object.onceWrapper (events.js:291:19)
    at emitTwo (events.js:106:13)
    at Connection.emit (events.js:192:7)
    at Socket.<anonymous> (/home/mean/node_modules/mongodb-core/lib/connection/connection.js:175:49)
    at Object.onceWrapper (events.js:291:19)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:189:7)
    at emitErrorNT (net.js:1280:8)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)
[nodemon] app crashed - waiting for file changes before starting...

Dockerfile:
Code:
FROM node:latest

MAINTAINER Sketchmaster

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

ENV NODE_ENV development

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

# Bundle app source
COPY . /usr/src/app

EXPOSE 3000

CMD [ "npm", "start" ]

Thoughts, ideas?
 

SketchMaster

Diamond Member
Feb 23, 2005
3,100
149
116
Ha! Duh... Needed to actually call out the mongo server in the connection string. the two containers are completely different systems.