title: Create Docker image for NodeJS + PostgreSQL web application
body: <p>I've been reading Docker's documentation, but I can't get around creating an image that will work.</p>
  
  <p>I have a NodeJS application that uses PostgreSQL as database:</p>
  
  <pre><code>var connectionString = process.env.DATABASE_URL || 'postgres://localhost:5432/db';
  var pg = require('pg');
  var pgp = require('pg-promise')();
  var db = pgp(connectionString);
  db.func('some_storedProcedure').then(//...)
  //...
  </code></pre>
  
  <p>I first created a Dockerfile according to <a href="https://nodejs.org/en/docs/guides/nodejs-docker-webapp/" rel="nofollow noreferrer">Node's documentation</a> for it:</p>
  
  <pre><code>FROM node:argon
  
  # Create app directory
  RUN mkdir -p /app
  WORKDIR /app
  
  # Install app dependencies
  COPY package.json /app
  RUN npm install
  
  # Bundle app source
  COPY . /app
  
  EXPOSE 5000
  
  CMD [ "npm", "start" ]
  </code></pre>
  
  <p>I then followed <a href="https://stackoverflow.com/questions/33357567/econnrefused-for-postgres-on-nodejs-with-dockers">this post</a> regarding connecting the database to it with docker-compose. the <code>docker-compose.yml</code> file looks like:</p>
  
  <pre><code>web:
  
  
   build: .
    ports:
      - "5000:5000"
    volumes:
      - .:/app
    links:
      - db
    environment:
      DATABASE_URL: postgres://myuser:mypass@db:5432/db
  db:
    image: postgres
    environment:
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypass
  </code></pre>
  
  <p>This is (some) of what is returned when I run <code>docker-compose up</code> with these files, after creating the image.</p>
  
  <pre><code>npm info ok 
   ---&gt; 87dbbae35721
  Removing intermediate container c73f826a0b3d
  Step 6 : COPY . /app
   ---&gt; ec56bfc11d3c
  Removing intermediate container 745ddf82d742
  Step 7 : EXPOSE 5000
   ---&gt; Running in b2be5aecd9d6
   ---&gt; a7d126a7ea5e
  Removing intermediate container b2be5aecd9d6
  Step 8 : CMD npm start
   ---&gt; Running in 0379d512c688
   ---&gt; 266517f47311
  Removing intermediate container 0379d512c688
  Successfully built 266517f47311
  WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
  Starting imagename_db_1
  Creating imagename_web_1
  Attaching to imagename_db_1, imagename_web_1
  web_1  | npm info it worked if it ends with ok
  web_1  | npm info using npm@2.15.1
  web_1  | npm info using node@v4.4.3
  web_1  | npm info prestart SharedServer@5.8.0
  web_1  | npm info start SharedServer@5.8.0
  web_1  | 
  web_1  | &gt; SharedServer@5.8.0 start /app
  web_1  | &gt; node index.js
  web_1  | 
  web_1  | Wed, 27 Apr 2016 00:41:19 GMT body-parser deprecated bodyParser: use individual json/urlencoded middlewares at index.js:13:9
  web_1  | Wed, 27 Apr 2016 00:41:19 GMT body-parser deprecated undefined extended: provide extended option at node_modules/body-parser/index.js:105:29
  web_1  | Node app is running on port 5000
  db_1   | LOG:  database system was interrupted; last known up at 2016-04-25 00:17:59 UTC
  db_1   | LOG:  database system was not properly shut down; automatic recovery in progress
  db_1   | LOG:  invalid record length at 0/17076E8
  db_1   | LOG:  redo is not required
  db_1   | LOG:  MultiXact member wraparound protections are now enabled
  db_1   | LOG:  database system is ready to accept connections
  db_1   | LOG:  autovacuum launcher started
  </code></pre>
  
  <p>When I access <a href="http://localhost:5000" rel="nofollow noreferrer">http://localhost:5000</a>, I see the web application running, but whenever I fire up something that tries to access the database, I get a HTTP 500 error with the following body</p>
  
  <pre><code>code: "28P01"
  file: "auth.c"
  length: 98
  line: "285"
  name: "error"
  routine: "auth_failed"
  severity: "FATAL"
  </code></pre>
  
  <p>What am I doing wrong? I'm not sure I understand what I'm doing with Docker, and the only thing I have for documentation are simple recipes to build specific environments (or at least, that's what I've understood)</p>
  
  <p>Thanks.</p>