2

I'm trying to connect my nodejs app with postgres using docker-compose.

Here's my docker-compose.yml

version: "3.9"

services:
  db:
    container_name: db
    image: postgres
    restart: always
    environment:
      POSTGRES_USER: agoodusername
      POSTGRES_PASSWORD: astrongpassword
      POSTGRES_DB: dbname
    volumes:
      - ./pgdata:/var/lib/postgresql/data
    ports:
      - 5433:5432
    networks:
      - postgres

  pgadmin:
    container_name: pgadmin4
    platform: linux/amd64
    image: dpage/pgadmin4
    restart: always
    depends_on:
      - db
    environment:
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: root
    ports:
      - 5050:80
    networks:
      - postgres

  be:
    container_name: be
    depends_on:
      - db
    build:
      context: .
      dockerfile: ./Dockerfile
    ports:
      - 3333:3333
    networks:
      - postgres

networks:
  postgres:
    driver: bridge

(Note that I have tried with and without networks)

My index.ts:

import { Entity, Column, PrimaryGeneratedColumn, createConnection } from "typeorm";
import express from 'express';

@Entity()
class Photo {
    @PrimaryGeneratedColumn()
    id?: number;
    @Column()
    name?: string;
    @Column()
    description?: string;
    @Column()
    filename?: string;
    @Column()
    views?: number;
    @Column()
    isPublished?: boolean;
}

const createCon = async () => {
    let retries = 5;
    while(retries) {
        try {
            const connection = await createConnection({
                type: 'postgres',
                url: 'postgres://agoodusername:astrongpassword@db:5433/dbname',
                synchronize: false,
                entities: [Photo]
            });
            console.log(connection.isConnected);
            break;
        }
        catch(e) {
            console.log(e);
            retries -= 1;
            await new Promise(res => setTimeout(res, 5000));
        }
    }
}

const app = express();

app.listen(3333, '0.0.0.0', () => {
    createCon();
    console.log("Server is running at port 3333");
})

Dockerfile:

FROM node:12.18.1-alpine

WORKDIR /app

COPY . .

RUN yarn

CMD ["yarn", "start"]

EXPOSE 3333

I ran postgres on its own docker container and node on another container (without docker-compose) everything works just fine.

Also, pgadmin container can't connect to the postgres container, I have provided the correct Hostname (db in this case) and the correct Host address (got it from running docker inspect db | grep IPAddress)

Here are the logs from the nodejs container:

yarn run v1.22.4

$ ts-node index.ts

Server is running at port 3333

Error: connect ECONNREFUSED 172.20.0.2:5433

at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {

errno: 'ECONNREFUSED',

code: 'ECONNREFUSED',

syscall: 'connect',

address: '172.20.0.2',

port: 5433

}

in case you want a full project check this repo

1
  • 3
    you are using incorrect port, 5433 port exposed outside, so it's only open on host, when using docker ip, you should use port inside running docker, that is 5432 Commented Sep 5, 2021 at 8:21

2 Answers 2

2

you are using incorrect port, 5433 port exposed outside, so it's only open on host, when using docker ip, you should use port inside running docker, that is 5432

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

Comments

2

As @crack_iT said in the comments, the port "5433" is exposed to the host machine, not to other containers, so to interact with the other container you should use the exposed port by the image (which in this case is 5432).

1 Comment

i added my comment as answer, you can accept if it helped.

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.