我正在尝试为我的应用程序创建一个Docker容器,但是我的Docker脚本似乎出现了错误。每当我运行命令docker build - < Dockerfile
时,都会得到以下输出:
我不确定为什么会这样,因为我的文件夹布局如下:
root folder, Docker ---
server ---- package.json
api
tests
如果文件夹布局有点混乱,我会将docker文件放在我的根文件夹中,并且在根文件夹中是一个名为 server 的文件夹,其中包含我的package.json,我的api文件和测试。
这是我的docker脚本:
# --- Base Node ---
FROM alpine:3.8 AS base
#install node
RUN apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/v3.7/main/ nodejs=8.9.3-r1 tini
# set working directory
WORKDIR /usr/src/app
# set tini as entrypoint
ENTRYPOINT ["/sbin/tini", "--"]
# copy project file
COPY . server/package*.json ./
# --- Dependencies ---
FROM base AS dependencies
# install node packages
RUN npm set progress=false && npm config set depth 0
RUN npm install .
# copy production node_modules aside
RUN cp -R node_modules prod_node_modules
# install ALL node_modules, including 'devDependencies'
RUN npm install
#
# ---- Test ----
# run linters, setup and tests
FROM dependencies AS test
COPY . .
RUN npm run lint && npm run test
#
# ---- Release ----
FROM base AS release
# copy production node_modules
COPY --from=dependencies /root/server/prod_node_modules ./node_modules
# copy app sources
COPY server/ ./
# expose port and define CMD
EXPOSE 3003
CMD [ "npm", "start" ]
我用过this question as an example.
为什么我从docker脚本中收到此错误?
答案 0 :(得分:1)
node_modules
的需求由npm install
根据解析package.json
生成。
但是,并非所有package.json
都会使npm install
生成此文件夹,只有带有devDependencies
,dependencies
的文件夹才能生成nodes_moduls
文件夹
我给您一个示例package.json
,它可以解决这个问题:
package.json,它将生成node_modules:
{
"name": "my-demo",
"version": "1.0.0",
"description": "a project",
"main": "index.js",
"scripts": {
"build": "weex-builder src dist",
"build_plugin": "webpack --config ./tools/webpack.config.plugin.js --color",
"dev": "weex-builder src dist -w",
"serve": "serve -p 8080"
},
"keywords": [
"weex"
],
"author": "xxx@gmail.com",
"license": "MIT",
"devDependencies": {
"babel-core": "^6.14.0"
},
"dependencies": {
"weex-html5": "^0.3.2"
}
}
以上版本的构建将以“ next”结束:
Step 8/10 : RUN npm install .
---> Running in 181f572843bc
> core-js@2.6.9 postinstall /usr/src/app/node_modules/core-js
> node scripts/postinstall || echo "ignore"
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN my-demo@1.0.0 No repository field.
added 53 packages in 6.491s
Removing intermediate container 181f572843bc
---> 016c98d9650d
Step 9/10 : RUN cp -R node_modules prod_node_modules
---> Running in c24631cc4bc6
Removing intermediate container c24631cc4bc6
---> de413db9140c
但是,如果您像下一个一样删除devDependencies
和dependencies
:
package.json不会生成node_modules:
{
"name": "my-demo",
"version": "1.0.0",
"description": "a project",
"main": "index.js",
"scripts": {
"build": "weex-builder src dist",
"build_plugin": "webpack --config ./tools/webpack.config.plugin.js --color",
"dev": "weex-builder src dist -w",
"serve": "serve -p 8080"
},
"keywords": [
"weex"
],
"author": "xxx@gmail.com",
"license": "MIT"
}
它将导致:
Step 8/10 : RUN npm install .
---> Running in 45031bd21886
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN my-demo@1.0.0 No repository field.
up to date in 0.115s
Removing intermediate container 45031bd21886
---> f88364d0725d
Step 9/10 : RUN cp -R node_modules prod_node_modules
---> Running in 16cd11546db0
cp: can't stat 'node_modules': No such file or directory
The command '/bin/sh -c cp -R node_modules prod_node_modules' returned a non-zero code: 1