Docker NPM安装不起作用,我怎么解决?

x33g5p2x  于 2022-10-23  发布在  Docker
关注(0)|答案(2)|浏览(342)

我扩展了我的上下文,因为我需要Dockerfile目录之外的一些文件。
这是我的命令:docker Build-f Dockerfile../../docker run(ImageID)
文件结构:

  • 型号
  • 服务器
  • 订阅(我在此目录中运行docker命令)
  • 文档文件
  • index.js
  • Package.json
  • Package-lock.json

在我的文档文件中:

FROM node:10

WORKDIR /app

COPY ./servers/subscribeship/package*.json .

RUN npm install

COPY ./servers/subscribeship .

COPY ./models ../../models

EXPOSE 3000

CMD ["node","index.js"]

构建映像的日志:

+] Building 7.5s (11/11) FINISHED                                                                                                                         
 => [internal] load build definition from Dockerfile                                                                                                  0.0s
 => => transferring dockerfile: 231B                                                                                                                  0.0s
 => [internal] load .dockerignore                                                                                                                     0.0s
 => => transferring context: 34B                                                                                                                      0.0s
 => [internal] load metadata for docker.io/library/node:10                                                                                            2.7s
 => [1/6] FROM docker.io/library/node:10@sha256:59531d2835edd5161c8f9512f9e095b1836f7a1fcb0ab73e005ec46047384911                                      0.0s
 => [internal] load build context                                                                                                                     1.3s
 => => transferring context: 23.27MB                                                                                                                  1.3s
 => CACHED [2/6] WORKDIR /app                                                                                                                         0.0s
 => [3/6] COPY ./servers/subscribeship/package*.json .                                                                                                0.1s
 => [4/6] RUN npm install                                                                                                                             2.6s
 => [5/6] COPY ./servers/subscribeship .                                                                                                              0.4s
 => [6/6] COPY ./models ../../models                                                                                                                  0.0s 
 => exporting to image                                                                                                                                0.3s 
 => => exporting layers                                                                                                                               0.3s 
 => => writing image sha256:9a87c205096b94e442a9f40e7c050ac68383f2da7fd8b285b5ee840c20f922af

运行容器的日志:

internal/modules/cjs/loader.js:638
    throw err;
    ^

Error: Cannot find module 'sequelize'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/models/index.js:5:19)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)

似乎NPM安装不起作用。Package.json和Package-lock.json确实有序列化的包

如果我的方法错了,我会用同样的方法来测试,但它似乎是有效的。
我创建了另一个目录调用错误,与服务器级别相同。然后在Bug文件夹中创建bug1目录,如下所示:
命令:

docker build -f Dockerfile ../../
docker run (imageId)
  • 错误
  • 错误1(在此处运行命令)
  • 文档文件
  • Package.json
  • pakage-lock.json
  • .dockerIgnore

Dockerfile:

FROM node:10

WORKDIR /app

COPY ./bug/bug1/package.json .

RUN npm install

COPY ./bug/bug1 .

EXPOSE 3000

CMD ["node","index.js"]

ma8fv8wu

ma8fv8wu1#

./表示您从当前目录导航。如果您在/servers/subscribeship中运行命令,./将引用此目录。然后,./servers/subscribeship/package*.json将引用/servers/subscribeship/servers/subscribeship/package*.json。因为该目录不存在,所以不复制Package.json,并且NPM安装不会安装所需的模块。

rm5edbpk

rm5edbpk2#

我解决了我的问题。结果是,模型目录中没有Package.json。

相关问题