Dockerfile 里面 RUN npm install 后,找不到 node_modules? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
selfcreditgiving
V2EX    问与答

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

  •  
  •   selfcreditgiving 2021-12-03 15:45:26 +08:00 1708 次点击
    这是一个创建于 1416 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 
    第 1 条附言    2021-12-03 17:11:31 +08:00

    找到原因了,是因为我自己的 base image 的 Dockerfile ,设置了 Volumn /apps 。 但是为什么这样就会导致用到这个base image 的容器镜像 /apps/express-upload/ 下的 node_modules 不见呢? 我留出了 /apps ,但是没有使用啊。还是没有搞懂。还请长的帅的解释下

    base-image-volumn-apps.png

    7 条回复    2021-12-03 17:19:41 +08:00
    blackeeper
        1
    blackeeper  
       2021-12-03 16:08:38 +08:00
    你可以查一下,安装到系统默认的路径:/usr/lib/node_modules
    selfcreditgiving
        2
    selfcreditgiving  
    OP
       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
        3
    blackeeper  
       2021-12-03 16:14:40 +08:00
    @selfcreditgiving 可以找找 /usr/local/lib/node_modules 或者 /lib/node_modules
    selfcreditgiving
        4
    selfcreditgiving  
    OP
       2021-12-03 16:21:49 +08:00
    @blackeeper 这两个下面也都没有。
    设置了 WORKDIR 而且 package.json package-lock.json 都是复制到 ./ 下面的(即 WORKDIR )
    应该安装也是安装到 WORKDIR 目录下的吧
    selfcreditgiving
        5
    selfcreditgiving  
    OP
       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
        6
    blackeeper  
       2021-12-03 17:16:50 +08:00
    一个是镜像,一个是容器,你是不是运行容器的时候把 /apps 目录挂载了,这样就覆盖了
    @selfcreditgiving
    CokeMine
        7
    CokeMine  
       2021-12-03 17:19:41 +08:00 via Android
    在 Dockerfile 里 写了 Volumn 会自动挂载一个匿名卷,不管 run 的时候有没有挂载
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2559 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 11:24 PVG 19:24 LAX 04:24 JFK 07:24
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86