brocoders / nestjs-boilerplate

NestJS boilerplate. Auth, TypeORM, Mongoose, Postgres, MongoDB, Mailing, I18N, Docker.

Home Page:https://nestjs-boilerplate-test.herokuapp.com/docs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

error when running "docker-compose up -d"

Yassin-H-Rassul opened this issue · comments

Describe the bug
I am new to Nest and Docker, so sorry for my noob questioning.
when I run the docker-compose up command in a terminal, it runs all the containers (services), except the API service, and it gives me the following error in the logs:

2023-04-21 15:29:56 wait-for-it.sh: waiting 15 seconds for postgres:5432
2023-04-21 15:29:57 wait-for-it.sh: postgres:5432 is available after 1 seconds
2023-04-21 15:30:01 Error: connect ECONNREFUSED 127.0.0.1:5432
2023-04-21 15:30:01     at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) {
2023-04-21 15:30:01   errno: -111,
2023-04-21 15:30:01   code: 'ECONNREFUSED',
2023-04-21 15:30:01   syscall: 'connect',
2023-04-21 15:30:01   address: '127.0.0.1',
2023-04-21 15:30:01   port: 5432
2023-04-21 15:30:01 }
2023-04-21 15:29:57 
2023-04-21 15:29:57 > nestjs-boilerplate@0.0.1 migration:run
2023-04-21 15:29:57 > npm run typeorm -- --dataSource=src/database/data-source.ts migration:run
2023-04-21 15:29:57 
2023-04-21 15:29:58 
2023-04-21 15:29:58 > nestjs-boilerplate@0.0.1 typeorm
2023-04-21 15:29:58 > env-cmd ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --dataSource=src/database/data-source.ts migration:run

although, I have no problem with Postgres config, or any issue related to env variables because the app runs perfectly when I run npm run start:dev

I have wasted almost one week on this, any help would be appreciated, thanks a lot.

To Reproduce
Steps to reproduce the behavior:

  1. Go to terminal and run docker-compose up -d
  2. the API service does not start, it says exited with (1), see logs
  3. the error is shown in the logs that I provided above.

Expected behavior
I wanted all the services to run perfectly, so I can deploy the code to server.

Desktop (please complete the following information):

  • OS: Windows
  • NodeJS Version 18.16.0

@Yassin-H-Rassul in .env file, please change DATABASE_HOST=localhost to DATABASE_HOST=postgres

@Yassin-H-Rassul in .env file, please change DATABASE_HOST=localhost to DATABASE_HOST=postgres

Bro I did it, thanks very much, but now it gives another error.
Now it throws an error that the database does not exist, when in fact it does exist when I run "npm run start:dev" but inside docker, in logs, it gives me this error inside the Postgres Service:

