V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
selfcreditgiving
V2EX  ›  问与答

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

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

    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   ·   我们的愿景   ·   实用小工具   ·   2812 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 15:00 · PVG 23:00 · LAX 08:00 · JFK 11:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.