0

Using NestJS, docker-compose and Postgres on Ubuntu 20.4

Trying my first database migration with this app:

npx typeorm migration:create -n mushroomRefactor

npm run build

npx typeorm migration:run

docker-compose:

version: "3"

services: 
  db:
    image: postgres
    restart: always
    volumes:
      - mushrooms-psql:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    environment: 
      POSTGRES_PASSWORD: pass123
  
volumes:
  mushrooms-psql:

ormconfig.js:

module.exports = {
  type: 'postgres',
  host: 'localhost',
  port: 5423,
  username: 'postgres',
  password: 'pass123',
  database: 'postgres',
  entities: ['dist/**/*.entity.js'],
  migrations: ['dist/migrations/*.js'],
  cli: {
    migrationsDir: 'src/migrations',
  },
};

Dockerfile:

FROM node:16

# install app dependencies 
# use wildcard * to install package.json and package-lock.json
COPY package*.json ./

RUN npm install

# bundle app source
COPY . . 

EXPOSE 8080

RUN npm run build

# entry file to run the app
CMD [ "node", "dist/main" ]

But it is returning: Error during migration run: Error: connect ECONNREFUSED 127.0.0.1:5423 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1146:16) { errno: -111, code: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 5423 }

4
  • 1
    Your docker compose is running, right? docker-compose ps? Commented Dec 7, 2021 at 20:48
  • Yup! world-of-mushrooms_db_1 docker-entrypoint.sh postgres Up 0.0.0.0:5432/tcp,:::5432->5432/tcp Commented Dec 7, 2021 at 20:54
  • And you're running this command from your machine, not from within the docker container, right? Commented Dec 7, 2021 at 20:57
  • Yes, I believe so... I'm not inside the container's shell. In case you are aware of it, I have been following the tutorial on the NestJS website. Commented Dec 7, 2021 at 20:58

1 Answer 1

2

Check port you've set, it's different

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

1 Comment

Ah! Well spotted. Thank you.

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.