node.js - 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
- 源中还没有 node_modules 文件夹,因此 copy 不会将其复制到容器中
- 看起来 npm install 从 dockerfile 运行
- 但是 node_modules 文件夹不在容器中
从上面继续,这里 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
- 现在 node_modules 文件夹在源代码中,但它在 .dockerignore 文件中,所以不应该被复制到容器中
- 再次看起来 npm install 发生了,但这次没有警告
- 但是这次 node_modules 文件夹在那里,所以看起来它被复制了,即使它在 .dockerignore 文件中
这是怎么回事?
为什么 RUN 命令不在容器中安装节点模块?
为什么 .dockerignore 中的文件夹会被复制到容器中?
解决方案
您正在将本地主机目录挂载到容器中,从而将所有内容隐藏在容器的/usr/src/app
文件夹中。在第一种情况下,您没有安装依赖项,因此node_modules
找不到该文件夹,因为它不在您的主机目录中。在第二种情况下,您在node_modules
本地拥有该文件夹并将其安装到容器中。.dockerignore
工作正常。
从您的命令中删除该-v
选项docker run
,一切都会按预期工作。
推荐阅读
- javascript - 给定两个数字,返回一个长度为 num1 且每个值为 nu 的数组
- javascript - touchmove 事件未在 google chrome for android 上触发
- google-analytics - 即使过滤器不匹配,Google 标记也会触发
- javascript - 获取在iframe中打开的url的内容
- reactjs - React 响应式 Orinetation 道具以 null 开头
- php - Mysqli db 到 db 复制和奇怪的字符集行为
- java - 更改 TextInputLayout 提示行为
- java - zk 如何使用 mvvm 为每个或模板在 zul 中生成组件?
- javascript - 从 Http (ng2) 升级到 HttpClient (ng6) 后登录请求的未定义响应
- python - 在不打开终端的情况下运行 Python 脚本