nodejs项目使用docker部署为何失败?

nodejs使用thinkjs开发的项目docker-compose部署失败

我在用docker-compose部署一个node.js项目时遇见的无法部署,该项目之前使用pm2直接部署在宿主机是可以正常运行的,项目使用了thinkjs框架,下面的是我的docker-compose文件:

services:  
  node:  
    image: node:18.17.1  
    volumes:  
      - /data/jxxw/node/h5-api/:/data/jxxw/node  
    working_dir: /data/jxxw/node  
    command: npm start
    ports:
      - "8362:8362"

h5-api是我把node项目解压之后的文件夹,执行docker-compose up -d之后发现容器未启动,查看logs获得如下提示:

> jxxw-api@1.0.0 start
> node production.js

node:internal/modules/cjs/loader:1080
  throw err;
  ^

Error: Cannot find module 'thinkjs'
Require stack:
- /data/jxxw/node/production.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
    at Module._load (node:internal/modules/cjs/loader:922:27)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:121:18)
    at Object.<anonymous> (/data/jxxw/node/production.js:2:21)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/data/jxxw/node/production.js' ]
}

Node.js v18.17.1

看提示是未读取到thinkjs的依赖,据node开发同学反馈他的打包流程如下:
1.npm install
2.npm pack
之后就把生成的tgz文件给到我这边,
我尝试过写dockerfile文件解决,如下:

# 使用官方 Node.js 镜像
FROM node:18.17.1
# 设置工作目录
WORKDIR /data/jxxw/node
# 将当前目录下的所有内容都复制到位于 /app 的容器中
COPY . /data/jxxw/node
# 安装项目依赖
RUN npm install
# 暴露端口,供外部访问
EXPOSE 8362

提示install失败:

 => ERROR [4/4] RUN npm install                                                                       1542.3s
------
 > [4/4] RUN npm install:
1542.3 npm ERR! code ECONNREFUSED
1542.3 npm ERR! syscall connect
1542.3 npm ERR! errno ECONNREFUSED
1542.3 npm ERR! FetchError: request to https://registry.npmjs.org/ava failed, reason: connect ECONNREFUSED 104.16.28.34:443
1542.3 npm ERR!     at ClientRequest.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/minipass-fetch/lib/index.js:130:14)
1542.3 npm ERR!     at ClientRequest.emit (node:events:514:28)
1542.3 npm ERR!     at TLSSocket.socketErrorListener (node:_http_client:501:9)
1542.3 npm ERR!     at TLSSocket.emit (node:events:526:35)
1542.3 npm ERR!     at emitErrorNT (node:internal/streams/destroy:151:8)
1542.3 npm ERR!     at emitErrorCloseNT (node:internal/streams/destroy:116:3)
1542.3 npm ERR!     at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
1542.3 npm ERR!  FetchError: request to https://registry.npmjs.org/ava failed, reason: connect ECONNREFUSED 104.16.28.34:443
1542.3 npm ERR!     at ClientRequest.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/minipass-fetch/lib/index.js:130:14)
1542.3 npm ERR!     at ClientRequest.emit (node:events:514:28)
1542.3 npm ERR!     at TLSSocket.socketErrorListener (node:_http_client:501:9)
1542.3 npm ERR!     at TLSSocket.emit (node:events:526:35)
1542.3 npm ERR!     at emitErrorNT (node:internal/streams/destroy:151:8)
1542.3 npm ERR!     at emitErrorCloseNT (node:internal/streams/destroy:116:3)
1542.3 npm ERR!     at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
1542.3 npm ERR!   code: 'ECONNREFUSED',
1542.3 npm ERR!   errno: 'ECONNREFUSED',
1542.3 npm ERR!   syscall: 'connect',
1542.3 npm ERR!   address: '104.16.28.34',
1542.3 npm ERR!   port: 443,
1542.3 npm ERR!   type: 'system',
1542.3 npm ERR!   requiredBy: '.'
1542.3 npm ERR! }
1542.3 npm ERR! 
1542.3 npm ERR! If you are behind a proxy, please make sure that the
1542.3 npm ERR! 'proxy' config is set properly.  See: 'npm help config'
1542.3 
1542.3 npm ERR! A complete log of this run can be found in: /root/.npm/_logs/2024-01-30T14_55_34_130Z-debug-0.log
------
Dockerfile:8
--------------------
   6 |     COPY . /data/jxxw/node
   7 |     # 安装项目依赖
   8 | >>> RUN npm install
   9 |     # 暴露 8000 端口,供外部访问
  10 |     EXPOSE 8362
--------------------
ERROR: failed to solve: process "/bin/sh -c npm install" did not complete successfully: exit code: 1
ERROR: Service 'node' failed to build : Build failed

文件夹里面的内容:
image.png
image.png

麻烦大佬指点为何,个人理解是thinkjs没有被正确的install,如果我想只写docker-compose文件不写dockerfile文件应该如何操作

阅读 508
2 个回答

thinkjs 不会是给按到了全局了吧,你看看项目下有thinkjs吗

npm install 失败提示的是网络异常,可以换成淘宝源试试npm config set registry http://registry.npmmirror.com dockerfile 改写成这样。

# 使用官方 Node.js 镜像
FROM node:18.17.1
# 设置工作目录
WORKDIR /data/jxxw/node
# 将当前目录下的所有内容都复制到位于 /app 的容器中
COPY . /data/jxxw/node
# 安装项目依赖
RUN npm config set registry http://registry.npmmirror.com &&  npm install
# 暴露端口,供外部访问
EXPOSE 8362
CMD ["npm",  "start"]
推荐问题
宣传栏