kubernetes Docker应该在找不到“package.json”但没有抛出错误时抛出错误

ie3xauqp  于 2023-08-03  发布在  Kubernetes
关注(0)|答案(1)|浏览(119)

我正在学习Docker,并创建了一个非常简单的镜像,创建了一个带有节点依赖项下载的容器。

见下面的docker镜像(Dockerfile):

FROM node:14-alpine
RUN npm install
CMD ["npm", "start"] here

字符串
该目录包含以下文件Dockerfileindex.jspackage.json。这三个文件都在同一级别。

当我运行docker build .时,我预计步骤2会抛出一个错误,指示package.json未被找到,因为从步骤1创建的临时容器不应该有package.json文件,但我没有得到这个错误,相反构建成功完成。有人能解释一下为什么构建成功完成而我没有得到任何错误?
查看下面构建的输出:

[+] Building 4.2s (6/6) FINISHED                                                                                              docker:desktop-linux
 => [internal] load build definition from Dockerfile                                                                                          0.0s
 => => transferring dockerfile: 95B                                                                                                           0.0s
 => [internal] load .dockerignore                                                                                                             0.0s
 => => transferring context: 2B                                                                                                               0.0s
 => [internal] load metadata for docker.io/library/node:14-alpine                                                                             0.9s
 => [1/2] FROM docker.io/library/node:14-alpine@sha256:434215b487a329c9e867202ff89e704d3a75e554822e07f3e0c0f9e606121b33                       2.6s
 => => resolve docker.io/library/node:14-alpine@sha256:434215b487a329c9e867202ff89e704d3a75e554822e07f3e0c0f9e606121b33                       0.0s
 => => sha256:4cf6a83c0e2af3c780abcda02cc33f9e812fdcb40b610ed1838281cc9ab94ec8 2.43MB / 2.43MB                                                0.2s
 => => sha256:434215b487a329c9e867202ff89e704d3a75e554822e07f3e0c0f9e606121b33 1.43kB / 1.43kB                                                0.0s
 => => sha256:3380ed827b250d2db2bd38c15f090af2b303b3c9ebb42f5927cb5b9adeea7a6e 1.16kB / 1.16kB                                                0.0s
 => => sha256:d561716d42b6a03639723c382c62977347ed62f13925e836614583253308e6c3 6.45kB / 6.45kB                                                0.0s
 => => sha256:c41833b44d910632b415cd89a9cdaa4d62c9725dc56c99a7ddadafd6719960f9 3.26MB / 3.26MB                                                0.2s
 => => sha256:683339ce8d6b9be2ca150a8de67b895e20ea5594b91d3911c95b0b8fea3e314c 36.99MB / 36.99MB                                              1.3s
 => => extracting sha256:c41833b44d910632b415cd89a9cdaa4d62c9725dc56c99a7ddadafd6719960f9                                                     0.1s
 => => sha256:686172e40c38722891b4004f55f6447548c8367968ac523a612591e0d92f9db3 447B / 447B                                                    0.3s
 => => extracting sha256:683339ce8d6b9be2ca150a8de67b895e20ea5594b91d3911c95b0b8fea3e314c                                                     1.2s
 => => extracting sha256:4cf6a83c0e2af3c780abcda02cc33f9e812fdcb40b610ed1838281cc9ab94ec8                                                     0.0s
 => => extracting sha256:686172e40c38722891b4004f55f6447548c8367968ac523a612591e0d92f9db3                                                     0.0s
 => [2/2] RUN npm install                                                                                                                     0.7s
 => exporting to image                                                                                                                        0.0s 
 => => exporting layers                                                                                                                       0.0s 
 => => writing image sha256:9ffbc17f35fbca106a7a540b8433b8e5144d792244a35aa140d1c57bd1b8387c

fafcakar

fafcakar1#

package.json丢失时,该图像中的npm install似乎不会返回错误代码。
尝试在该映像中启动一个shell并运行npm install,然后检查退出代码:

$ docker run --rm -it node:14-alpine sh
/ # npm install
npm WARN saveError ENOENT: no such file or directory, open '/package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/package.json'
npm WARN !invalid#2 No description
npm WARN !invalid#2 No repository field.
npm WARN !invalid#2 No README data
npm WARN !invalid#2 No license field.

up to date in 0.208s
found 0 vulnerabilities

/ # echo $?
0

字符串
关键部分是最后一行,它表明尽管输出上有警告,但命令仍成功完成。
由于该命令没有出现错误,Docker相信它是成功的,并继续构建镜像。
请注意,这种行为似乎在npm的更新版本中有所改变。例如,使用最新图像:

$ docker run --rm -it node:20-alpine sh
/ # npm install
npm ERR! Tracker "idealTree" already exists

npm ERR! A complete log of this run can be found in: /root/.npm/_logs/2023-07-07T01_02_38_828Z-debug-0.log
/ # echo $?
1


所以如果你要使用那个版本的图像,它会像你预期的那样失败。

相关问题