美文网首页
Dockerfile相关内容分享-Hadolint

Dockerfile相关内容分享-Hadolint

作者: 糖醋沼跃鱼 | 来源:发表于2022-04-14 17:28 被阅读0次

Dockerfile相关内容分享-Hadolint,以及DL4006解决方法

如何解决Hadolint:DL4006问题?

Set the SHELL option -o pipefail before RUN with a pipe in

这个问题大致就是说,平时使用的管道命令,如果表达式出错了,只会报管道符号右侧的表达式,左侧的如果出现失败是不报错的。推荐奖shell option 添加-o pipefail,那么只要出错就会报错。我一开始是这么写的
RUN set -o pipefail,会出现其他问题。正解如下

SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN wget -O - https://some.site | wc -l > /number

什么是Hadolint

通过智能的Dockerfile linter可以帮我们构建更加合理的Docker镜像,linter会解析Dockerfile到AST,基于AST的规则上执行还能借助ShellCheck去检查RUN指令里面的bash代码。简单的说,Hadolint就好像是一种规范检查,和Dockerfile的写法推荐。

比如一个原始dockerfile是这样的

FROM debian
RUN export node_version="0.10" \
&& apt-get update && apt-get -y install nodejs="$node_verion"
COPY package.json usr/src/app
RUN cd /usr/src/app \
&& npm install node-static

EXPOSE 80000
CMD ["npm", "start"]

经过hadolint检查之后(并非dockerfile不能正常构建),会给予推荐提示

[Always tag the version of an image explicitly](https://github.com/hadolint/hadolint/wiki/DL3009)
FROM debian
[node_verion is referenced but not assigned (did you mean 'node_version'?).](https://github.com/koalaman/shellcheck/wiki/SC2154)
[Delete the apt-get lists after installing something](https://github.com/hadolint/hadolint/wiki/DL3009)
[Avoid additional packages by specifying `--no-install-recommends`](https://github.com/hadolint/hadolint/wiki/DL3015)
RUN export node_version="0.10" \
&& apt-get update && apt-get -y install nodejs="$node_verion"
COPY package.json usr/src/app
[Use WORKDIR to switch to a directory](https://github.com/hadolint/hadolint/wiki/DL3003)
[Pin versions in npm. Instead of `npm install <package>` use `npm install <package>@<version>`](https://github.com/hadolint/hadolint/wiki/DL3016)
RUN cd /usr/src/app \
&& npm install node-static

[Valid UNIX ports range from 0 to 65535](https://github.com/hadolint/hadolint/wiki/DL3011)
EXPOSE 80000
CMD ["npm", "start"]

上面提到的AST是什么

Abstract Syntax Tree, AST语法树,以树状形式表示编程语言的语法结构。一般通过语法解析器分析创建出分析树,分析树生成AST,可用于后续的语义分析。

相关文章

网友评论

      本文标题:Dockerfile相关内容分享-Hadolint

      本文链接:https://www.haomeiwen.com/subject/jrjqertx.html