Dockerfile 里面 RUN npm install 后,找不到 node_modules?

2021-12-03 15:45:26 +08:00
 selfcreditgiving

Dockerfile 是这样的(直接复制的 node 官方的 Dockerfile 示例)
链接: https://nodejs.org/en/docs/guides/nodejs-docker-webapp/

#FROM node:12
FROM my-node-ubuntu-image

# Create app directory
WORKDIR /apps/express-upload

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

RUN ls -al

# Bundle app source
COPY . .

EXPOSE 8080
CMD [ "node", "app.js" ]

现在问题是:
使用官方一样的 base image node:12 就可以在 RUN ls -al 那一步看到有 node_modules 文件夹,
但是用我自己创建的 base image my-node-ubuntu-image (是装有 node 的),就找不到 node_modules 文件夹了?

docker build 语句:

docker build . -t node-web-app:0.05

执行结果: (可以看到 npm install 是执行了的,但是下面的 RUN ls -al 没有 node_modules )

Sending build context to Docker daemon  121.1MB
Step 1/8 : FROM my-node-ubuntu-image
 ---> 4d66ffd553f3
Step 2/8 : WORKDIR /apps/express-upload
 ---> Using cache
 ---> 61e3227e0d01
Step 3/8 : COPY package*.json ./
 ---> 47e0df84b2e3
Step 4/8 : RUN npm install
 ---> Running in 55437825f54f
npm WARN nodejs@1.0.0 No description
npm WARN nodejs@1.0.0 No repository field.

added 224 packages from 267 contributors and audited 224 packages in 8.51s
found 3 vulnerabilities (1 low, 2 moderate)
  run `npm audit fix` to fix them, or `npm audit` for details
Removing intermediate container 55437825f54f
 ---> a8e5ce1df24a
Step 5/8 : RUN ls -al
 ---> Running in a96450a9b1ba
total 76
drwxr-xr-x 2 root root  4096 Dec  3 07:17 .
drwxr-xr-x 3 root root  4096 Dec  3 07:17 ..
-rw-r--r-- 1 root root 63608 Dec  1 18:34 package-lock.json
-rw-r--r-- 1 root root   615 Dec  3 06:24 package.json
Removing intermediate container a96450a9b1ba
 ---> 12e7d8dea420
Step 6/8 : COPY . .
 ---> 60aa2971f746
Step 7/8 : EXPOSE 8080
 ---> Running in d9693d5e39c4
Removing intermediate container d9693d5e39c4
 ---> 3039a0c5d66c
Step 8/8 : CMD [ "node", "app.js" ]
 ---> Running in e925d8d9e250
Removing intermediate container e925d8d9e250
 ---> 69941e3a6915
Successfully built 69941e3a6915
Successfully tagged node-web-app:0.05

1073 次点击
所在节点    问与答
7 条回复
blackeeper
2021-12-03 16:08:38 +08:00
你可以查一下,安装到系统默认的路径:/usr/lib/node_modules
selfcreditgiving
2021-12-03 16:11:46 +08:00
@blackeeper 也没有装到那里

ls: cannot access '/usr/lib/node_modules': No such file or directory

只是换了一下 From 的 base image ,就没有 node_modules 了,node:12 就是正常的
blackeeper
2021-12-03 16:14:40 +08:00
@selfcreditgiving 可以找找 /usr/local/lib/node_modules 或者 /lib/node_modules
selfcreditgiving
2021-12-03 16:21:49 +08:00
@blackeeper 这两个下面也都没有。
设置了 WORKDIR 而且 package.json package-lock.json 都是复制到 ./ 下面的(即 WORKDIR )
应该安装也是安装到 WORKDIR 目录下的吧
selfcreditgiving
2021-12-03 17:06:22 +08:00
找到原因了,是因为我自己的 base image 的 Dockerfile ,设置了 Volumn /apps 。 但是为什么这样就会导致 /apps/express-upload/ 下的 node_modules 不见呢? 还是没有搞懂

![base-image-volumn-apps.png]( https://cdn.jsdelivr.net/gh/luobin100/gofly@main/base-image-volumn-apps.png)
blackeeper
2021-12-03 17:16:50 +08:00
一个是镜像,一个是容器,你是不是运行容器的时候把 /apps 目录挂载了,这样就覆盖了
@selfcreditgiving
CokeMine
2021-12-03 17:19:41 +08:00
在 Dockerfile 里 写了 Volumn 会自动挂载一个匿名卷,不管 run 的时候有没有挂载

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/819817

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX