首页 > 解决方案 > Docker 没有正确执行 RUN 命令,也没有读取 dockerignore 文件

问题描述

这里的 RUN 命令没有按我预期的方式执行:

/tmp $ express -c --view=ejs test-app

  warning: the default view engine will not be jade in future releases
  warning: use `--view=jade' or `--help' for additional options


   create : test-app/
   create : test-app/public/
   create : test-app/public/javascripts/
   create : test-app/public/images/
   create : test-app/public/stylesheets/
   create : test-app/public/stylesheets/style.css
   create : test-app/routes/
   create : test-app/routes/index.js
   create : test-app/routes/users.js
   create : test-app/views/
   create : test-app/views/error.jade
   create : test-app/views/index.jade
   create : test-app/views/layout.jade
   create : test-app/app.js
   create : test-app/package.json
   create : test-app/bin/
   create : test-app/bin/www

   change directory:
     $ cd test-app

   install dependencies:
     $ npm install

   run the app:
     $ DEBUG=test-app:* npm start

/tmp $ cd test-app/
/tmp/test-app $ ll | grep node_modules
/tmp/test-app $ vim Dockerfile
/tmp/test-app $ cat Dockerfile
FROM node:12.15-alpine AS development
WORKDIR /usr/src/app
ENV NODE_ENV development
COPY . .
RUN npm install
/tmp/test-app $ docker build -t test-app:development --target development .
Sending build context to Docker daemon  17.41kB
Step 1/5 : FROM node:12.15-alpine AS development
 ---> afd897e3184b
Step 2/5 : WORKDIR /usr/src/app
 ---> Using cache
 ---> b635d27109d9
Step 3/5 : ENV NODE_ENV development
 ---> Using cache
 ---> d8358f2dc7b3
Step 4/5 : COPY . .
 ---> a02fff431f86
Step 5/5 : RUN npm install
 ---> Running in 8cb422535183
npm WARN deprecated jade@1.11.0: Jade has been renamed to pug, please install the latest version of pug instead of jade
npm WARN deprecated constantinople@3.0.2: Please update to at least constantinople 3.1.1
npm WARN deprecated transformers@2.1.0: Deprecated, use jstransformer
npm notice created a lockfile as package-lock.json. You should commit this file.
added 99 packages from 139 contributors and audited 194 packages in 23.436s
found 4 vulnerabilities (3 low, 1 critical)
  run `npm audit fix` to fix them, or `npm audit` for details
Removing intermediate container 8cb422535183
 ---> 727228b0222f
Successfully built 727228b0222f
Successfully tagged test-app:development
/tmp/test-app $ docker run --rm -it --init -v "${PWD}:/usr/src/app" -p 3000:3000 test-app:development ls -l | grep node_modules
/tmp/test-app $ docker run --rm -it --init -v "${PWD}:/usr/src/app" -p 3000:3000 test-app:development pwd
/usr/src/app

从上面继续,这里 dockerignore 中的文件不会被忽略:

/tmp/test-app $ vim .dockerignore
/tmp/test-app $ cat .dockerignore
node_modules
/tmp/test-app $ npm install
npm WARN deprecated jade@1.11.0: Jade has been renamed to pug, please install the latest version of pug instead of jade
npm WARN deprecated transformers@2.1.0: Deprecated, use jstransformer
npm WARN deprecated constantinople@3.0.2: Please update to at least constantinople 3.1.1
npm notice created a lockfile as package-lock.json. You should commit this file.
added 99 packages from 139 contributors and audited 194 packages in 6.48s
found 4 vulnerabilities (3 low, 1 critical)
  run `npm audit fix` to fix them, or `npm audit` for details
/tmp/test-app $ ll | grep node_modules
drwxr-xr-x  94 user  wheel   3196 12 Feb 01:42 node_modules
/tmp/test-app $ docker build -t test-app:development --target development .
Sending build context to Docker daemon  45.57kB
Step 1/5 : FROM node:12.15-alpine AS development
 ---> afd897e3184b
Step 2/5 : WORKDIR /usr/src/app
 ---> Using cache
 ---> b635d27109d9
Step 3/5 : ENV NODE_ENV development
 ---> Using cache
 ---> d8358f2dc7b3
Step 4/5 : COPY . .
 ---> 90a3b228a863
Step 5/5 : RUN npm install
 ---> Running in e65e1c18dc6a
added 99 packages from 139 contributors and audited 194 packages in 4.355s
found 4 vulnerabilities (3 low, 1 critical)
  run `npm audit fix` to fix them, or `npm audit` for details
Removing intermediate container e65e1c18dc6a
 ---> a5fd730535ce
Successfully built a5fd730535ce
Successfully tagged test-app:development
/tmp/test-app $ docker run --rm -it --init -v "${PWD}:/usr/src/app" -p 3000:3000 test-app:development ls -l | grep node_modules
drwxr-xr-x   94 root     root          3196 Feb 11 18:42 node_modules

这是怎么回事?

为什么 RUN 命令不在容器中安装节点模块?

为什么 .dockerignore 中的文件夹会被复制到容器中?

标签: node.jslinuxdocker

解决方案


您正在将本地主机目录挂载到容器中,从而将所有内容隐藏在容器的/usr/src/app文件夹中。在第一种情况下,您没有安装依赖项,因此node_modules找不到该文件夹​​,因为它不在您的主机目录中。在第二种情况下,您在node_modules本地拥有该文件夹并将其安装到容器中。.dockerignore工作正常。

从您的命令中删除该-v选项docker run,一切都会按预期工作。


推荐阅读