2023-04-21 19:26:41 
2023-04-21 19:26:41 PostgreSQL Database directory appears to contain a database; Skipping initialization
2023-04-21 19:26:41 
2023-04-21 19:28:42 
2023-04-21 19:28:42 PostgreSQL Database directory appears to contain a database; Skipping initialization
2023-04-21 19:28:42 
2023-04-21 20:16:52 
2023-04-21 20:16:52 PostgreSQL Database directory appears to contain a database; Skipping initialization
2023-04-21 20:16:52 
2023-04-21 19:26:41 2023-04-21 16:26:41.482 UTC [1] LOG:  starting PostgreSQL 15.2 on x86_64-pc-linux-musl, compiled by gcc (Alpine 12.2.1_git20220924-r4) 12.2.1 20220924, 64-bit
2023-04-21 19:26:41 2023-04-21 16:26:41.483 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2023-04-21 19:26:41 2023-04-21 16:26:41.483 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2023-04-21 19:26:41 2023-04-21 16:26:41.497 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-04-21 19:26:41 2023-04-21 16:26:41.541 UTC [24] LOG:  database system was shut down at 2023-04-21 16:26:37 UTC
2023-04-21 19:26:41 2023-04-21 16:26:41.589 UTC [1] LOG:  database system is ready to accept connections
2023-04-21 19:27:07 2023-04-21 16:27:07.120 UTC [29] FATAL:  database "topen_db_new" does not exist
2023-04-21 19:28:35 2023-04-21 16:28:35.842 UTC [31] FATAL:  database "topen_db_new" does not exist
2023-04-21 19:28:39 2023-04-21 16:28:39.946 UTC [1] LOG:  received fast shutdown request
2023-04-21 19:28:39 2023-04-21 16:28:39.954 UTC [1] LOG:  aborting any active transactions
2023-04-21 19:28:39 2023-04-21 16:28:39.957 UTC [1] LOG:  background worker "logical replication launcher" (PID 27) exited with exit code 1
2023-04-21 19:28:39 2023-04-21 16:28:39.957 UTC [22] LOG:  shutting down
2023-04-21 19:28:39 2023-04-21 16:28:39.963 UTC [22] LOG:  checkpoint starting: shutdown immediate
2023-04-21 19:28:40 2023-04-21 16:28:40.025 UTC [22] LOG:  checkpoint complete: wrote 3 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.017 s, sync=0.006 s, total=0.068 s; sync files=2, longest=0.003 s, average=0.003 s; distance=0 kB, estimate=0 kB
2023-04-21 19:28:40 2023-04-21 16:28:40.041 UTC [1] LOG:  database system is shut down
2023-04-21 19:28:42 2023-04-21 16:28:42.134 UTC [1] LOG:  starting PostgreSQL 15.2 on x86_64-pc-linux-musl, compiled by gcc (Alpine 12.2.1_git20220924-r4) 12.2.1 20220924, 64-bit
2023-04-21 19:28:42 2023-04-21 16:28:42.134 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2023-04-21 19:28:42 2023-04-21 16:28:42.134 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2023-04-21 19:28:42 2023-04-21 16:28:42.166 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-04-21 19:28:42 2023-04-21 16:28:42.212 UTC [24] LOG:  database system was shut down at 2023-04-21 16:28:40 UTC
2023-04-21 19:28:42 2023-04-21 16:28:42.257 UTC [1] LOG:  database system is ready to accept connections
2023-04-21 19:29:00 2023-04-21 16:29:00.357 UTC [29] FATAL:  database "topen_db_new" does not exist
2023-04-21 19:33:42 2023-04-21 16:33:42.223 UTC [22] LOG:  checkpoint starting: time
2023-04-21 19:33:42 2023-04-21 16:33:42.307 UTC [22] LOG:  checkpoint complete: wrote 3 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.018 s, sync=0.008 s, total=0.084 s; sync files=2, longest=0.005 s, average=0.004 s; distance=0 kB, estimate=0 kB
2023-04-21 20:16:40 2023-04-21 17:16:40.996 UTC [31] FATAL:  database "topen_db_new" does not exist
2023-04-21 20:16:50 2023-04-21 17:16:50.108 UTC [1] LOG:  received fast shutdown request
2023-04-21 20:16:50 2023-04-21 17:16:50.122 UTC [1] LOG:  aborting any active transactions
2023-04-21 20:16:50 2023-04-21 17:16:50.124 UTC [1] LOG:  background worker "logical replication launcher" (PID 27) exited with exit code 1
2023-04-21 20:16:50 2023-04-21 17:16:50.125 UTC [22] LOG:  shutting down
2023-04-21 20:16:50 2023-04-21 17:16:50.135 UTC [22] LOG:  checkpoint starting: shutdown immediate
2023-04-21 20:16:50 2023-04-21 17:16:50.190 UTC [22] LOG:  checkpoint complete: wrote 0 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.010 s, sync=0.001 s, total=0.066 s; sync files=0, longest=0.000 s, average=0.000 s; distance=0 kB, estimate=0 kB
2023-04-21 20:16:50 2023-04-21 17:16:50.203 UTC [1] LOG:  database system is shut down
2023-04-21 20:16:52 2023-04-21 17:16:52.165 UTC [1] LOG:  starting PostgreSQL 15.2 on x86_64-pc-linux-musl, compiled by gcc (Alpine 12.2.1_git20220924-r4) 12.2.1 20220924, 64-bit
2023-04-21 20:16:52 2023-04-21 17:16:52.165 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2023-04-21 20:16:52 2023-04-21 17:16:52.165 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2023-04-21 20:16:52 2023-04-21 17:16:52.181 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-04-21 20:16:52 2023-04-21 17:16:52.237 UTC [24] LOG:  database system was shut down at 2023-04-21 17:16:50 UTC
2023-04-21 20:16:52 2023-04-21 17:16:52.286 UTC [1] LOG:  database system is ready to accept connections
2023-04-21 20:17:07 2023-04-21 17:17:07.961 UTC [29] FATAL:  database "topen_db_new" does not exist
2023-04-21 20:19:02 2023-04-21 17:19:02.622 UTC [1] LOG:  received fast shutdown request
2023-04-21 20:19:02 2023-04-21 17:19:02.638 UTC [1] LOG:  aborting any active transactions
2023-04-21 20:19:02 2023-04-21 17:19:02.643 UTC [1] LOG:  background worker "logical replication launcher" (PID 27) exited with exit code 1
2023-04-21 20:19:02 2023-04-21 17:19:02.644 UTC [22] LOG:  shutting down
2023-04-21 20:19:02 2023-04-21 17:19:02.648 UTC [22] LOG:  checkpoint starting: shutdown immediate
2023-04-21 20:19:02 2023-04-21 17:19:02.715 UTC [22] LOG:  checkpoint complete: wrote 3 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.019 s, sync=0.005 s, total=0.072 s; sync files=2, longest=0.003 s, average=0.003 s; distance=0 kB, estimate=0 kB
2023-04-21 20:19:02 2023-04-21 17:19:02.724 UTC [1] LOG:  database system is shut down
2023-04-21 20:19:15 2023-04-21 17:19:15.405 UTC [1] LOG:  starting PostgreSQL 15.2 on x86_64-pc-linux-musl, compiled by gcc (Alpine 12.2.1_git20220924-r4) 12.2.1 20220924, 64-bit
2023-04-21 20:19:15 2023-04-21 17:19:15.405 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2023-04-21 20:19:15 2023-04-21 17:19:15.405 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2023-04-21 20:19:15 2023-04-21 17:19:15.415 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-04-21 20:19:15 2023-04-21 17:19:15.449 UTC [24] LOG:  database system was shut down at 2023-04-21 17:19:02 UTC
2023-04-21 20:19:15 2023-04-21 17:19:15.479 UTC [1] LOG:  database system is ready to accept connections
2023-04-21 20:19:20 2023-04-21 17:19:20.131 UTC [29] FATAL:  database "topen_db_new" does not exist
2023-04-21 20:19:15 
2023-04-21 20:19:15 PostgreSQL Database directory appears to contain a database; Skipping initialization
2023-04-21 20:19:15 

