GCP Compute Engine VM中Docker容器启动失败:找不到index.js模块
问题描述
将Docker容器部署到GCP Cloud Compute Engine VM后无法启动,但本地Mac M3芯片机器上可正常运行。报错显示找不到/app/build/src/index.js模块,错误日志如下:
Node.js v22.3.0 > test-fastify@1.0.0 start > node build/src/index.js node:internal/modules/cjs/loader:1215 throw err; ^ Error: Cannot find module '/app/build/src/index.js' at Module._resolveFilename (node:internal/modules/cjs/loader:1212:15) at Module._load (node:internal/modules/cjs/loader:1038:27) at wrapModuleLoad (node:internal/modules/cjs/loader:212:19) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:158:5) at node:internal/main/run_main_module:30:49 { code: 'MODULE_NOT_FOUND', requireStack: [] }
相关配置文件
Dockerfile
FROM node:22 # Create and change to the app directory. WORKDIR /app COPY package*.json ./ # Install production dependencies. RUN npm install # Copy local code to the container image. COPY . . EXPOSE 3000 # Run the web service on container startup. CMD [ "npm", "run", "build" ] CMD [ "npm", "run", "start" ]
package.json
{ "name": "test-fastify", "version": "1.0.0", "main": "index.js", "scripts": { "build": "tsc -p tsconfig.json", "dev": "ts-node-dev --respawn src/index.ts", "start": "node build/src/index.js", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "description": "", "dependencies": { "@fastify/cookie": "^9.3.1", "@fastify/cors": "^9.0.1", "@fastify/mysql": "^4.3.0", "@fastify/session": "^10.9.0", "@fastify/type-provider-typebox": "^4.0.0", "@sinclair/typebox": "^0.32.30", "dotenv": "^16.4.5", "fastify": "^4.27.0", "firebase": "^10.12.2", "firebase-admin": "^12.1.1", "kysely": "^0.27.3", "mysql2": "^3.9.7", "nodemon": "^3.1.0", "pino": "^9.1.0", "short-uuid": "^5.2.0", "uninstall": "^0.0.0", "@fastify/swagger": "^8.14.0", "@fastify/swagger-ui": "^3.0.0" }, "devDependencies": { "@fastify/autoload": "^5.8.2", "@types/node": "^20.12.11", "@typescript-eslint/eslint-plugin": "^7.11.0", "@typescript-eslint/parser": "^7.11.0", "eslint": "^8.57.0", "eslint-config-prettier": "^9.1.0", "prettier": "^3.2.5", "ts-node-dev": "^2.0.0", "typescript": "^5.4.5" } }
tsconfig.json
{ "compilerOptions": { /* Language and Environment */ "target": "es2017" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, /* Modules */ "module": "commonjs" /* Specify what module code is generated. */, "rootDir": "./" /* Specify the root folder within your source files. */, // "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */ "baseUrl": "./" /* Specify the base directory to resolve non-relative module names. */, "outDir": "./build" /* Specify an output folder for all emitted files. */, "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */, "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */, /* Type Checking */ "strict": true /* Enable all strict type-checking options. */, "skipLibCheck": true /* Skip type checking all .d.ts files. */ } }
本地执行docker build -t gcr.io/some-project-id/fastify-backend-v1 .构建镜像并运行正常,但部署到GCP VM后失败。
解决方案
1. 修复Dockerfile的CMD指令问题
Dockerfile中多个CMD指令只有最后一个会生效,你写的CMD ["npm", "run", "build"]根本没执行,直接跳过编译步骤就启动start命令,导致build目录不存在。
有两种修复方式:
- 方式一:用shell命令串联build和start,启动容器时依次执行
CMD ["sh", "-c", "npm run build && npm run start"]
- 方式二:在构建阶段提前执行build,启动时直接运行编译好的代码(更高效,build步骤会被缓存)
FROM node:22 WORKDIR /app COPY package*.json ./ RUN npm install COPY . . # 构建阶段执行TypeScript编译 RUN npm run build EXPOSE 3000 CMD ["npm", "run", "start"]
2. 优化tsconfig配置(可选)
当前rootDir设为./会把所有文件(包括node_modules)都编译到build目录,建议把rootDir改为./src,只编译源码:
"rootDir": "./src"
对应package.json的start脚本要改成:
"start": "node build/index.js"
这样能避免不必要的文件被编译,也更符合项目结构规范。
3. 验证GCP镜像完整性
确认GCP VM上拉取的镜像和本地构建的是同一版本,可通过docker images查看镜像ID是否一致,若不一致,重新推送镜像到GCR:
docker push gcr.io/some-project-id/fastify-backend-v1
然后在GCP VM上重新拉取最新镜像再启动容器。
内容的提问来源于stack exchange,提问作者cornstar94
相关产品推荐
相关产品推荐

