Home > database >  Why is hot reloading not working in my NestJS/Docker-Compose multistage project?
Why is hot reloading not working in my NestJS/Docker-Compose multistage project?

Time:12-17

Hot reloading is not working. The API is not being updated after changes in the code are saved. Here is the code:

https://codesandbox.io/s/practical-snowflake-c4j6fh

When bulding (docker-compose up -V --build) I get the following messages on terminal:

2022-12-16 09:29:53 redis                | 1:C 16 Dec 2022 12:29:53.411 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo</br>2022-12-16 09:29:53 redis                | 1:C 16 Dec 2022 12:29:53.411 # Redis version=7.0.6, bits=64, commit=00000000, modified=0, pid=1, just started</br>2022-12-16 09:29:53 redis                | 1:C 16 Dec 2022 12:29:53.411 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf</br>2022-12-16 09:29:53 redis                | 1:M 16 Dec 2022 12:29:53.411 * monotonic clock: POSIX clock_gettime</br>2022-12-16 09:29:53 redis                | 1:M 16 Dec 2022 12:29:53.411 * Running mode=standalone, port=6379.</br>2022-12-16 09:29:53 redis                | 1:M 16 Dec 2022 12:29:53.411 # Server initialized</br>2022-12-16 09:29:53 redis                | 1:M 16 Dec 2022 12:29:53.411 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can can also cause failures without low memory condition, see https://github.com/</br>jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.</br>2022-12-16 09:29:53 redis                | 1:M 16 Dec 2022 12:29:53.412 * Ready to accept connections</br>2022-12-16 09:29:53 postgres             | The files belonging to this database system will be owned by user "postgres".</br>2022-12-16 09:29:53 postgres             | This user must also own the server process.</br>2022-12-16 09:29:53 postgres             | </br>2022-12-16 09:29:53 postgres             | The database cluster will be initialized with locale "en_US.utf8".</br>2022-12-16 09:29:53 postgres             | The default database encoding has accordingly been set to "UTF8".</br>2022-12-16 09:29:53 postgres             | The default text search configuration will be set to "english".</br>2022-12-16 09:29:53 postgres             | </br>2022-12-16 09:29:53 postgres             | Data page checksums are disabled.</br>2022-12-16 09:29:53 postgres             | </br>2022-12-16 09:29:53 postgres             | fixing permissions on existing directory /var/lib/postgresql/data ... ok</br>2022-12-16 09:29:53 postgres             | creating subdirectories ... ok</br>2022-12-16 09:29:53 postgres             | selecting dynamic shared memory implementation ... posix</br>2022-12-16 09:29:53 postgres             | selecting default max_connections ... 100</br>2022-12-16 09:29:53 postgres             | selecting default shared_buffers ... 128MB</br>2022-12-16 09:29:53 postgres             | selecting default time zone ... Etc/UTC</br>2022-12-16 09:29:53 postgres             | creating configuration files ... ok</br>2022-12-16 09:29:53 postgres             | running bootstrap script ... ok</br>2022-12-16 09:29:54 postgres             | performing post-bootstrap initialization ... ok</br>2022-12-16 09:29:54 postgres             | initdb: warning: enabling "trust" authentication for local connections</br>2022-12-16 09:29:54 postgres             | You can change this by editing pg_hba.conf or using the option -A, or</br>2022-12-16 09:29:54 postgres             | --auth-local and --auth-host, the next time you run initdb.</br>2022-12-16 09:29:54 postgres             | syncing data to disk ... ok</br>2022-12-16 09:29:54 postgres             | </br>2022-12-16 09:29:54 postgres             | </br>2022-12-16 09:29:54 postgres             | Success. You can now start the database server using:</br>2022-12-16 09:29:54 postgres             | </br>2022-12-16 09:29:54 postgres             |     pg_ctl -D /var/lib/postgresql/data -l logfile start</br>2022-12-16 09:29:54 postgres             | </br>2022-12-16 09:29:54 postgres             | waiting for server to start....2022-12-16 12:29:54.305 UTC [48] LOG:  starting PostgreSQL 12.13 (Debian 12.13-1.pgdg110 1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit</br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.311 UTC [48] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"</br>2022-12-16 09:29:54 store-backend-api-1  | </br>2022-12-16 09:29:54 store-backend-api-1  | > [email protected] start:dev</br>2022-12-16 09:29:54 store-backend-api-1  | > nest start --watch</br>2022-12-16 09:29:54 store-backend-api-1  | </br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.338 UTC [49] LOG:  database system was shut down at 2022-12-16 12:29:54 UTC</br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.345 UTC [48] LOG:  database system is ready to accept connections</br>2022-12-16 09:29:54 postgres             |  done</br>2022-12-16 09:29:54 postgres             | server started</br>2022-12-16 09:29:54 postgres             | </br>2022-12-16 09:29:54 postgres             | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*</br>2022-12-16 09:29:54 postgres             | </br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.434 UTC [48] LOG:  received fast shutdown request</br>2022-12-16 09:29:54 postgres             | waiting for server to shut down....2022-12-16 12:29:54.444 UTC [48] LOG:  aborting any active transactions</br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.445 UTC [48] LOG:  background worker "logical replication launcher" (PID 55) exited with exit code 1</br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.445 UTC [50] LOG:  shutting down</br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.482 UTC [48] LOG:  database system is shut down</br>2022-12-16 09:29:54 postgres             |  done</br>2022-12-16 09:29:54 postgres             | server stopped</br>2022-12-16 09:29:54 postgres             | </br>2022-12-16 09:29:54 postgres             | PostgreSQL init process complete; ready for start up.</br>2022-12-16 09:29:54 postgres             | </br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.552 UTC [1] LOG:  starting PostgreSQL 12.13 (Debian 12.13-1.pgdg110 1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit</br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.552 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432</br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.552 UTC [1] LOG:  listening on IPv6 address "::", port 5432</br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.563 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"</br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.592 UTC [67] LOG:  database system was shut down at 2022-12-16 12:29:54 UTC</br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.600 UTC [1] LOG:  database system is ready to accept connections</br>

