首页 > 解决方案 > node,js 在官方 node.js docker 镜像上找不到本地安装的模块

问题描述

即使模块目录是正在执行 npm 的子目录,node.js 似乎也无法找到本地安装的模块。我确实观察到发现全局安装的模块没有问题。我确实查看了这个 docker 图像的 Git 文档,但我没有发现任何表明我做错了什么的东西。 https://github.com/nodejs/docker-node/blob/main/README.md#how-to-use-this-image

以下是使用的文件和生成的日志。

更新找到的解决方案

谢谢大卫迷宫的帮助。

基本上,当我堆积 docker 卷时,我正在覆盖工作目录。在 Daivd 的帮助下,我发现您可以通过在初始卷挂载后添加额外的 docker 卷来重新加载现有的子目录。例如...

你可以挂载包含node.js代码的目录
-v /home/docker/node-app:/home/node/app

然后你可以挂载构建docker镜像时创建的node_modules目录在你挂载你的 node.js 代码之后。
-v /home/node/app/node_modules

这解决了我的问题。

Dockerfile

FROM node:14
WORKDIR /home/node/app
EXPOSE 3000
COPY package.json /home/node/app
RUN npm install
CMD ["npm", "start"]

包.json

{
    "name": "hosting-app",
    "version": "1.0.0",
    "description": "",
    "main": "app.js",
    "scripts": {
        "start": "nodemon app.js"
    },
    "author": "",
    "license": "ISC",
    "dependencies": {
        "express": "^4.17.1",
        "nodemon": "^2.0.9"
    }
}

码头工人命令

docker create --name=node-app -p 3000:3000 -v /home/docker/node-app:/home/node/app -e PUID=1000 -e PGID=1000 -e VERSION=docker --restart unless-stopped node-app:latest

docker 镜像构建日志

Running with gitlab-runner 14.0.1 (c1edb478)
  on docker02 shell exe 4F1NYSz3
Preparing the "shell" executor
00:00
Using Shell executor...
Preparing environment
00:00
Running on docker02...
Getting source from Git repository
00:00
Fetching changes with git depth set to 50...
Reinitialized existing Git repository in /home/gitlab-runner/builds/4F1NYSz3/0/game-hosting/nodejs-docker-image/.git/
Checking out da691440 as main...
Skipping Git submodules setup
Executing "step_script" stage of the job script
00:29
$ docker build -t node-app .
Step 1/6 : FROM node:14
14: Pulling from library/node
199ebcd83264: Pulling fs layer
ddbb155879c0: Pulling fs layer
c194bbaa3d8b: Pulling fs layer
6154ac87d7f3: Pulling fs layer
0c283e88ced7: Pulling fs layer
dba101298560: Pulling fs layer
3a17991c3d86: Pulling fs layer
50a7a006fc2a: Pulling fs layer
a79c5c44c974: Pulling fs layer
6154ac87d7f3: Waiting
0c283e88ced7: Waiting
dba101298560: Waiting
3a17991c3d86: Waiting
50a7a006fc2a: Waiting
a79c5c44c974: Waiting
c194bbaa3d8b: Verifying Checksum
c194bbaa3d8b: Download complete
ddbb155879c0: Verifying Checksum
ddbb155879c0: Download complete
199ebcd83264: Verifying Checksum
199ebcd83264: Download complete
dba101298560: Verifying Checksum
dba101298560: Download complete
199ebcd83264: Pull complete
ddbb155879c0: Pull complete
c194bbaa3d8b: Pull complete
3a17991c3d86: Verifying Checksum
3a17991c3d86: Download complete
50a7a006fc2a: Verifying Checksum
50a7a006fc2a: Download complete
a79c5c44c974: Verifying Checksum
a79c5c44c974: Download complete
6154ac87d7f3: Verifying Checksum
6154ac87d7f3: Download complete
6154ac87d7f3: Pull complete
0c283e88ced7: Verifying Checksum
0c283e88ced7: Download complete
0c283e88ced7: Pull complete
dba101298560: Pull complete
3a17991c3d86: Pull complete
50a7a006fc2a: Pull complete
a79c5c44c974: Pull complete
Digest: sha256:0c558991de47e614588a61f9cc90afdcf034c6fc641d3ab650c9570a98b37605
Status: Downloaded newer image for node:14
 ---> 813e282b6d18
Step 2/6 : WORKDIR /home/node/app
 ---> Running in 243bdb8356eb
Removing intermediate container 243bdb8356eb
 ---> b5062c68716c
Step 3/6 : EXPOSE 3000
 ---> Running in eb8c74a5419e
Removing intermediate container eb8c74a5419e
 ---> 2d91a4109c3e
Step 4/6 : COPY package.json /home/node/app
 ---> b1fee0b41d55
Step 5/6 : RUN npm install
 ---> Running in b417bc297123
> nodemon@2.0.12 postinstall /home/node/app/node_modules/nodemon
> node bin/postinstall || exit 0
Love nodemon? You can now support the project via the open collective:
 > https://opencollective.com/nodemon/donate
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@~2.3.2 (node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN hosting-app@1.0.0 No description
npm WARN hosting-app@1.0.0 No repository field.
added 167 packages from 85 contributors and audited 168 packages in 3.935s
11 packages are looking for funding
  run `npm fund` for details
found 0 vulnerabilities
Removing intermediate container b417bc297123
 ---> 8462204d5ee5
Step 6/6 : CMD ["npm", "start"]
 ---> Running in 259892da6614
Removing intermediate container 259892da6614
 ---> 785a4b48ca95
Successfully built 785a4b48ca95
Successfully tagged node-app:latest
Cleaning up file based variables
00:00
Job succeeded

码头集装箱日志

rest-api-docker@1.0.0 start /home/node/app
> nodemon app.js

sh: 1: nodemon: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! rest-api-docker@1.0.0 start: `nodemon app.js`
npm ERR! spawn ENOENT
npm ERR! 
npm ERR! Failed at the rest-api-docker@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2021-07-12T20_40_42_967Z-debug.log

标签: node.jsdocker

解决方案


推荐阅读