Nodemon 未在 Docker 中重新启动 NodeJS 服务器

问题描述 投票:0回答:1

我正在尝试使用 Typescript 对 NodeJS Express 服务器进行 dockerize,但我无法让 nodemon 在发生更改时重新启动我的应用程序。我在 StackOverflow 上发现了有关此问题的其他几个问题,但没有一个真正对我有帮助。我相信我的配置有问题。

package.json

{
    "name": "backend",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "build": "tsc -p .",
        "dev:ts": "tsc -w",
        "dev:js": "nodemon --legacy-watch --watch ./src --watch ./app/src dist/index.js",
        "dev": "concurrently npm:dev:*",
        "start": "node dist/index.js"
    },
    "keywords": [],
    "author": "alessandrofoglia07",
    "license": "MIT",
    "dependencies": {
        "bcrypt": "^5.1.0",
        "cheerio": "^1.0.0-rc.12",
        "cors": "^2.8.5",
        "curse-filter": "^3.1.0",
        "dotenv": "^16.3.1",
        "express": "^4.18.2",
        "jsonwebtoken": "^9.0.1",
        "mongoose": "^7.4.1",
        "multer": "^1.4.5-lts.1",
        "node-cron": "^3.0.2",
        "nodemailer": "^6.9.4",
        "uuid": "^9.0.0",
        "zod": "^3.21.4"
    },
    "devDependencies": {
        "@types/bcrypt": "^5.0.0",
        "@types/cors": "^2.8.13",
        "@types/express": "^4.17.17",
        "@types/jsonwebtoken": "^9.0.2",
        "@types/leo-profanity": "^1.5.0",
        "@types/multer": "^1.4.7",
        "@types/node-cron": "^3.0.8",
        "@types/nodemailer": "^6.4.9",
        "@types/uuid": "^9.0.2",
        "@typescript-eslint/eslint-plugin": "^6.2.0",
        "@typescript-eslint/parser": "^6.2.0",
        "concurrently": "^8.2.0",
        "eslint": "^8.45.0",
        "nodemon": "^3.0.1",
        "typescript": "^5.1.6"
    },
    "type": "module"
}

tsconfig.json

{
    "compilerOptions": {
        "target": "es2016",
        "module": "ESNext",
        "rootDir": "./src",
        "moduleResolution": "NodeNext",
        "outDir": "./dist",
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "strict": true,
        "noImplicitAny": true,
        "noUncheckedIndexedAccess": true,
        "skipLibCheck": true
    }
}

Dockerfile.dev

FROM node:18
WORKDIR /app
COPY package*.json .
RUN npm install
COPY . .
ENV PORT 5000
ENV NODE_ENV development
EXPOSE $PORT
CMD ["npm", "run", "dev"]

docker-compose.yml

version: '3'
services:
    frontend:
        container_name: luxedge-frontend
        tty: true
        stdin_open: true
        env_file:
            - ./frontend/.env
    backend:
        container_name: luxedge-backend
        tty: true
        stdin_open: true
        env_file:
            - ./backend/.env
        ports:
            - '5000:5000'

docker-compose-dev.yml

version: '3'
services:
    frontend:
        build:
            context: ./frontend
            dockerfile: Dockerfile.dev
        ports:
            - '3000:3000'
        volumes:
            - ./frontend/src:/app/src:ro
            - ./frontend/public:/app/public:ro
            - ./frontend/index.html:/app/index.html:ro
    backend:
        restart: always
        build:
            context: ./backend
            dockerfile: Dockerfile.dev
        volumes:
            - ./backend/src:/app/src:ro
            - ./backend/public:/app/public:ro
            - /app/node_modules
        environment:
            - NODE_ENV=development
            - CHOKIDAR_USEPOLLING=true

我用来启动前端和后端容器的命令

docker-compose -f docker-compose.yml -f docker-compose-dev.yml up -d

我在 Windows x64 上使用带有 WSL2 的 Docker 桌面。

node.js docker docker-compose docker-volume nodemon
1个回答
0
投票

因为Windows的文件系统与容器不同。 Docker 容器原生于 Linux 系统,因此文件系统事件(即文件更改事件、文件删除事件等)无法被主机 (Windows) 识别。

如果您将项目复制到 WSL2 系统(Ubuntu / Debian / 您正在使用的任何发行版)中,那么它将可以工作。现在容器的文件系统将与主机(WSL)系统匹配。

我不久前在 LinkedIn 上写了一篇关于这个主题的文章。您可以查看以进一步阅读。

© www.soinside.com 2019 - 2024. All rights reserved.