在这里,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中
到底怎么回事?
为什么运行命令没有在容器中安装节点模块?
为什么.dockerignore中的文件夹被复制到容器中?
发布于 2020-02-11 19:31:57
您正在将本地主机目录安装到容器中,从而将容器的/usr/src/app文件夹中的所有内容隐藏起来。在第一种情况下,您没有安装依赖项,因此找不到node_modules文件夹,因为它不在宿主目录中。在第二种情况下,您在本地拥有node_modules文件夹,并将其挂载到容器中。.dockerignore运行良好。
从-v命令中删除docker run选项,一切都将按预期工作。
https://stackoverflow.com/questions/60175720
复制相似问题