0

My app.js file is as follows

var express = require('express');
var app = express();
var uuid = require('node-uuid');

var pg = require('pg');
var conString = process.env.DB; // "postgres://username:password@localhost/database";

// Routes
app.get('/api/status', function(req, res) {
  pg.connect(conString, function(err, client, done) {
    if(err) {
      return res.status(500).send('error fetching client from pool');
    }
    client.query('SELECT now() as time', [], function(err, result) {
      //call `done()` to release the client back to the pool
      done();

      if(err) {
        return res.status(500).send('error running query');
      }

      return res.json({
        request_uuid: uuid.v4(),
        time: result.rows[0].time
      });
    });
  });
});

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.json({
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.json({
    message: err.message,
    error: {}
  });
});


module.exports = app;

and my Dockerfile is as follows

FROM node:14

WORKDIR /app

COPY package*.json ./

RUN  npm install

COPY . .

my docker-compose file is as follows:

version: "3.2"
services:
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: test123
      POSTGRES_DB: testing
    volumes:
      - ./pgdata:/var/lib/postgresql/data
  web:
    build: .
    command: npm start app.js
    depends_on:
      - db
    environment:
      DB: 'postgres://postgres:test123@localhost:5432/testing'
    ports:
      - "3000:3000"

When I am checking into the container DB, I can see the user, database but my app.js file cannot fetch a value from the database. I am routing the data in this endpoint /api/status when I am accessing http://ip-address:3000 == then i am getting 404 error which I mentioned in my code. without docker, I can easily access my output at the given endpoint.

Any help will be helpful. Thanksss

7
  • Which IP url do you call on http://ip-address:3000 the IP of the container? Try with http://localhost:3000 but stop the local one if is active. Commented Aug 27, 2021 at 10:29
  • ip-address of my localmachine, my container running on different IP maybe I should try this postgres://postgres:[email protected]:5432/testing Commented Aug 27, 2021 at 10:34
  • The express apps I've seen have all had an app.listen call. Where do you tell your app to listen on port 3000? Commented Aug 27, 2021 at 11:15
  • I have added it in /bin/www Commented Aug 27, 2021 at 12:29
  • This will not help with 404 error, but your front will not be able to connect to your database. Change your database connection from DB: 'postgres://postgres:test123@localhost:5432/testing' to DB: 'postgres://postgres:test123@db:5432/testing' As you are running docker containers, localhost refers to the container itself, not your host. You need to connecto to another container. Also add a ports section in db service to expose 5432 port Commented Aug 27, 2021 at 13:16

1 Answer 1

1

When you ask docker-compose to spin up your containers, it creates a virtual network where each container has it's own ip address. When you - in your web container - ask to connect to localhost, you're trying to connect to the web container itself and not the database container.

You can access the database container by its service name db. So change your docker-compose file from

environment:
  DB: 'postgres://postgres:test123@localhost:5432/testing'

to

environment:
  DB: 'postgres://postgres:test123@db:5432/testing'
Sign up to request clarification or add additional context in comments.

1 Comment

No success from this.

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.