and inside Api service logs it gives this error:

2023-04-21 20:16:37 wait-for-it.sh: waiting 15 seconds for postgres:5432
2023-04-21 20:16:37 wait-for-it.sh: postgres:5432 is available after 0 seconds
2023-04-21 20:16:41 error: database "topen_db_new" does not exist
2023-04-21 20:16:41     at Parser.parseErrorMessage (/usr/src/app/node_modules/pg-protocol/src/parser.ts:369:69)
2023-04-21 20:16:41     at Parser.handlePacket (/usr/src/app/node_modules/pg-protocol/src/parser.ts:188:21)
2023-04-21 20:16:41     at Parser.parse (/usr/src/app/node_modules/pg-protocol/src/parser.ts:103:30)
2023-04-21 20:16:41     at Socket.<anonymous> (/usr/src/app/node_modules/pg-protocol/src/index.ts:7:48)
2023-04-21 20:16:41     at Socket.emit (node:events:513:28)
2023-04-21 20:16:41     at Socket.emit (node:domain:489:12)
2023-04-21 20:16:41     at addChunk (node:internal/streams/readable:324:12)
2023-04-21 20:16:41     at readableAddChunk (node:internal/streams/readable:297:9)
2023-04-21 20:16:41     at Socket.Readable.push (node:internal/streams/readable:234:10)
2023-04-21 20:16:41     at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
2023-04-21 20:16:41   length: 97,
2023-04-21 20:16:41   severity: 'FATAL',
2023-04-21 20:16:41   code: '3D000',
2023-04-21 20:16:41   detail: undefined,
2023-04-21 20:16:41   hint: undefined,
2023-04-21 20:16:41   position: undefined,
2023-04-21 20:16:41   internalPosition: undefined,
2023-04-21 20:16:41   internalQuery: undefined,
2023-04-21 20:16:41   where: undefined,
2023-04-21 20:16:41   schema: undefined,
2023-04-21 20:16:41   table: undefined,
2023-04-21 20:16:41   column: undefined,
2023-04-21 20:16:41   dataType: undefined,
2023-04-21 20:16:41   constraint: undefined,
2023-04-21 20:16:41   file: 'postinit.c',
2023-04-21 20:16:37 
2023-04-21 20:16:37 > nestjs-boilerplate@0.0.1 migration:run
2023-04-21 20:16:37 > npm run typeorm -- --dataSource=src/database/data-source.ts migration:run
2023-04-21 20:16:37 
2023-04-21 20:16:38 
2023-04-21 20:16:38 > nestjs-boilerplate@0.0.1 typeorm
2023-04-21 20:16:38 > env-cmd ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --dataSource=src/database/data-source.ts migration:run
2023-04-21 20:16:38 
2023-04-21 20:16:40 Error during migration run:
2023-04-21 20:17:04 
2023-04-21 20:17:04 > nestjs-boilerplate@0.0.1 migration:run
2023-04-21 20:17:04 > npm run typeorm -- --dataSource=src/database/data-source.ts migration:run
2023-04-21 20:17:04 
2023-04-21 20:17:05 
2023-04-21 20:17:05 > nestjs-boilerplate@0.0.1 typeorm
2023-04-21 20:17:05 > env-cmd ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --dataSource=src/database/data-source.ts migration:run
2023-04-21 20:17:05 
2023-04-21 20:17:07 Error during migration run:
2023-04-21 20:16:41   line: '941',
2023-04-21 20:16:41   routine: 'InitPostgres'
2023-04-21 20:16:41 }
2023-04-21 20:17:04 wait-for-it.sh: waiting 15 seconds for postgres:5432
2023-04-21 20:17:04 wait-for-it.sh: postgres:5432 is available after 0 seconds
2023-04-21 20:17:07 error: database "topen_db_new" does not exist
2023-04-21 20:17:07     at Parser.parseErrorMessage (/usr/src/app/node_modules/pg-protocol/src/parser.ts:369:69)
2023-04-21 20:17:07     at Parser.handlePacket (/usr/src/app/node_modules/pg-protocol/src/parser.ts:188:21)
2023-04-21 20:17:07     at Parser.parse (/usr/src/app/node_modules/pg-protocol/src/parser.ts:103:30)
2023-04-21 20:17:07     at Socket.<anonymous> (/usr/src/app/node_modules/pg-protocol/src/index.ts:7:48)
2023-04-21 20:17:07     at Socket.emit (node:events:513:28)
2023-04-21 20:17:07     at Socket.emit (node:domain:489:12)
2023-04-21 20:17:07     at addChunk (node:internal/streams/readable:324:12)
2023-04-21 20:17:07     at readableAddChunk (node:internal/streams/readable:297:9)
2023-04-21 20:17:07     at Socket.Readable.push (node:internal/streams/readable:234:10)
2023-04-21 20:17:07     at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
2023-04-21 20:17:07   length: 97,
2023-04-21 20:17:07   severity: 'FATAL',
2023-04-21 20:17:07   code: '3D000',
2023-04-21 20:17:07   detail: undefined,
2023-04-21 20:17:07   hint: undefined,
2023-04-21 20:17:07   position: undefined,
2023-04-21 20:17:07   internalPosition: undefined,
2023-04-21 20:17:07   internalQuery: undefined,
2023-04-21 20:17:07   where: undefined,
2023-04-21 20:17:07   schema: undefined,
2023-04-21 20:17:07   table: undefined,
2023-04-21 20:17:07   column: undefined,
2023-04-21 20:17:07   dataType: undefined,
2023-04-21 20:17:07   constraint: undefined,
2023-04-21 20:17:07   file: 'postinit.c',
2023-04-21 20:17:07   line: '941',
2023-04-21 20:17:07   routine: 'InitPostgres'
2023-04-21 20:17:07 }
2023-04-21 20:19:13 wait-for-it.sh: waiting 15 seconds for postgres:5432
2023-04-21 20:19:16 wait-for-it.sh: postgres:5432 is available after 3 seconds
2023-04-21 20:19:20 error: database "topen_db_new" does not exist
2023-04-21 20:19:20     at Parser.parseErrorMessage (/usr/src/app/node_modules/pg-protocol/src/parser.ts:369:69)
2023-04-21 20:19:20     at Parser.handlePacket (/usr/src/app/node_modules/pg-protocol/src/parser.ts:188:21)
2023-04-21 20:19:20     at Parser.parse (/usr/src/app/node_modules/pg-protocol/src/parser.ts:103:30)
2023-04-21 20:19:20     at Socket.<anonymous> (/usr/src/app/node_modules/pg-protocol/src/index.ts:7:48)
2023-04-21 20:19:20     at Socket.emit (node:events:513:28)
2023-04-21 20:19:20     at Socket.emit (node:domain:489:12)
2023-04-21 20:19:20     at addChunk (node:internal/streams/readable:324:12)
2023-04-21 20:19:20     at readableAddChunk (node:internal/streams/readable:297:9)
2023-04-21 20:19:20     at Socket.Readable.push (node:internal/streams/readable:234:10)
2023-04-21 20:19:20     at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
2023-04-21 20:19:20   length: 97,
2023-04-21 20:19:16 
2023-04-21 20:19:16 > nestjs-boilerplate@0.0.1 migration:run
2023-04-21 20:19:16 > npm run typeorm -- --dataSource=src/database/data-source.ts migration:run
2023-04-21 20:19:16 
2023-04-21 20:19:17 
2023-04-21 20:19:17 > nestjs-boilerplate@0.0.1 typeorm
2023-04-21 20:19:17 > env-cmd ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --dataSource=src/database/data-source.ts migration:run
2023-04-21 20:19:17 
2023-04-21 20:19:20 Error during migration run:
2023-04-21 20:19:20   severity: 'FATAL',
2023-04-21 20:19:20   code: '3D000',
2023-04-21 20:19:20   detail: undefined,
2023-04-21 20:19:20   hint: undefined,
2023-04-21 20:19:20   position: undefined,
2023-04-21 20:19:20   internalPosition: undefined,
2023-04-21 20:19:20   internalQuery: undefined,
2023-04-21 20:19:20   where: undefined,
2023-04-21 20:19:20   schema: undefined,
2023-04-21 20:19:20   table: undefined,
2023-04-21 20:19:20   column: undefined,
2023-04-21 20:19:20   dataType: undefined,
2023-04-21 20:19:20   constraint: undefined,
2023-04-21 20:19:20   file: 'postinit.c',
2023-04-21 20:19:20   line: '941',
2023-04-21 20:19:20   routine: 'InitPostgres'
2023-04-21 20:19:20 }