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 [email protected] No description npm WARN [email protected] 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
找到原因了,是因为我自己的 base image 的 Dockerfile ,设置了 Volumn /apps 。 但是为什么这样就会导致用到这个base image 的容器镜像 /apps/express-upload/ 下的 node_modules 不见呢? 我留出了 /apps ,但是没有使用啊。还是没有搞懂。还请长的帅的解释下
![]() | 1 blackeeper 2021-12-03 16:08:38 +08:00 你可以查一下,安装到系统默认的路径:/usr/lib/node_modules |
![]() | 2 selfcreditgiving OP @blackeeper 也没有装到那里 ls: cannot access '/usr/lib/node_modules': No such file or directory 只是换了一下 From 的 base image ,就没有 node_modules 了,node:12 就是正常的 |
![]() | 3 blackeeper 2021-12-03 16:14:40 +08:00 @selfcreditgiving 可以找找 /usr/local/lib/node_modules 或者 /lib/node_modules |
![]() | 4 selfcreditgiving OP @blackeeper 这两个下面也都没有。 设置了 WORKDIR 而且 package.json package-lock.json 都是复制到 ./ 下面的(即 WORKDIR ) 应该安装也是安装到 WORKDIR 目录下的吧 |
![]() | 5 selfcreditgiving OP 找到原因了,是因为我自己的 base image 的 Dockerfile ,设置了 Volumn /apps 。 但是为什么这样就会导致 /apps/express-upload/ 下的 node_modules 不见呢? 还是没有搞懂  |
![]() | 6 blackeeper 2021-12-03 17:16:50 +08:00 一个是镜像,一个是容器,你是不是运行容器的时候把 /apps 目录挂载了,这样就覆盖了 @selfcreditgiving |
![]() | 7 CokeMine 2021-12-03 17:19:41 +08:00 via Android 在 Dockerfile 里 写了 Volumn 会自动挂载一个匿名卷,不管 run 的时候有没有挂载 |