Sorry @gettho.child, I accepted your answer too quickly. I thought it was working but it wasn't. I'll report here my final solution since I have been struggling quite some to achieve it.
Dockerfile:
FROM node:0.12
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev libkrb5-dev
RUN mkdir /myapp
WORKDIR /myapp
ADD package.json /myapp/package.json
RUN npm install
ADD . /myapp
docker-compose.yml:
db:
image: mongo
ports:
- "27017:27017"
command: "--smallfiles --logpath=/dev/null"
web:
build: .
command: node app.js
volumes:
- .:/myapp
ports:
- "3000:3000"
links:
- db
environment:
PORT: 3000 # this is optional, allows express to use process.env.PORT instead of a raw 3000
And the interesting app.js extracts:
var MONGO_DB;
var DOCKER_DB = process.env.DB_PORT;
if ( DOCKER_DB ) {
MONGO_DB = DOCKER_DB.replace( 'tcp', 'mongodb' ) + '/myapp';
} else {
MONGO_DB = process.env.MONGODB;
}
var retry = 0;
mongoose.connect(MONGO_DB);
app.listen(process.env.PORT || 3000);
Regarding the process.env.DB_PORT I've tried many things around. If it doesn't work out-of-the-box, I suggest to console.log(process.env); and look for mongo's IP.
The final URL should look like: mongodb://172.17.0.76:27017/myapp
Good luck, it is worth it, Docker's awesome!
EDIT:
If the above works, I now found a techno-agnostic workflow by running:
docker-compose run web /bin/bash
- and in there running
printenv
I hope this is not too much self promotion but I wrote a double article on the topic which may help some of the readers: https://augustin-riedinger.fr/en/resources/using-docker-as-a-development-environment-part-1/
Cheers