12

I can't make my docker compose work.

Here's my dockerfile:

FROM node:0.12
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev
RUN mkdir /myapp
WORKDIR /myapp
ADD . /myapp
RUN npm install

My docker-compose.yml

db:
  image: mongo
  ports:
    - 27017
web:
  build: .
  command: npm start
  volumes:
    - .:/myapp
  ports:
    - 3000:3000
  links:
    - db
  environment:
    PORT: 3000

And in server.js:

  var MONGO_DB;
  var DOCKER_DB = process.env.DB_1_PORT;
  if ( DOCKER_DB ) {
    MONGO_DB = DOCKER_DB.replace( "tcp", "mongodb" ) + "/dev_db";
  } else {
    MONGO_DB = process.env.MONGODB;
  }

  mongoose.connect(MONGO_DB);

as from duplicated from this repo: https://github.com/projectweekend/Node-Backend-Seed but process.env.DB_1_PORT is empty. How can I add it?

Thanks

4 Answers 4

25

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

Sign up to request clarification or add additional context in comments.

8 Comments

the port under environment should be declared like PORT=3000, that turns it into an environment variable on build. you can test by going echo $PORT which should produce 3000.
Yep, that's what I commented in the docker-compose.yml: # this is optional, allows express to use process.env.PORT instead of a raw 3000
I think what @GHETTO.CHiLD is saying is that PORT: 3000 should be - PORT=3000. My apologies I dont know how to format this answer.
About the final URL. It could be mongodb://<db image name>:27017/myapp in your case mongodb://db:27017/myapp should work. +1 for the great question and answer.
|
1

Make sure that mongoDB IP (and port) in your 'Server.js' file is set to 'PORT_27017_TCP_ADDR' (check port too) - can be found when running 'docker exec {web app container id} env'.

1 Comment

Indeed. Now I don't bother anymore. I run docker-compose run web /bin/bash and make printenv of whatever in the container!
0

Since we use docker-compose.yml and for reliable connection web service with db (mondoDB in this case) the final MONGO_DB connection string should look like (without authorization and additional args) as the following:

mongodb://db:27017/myapp

where:

  • db - name of the docker-composer MongoDB service
  • myapp - name of the mongoDB database

More see here

1 Comment

Correct. this was visible in the comments but indeed it is better to have a proper answer for this. Cheers
0

Your docker-compose should be

environment:
   - MONGODB=3000

See this link for more information on mapping environment variables. You are declaring the environment variable as PORT instead of MONGODB.

2 Comments

Not sure of why it should work, but it actually does.
In fact it didn't, sorry. I reported my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.