skip to Main Content

So i’ve been developing this backend on NestJS and Docker using docker-compose also for about 6 months now but never had issues with my docker not listening to my file changes. A few days ago, this started happening and i dont know why.

This is my Dockerfile

FROM node:14-alpine

ENV NODE_ENV development

ENV NODE_OPTIONS=--max_old_space_size=8192

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

COPY . .

RUN yarn install

ENTRYPOINT ["yarn", "start:dev"]

And this is my docker-compose file

services:
  api:
    image: cacao-be
    container_name: cacao-be-api
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - '3000:3000'
    depends_on:
      - postgres
    volumes:
      - ./src:/usr/src/app/src
    networks:
      - default
    links:
      - redis
    env_file:
      - .env
volumes:
  db_data:
  admin_data:
  redis_data:
    driver: local

This is my package.json which includes the script to start my proyect

"scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write "src/**/*.ts" "test/**/*.ts"",
    "start": "nest start",
    "start:dev": "nest start --watch"
}

HereĀ“s also my tsconfig file

{
  "compilerOptions": {
    "jsx": "react",
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
},
}

And tsconfig.build.json file

{
  "extends": "./tsconfig.json",
  "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}

I’ve tried running my proyect without docker and it did listen to file changes, but when i run it in docker it stops listening to them. I’ve verifyed and in both approaches it runs in watch mode.
Starting compilation in watch mode...

Also, i’ve entered to my container via cmd, and checked if files were changing as i modified them, and they did (i could prove that my service was well mounted). So inside the container file changes but it doesnt restart the development

I’ve set

command: npm run start:dev

in my docker-compose file, but it didn’t work. Cleaning my docker and restarting it also didnt work.

What i expect is that when i save a file, my docker logs restart and apply changes.

2

Answers


  1. Same issue here with NestJS services in Docker-compose.
    Hot reload stops working few days ago.

    Dockerfile :

    FROM node:16-alpine
    WORKDIR /usr/local/app
    
    COPY package.json .
    RUN npm install
    COPY . .
    
    CMD ["npm", "run", "start:dev"]
    

    docker-compose.yml file

      back:
        build: ./back
        restart: unless-stopped
        hostname: back
        ports: #For health check
          - "3001:3001"
        volumes:
          - ./back:/usr/local/app
          - node_modules_back:/usr/local/app/node_modules
        networks:
          - backend
        depends_on:
          db:
            condition: service_healthy
        links:
          - db
    

    NestJS scripts

    "scripts": {
        "prebuild": "rimraf dist",
        "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",
        "ci": "npm run build && npm run format && npm run lint && npm run test && npm audit"
      },
    
    Login or Signup to reply.
  2. Sorry for being late to the party. Actually it’s due to the TypeScript version. If you upgrade it to 4.9, then it won’t work anymore. Because in 4.9, they changed how files should be watched, and they’re now using FS events instead of legacy polling. You can read more about it here. So if you want to make it work again, use 4.8.4 for now. In your package.json, change the typescript version like so:

    {
      ...
      "devDependencies": {
        ...
        "typescript": "~4.8.4",
        ...
      },
      ...
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search