And then the previous messages disappear and the following ones are shown:

[12:29:55 PM] Starting compilation in watch mode...</br>2022-12-16 09:29:55 store-backend-api-1  | </br>2022-12-16 09:29:58 store-backend-api-1  | [12:29:58 PM] Found 0 errors. Watching for file changes.</br>2022-12-16 09:29:58 store-backend-api-1  | </br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [NestFactory] Starting Nest application...</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [InstanceLoader] TypeOrmModule dependencies initialized  65ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [InstanceLoader] ConfigHostModule dependencies initialized  1ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [InstanceLoader] AppModule dependencies initialized  0ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [InstanceLoader] ConfigModule dependencies initialized  1ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [InstanceLoader] TypeOrmCoreModule dependencies initialized  49ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [InstanceLoader] TypeOrmModule dependencies initialized  0ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [InstanceLoader] UserModule dependencies initialized  1ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [RoutesResolver] AppController {/api}:  7ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [RouterExplorer] Mapped {/api, GET} route  4ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [RouterExplorer] Mapped {/api/test, GET} route  0ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [RoutesResolver] UserController {/api/users}:  1ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [RouterExplorer] Mapped {/api/users, POST} route  1ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [RouterExplorer] Mapped {/api/users, GET} route  1ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [NestApplication] Nest application successfully started  3ms

CodePudding user response:

Try this. I downloaded your code an tested it. You have a permission problem. Remove container and delete docker volumes , then run docker compose with this changes in the Dockerfile:

###################
# BUILD FOR LOCAL DEVELOPMENT
###################

FROM node:18-alpine As development
USER root
# Create app directory
WORKDIR /usr/src/app

# Copy application dependency manifests to the container image.
# A wildcard is used to ensure copying both package.json AND package-lock.json (when available).
# Copying this first prevents re-running npm install on every code change.
COPY package*.json ./

# Install app dependencies using the `npm ci` command instead of `npm install`
RUN npm ci

# Bundle app source
COPY  . .
RUN npm  run build

# Use the node user from the image (instead of the root user)
USER node

CodePudding user response:

OK! This is very strange!

I probably solved this by deleting all files inside my project and recreating new files.

Some files, I copied from the tutorial repository below. Other files, I manually recreated. But in all I put the old content, without changes.

Maybe there was some file permission issue that didn't allow hot reloading. I am not sure! But the hot reload is working, for now.

https://www.tomray.dev/nestjs-docker-compose-postgres

  • Related