0

My dockerfile is

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

FROM node:18-alpine As development

# 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 --chown=node:node package*.json ./

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

# Bundle app source
COPY --chown=node:node . .

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

###################
# BUILD FOR PRODUCTION
###################

FROM node:18-alpine As build

WORKDIR /usr/src/app

COPY --chown=node:node package*.json ./

# In order to run `npm run build` we need access to the Nest CLI.
# The Nest CLI is a dev dependency,
# In the previous development stage we ran `npm ci` which installed all dependencies.
# So we can copy over the node_modules directory from the development image into this build image.
COPY --chown=node:node --from=development /usr/src/app/node_modules ./node_modules

COPY --chown=node:node . .

# Run the build command which creates the production bundle
RUN npm run build

# Set NODE_ENV environment variable
ENV NODE_ENV production

# Running `npm ci` removes the existing node_modules directory.
# Passing in --only=production ensures that only the production dependencies are installed.
# This ensures that the node_modules directory is as optimized as possible.
RUN npm ci --only=production && npm cache clean --force

USER node

###################
# PRODUCTION
###################

FROM node:18-alpine As production

# Copy the bundled code from the build stage to the production image
COPY --chown=node:node --from=build /usr/src/app/node_modules ./node_modules
COPY --chown=node:node --from=build /usr/src/app/dist ./dist

# Start the server using the production build
CMD [ "node", "dist/main.js" ]

My docker-compose.yml

version: "3.9"
  services:
    nest_app:
      container_name: nest_app
      build:
        context: .
        dockerfile: Dockerfile
        # Only will build development stage from our dockerfile
        target: development
        args:
          - NEST_APP_PORT=${NEST_APP_PORT}
      volumes:
        - .:/usr/src/app
      # Run a command against the development stage of the image
      command: npm run start:dev
      ports:
        - '${NEST_APP_PORT}:${NEST_APP_PORT}'
      networks:
        - nest_try_2
  networks:
    nest_try_2:
      driver: bridge

My package.json

    {
    "name": "nest_try_2",
    "version": "0.0.1",
    "description": "",
    "author": "",
    "private": true,
    "license": "UNLICENSED",
    "scripts": {
        "build": "nest build",
        "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
        "start": "nest start",
        "start:dev": "nest start --watch",
        "start:debug": "nest start --debug --watch",
        "start:prod": "node dist/main",
        "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
        "test": "jest",
        "test:watch": "jest --watch",
        "test:cov": "jest --coverage",
        "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
        "test:e2e": "jest --config ./test/jest-e2e.json"
    },
    "dependencies": {
        "@nestjs/common": "^9.0.0",
        "@nestjs/core": "^9.0.0",
        "@nestjs/platform-express": "^9.0.0",
        "reflect-metadata": "^0.1.13",
        "rxjs": "^7.2.0"
    },
    "devDependencies": {
        "@nestjs/cli": "^9.0.0",
        "@nestjs/schematics": "^9.0.0",
        "@nestjs/testing": "^9.0.0",
        "@types/express": "^4.17.13",
        "@types/jest": "29.5.1",
        "@types/node": "18.16.12",
        "@types/supertest": "^2.0.11",
        "@typescript-eslint/eslint-plugin": "^5.0.0",
        "@typescript-eslint/parser": "^5.0.0",
        "eslint": "^8.0.1",
        "eslint-config-prettier": "^8.3.0",
        "eslint-plugin-prettier": "^4.0.0",
        "jest": "29.5.0",
        "prettier": "^2.3.2",
        "source-map-support": "^0.5.20",
        "supertest": "^6.1.3",
        "ts-jest": "29.1.0",
        "ts-loader": "^9.2.3",
        "ts-node": "^10.0.0",
        "tsconfig-paths": "4.2.0",
        "typescript": "^5.0.0"
    },
    "jest": {
        "moduleFileExtensions": [
        "js",
        "json",
        "ts"
        ],
        "rootDir": "src",
        "testRegex": ".*\\.spec\\.ts$",
        "transform": {
        "^.+\\.(t|j)s$": "ts-jest"
        },
        "collectCoverageFrom": [
        "**/*.(t|j)s"
        ],
        "coverageDirectory": "../coverage",
        "testEnvironment": "node"
    }
}

I follow this tutorial https://www.tomray.dev/nestjs-docker-compose-postgres but I think it is related to some ts or nest versions because the same Dockerfile and docker-compose.yml but it is not working the hot reload. Because, the service is running and when I change files content I could verify the changes are reflected in docker image files but it is not triggering the rebuilding

2 Answers2

0

You have to specify volumes separately in docker compose file (Try with named volumes). Below link may help.

https://www.freecodecamp.org/news/how-to-enable-live-reload-on-docker-based-applications/

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 27 '23 at 07:52
0

I have solved it by using this

 "watchOptions": {
    // Use native file system events for files and directories
    "watchFile": "priorityPollingInterval",
    "watchDirectory": "dynamicprioritypolling",
    // Poll files for updates more frequently
    // when they're updated a lot.
    "fallbackPolling": "dynamicPriority",
    // Don't coalesce watch notification
    "synchronousWatchDirectory": true,
    // Finally, two additional settings for reducing the amount of possible
    // files to track  work from these directories
    "excludeDirectories": ["**/node_modules", "dist"]
  }

https://stackoverflow.com/a/74919692/8062335 as far as I understand is that is watching the changes but it would be better is someone can explain it in more details.