Dockerfile相关内容分享-Hadolint

简介: Dockerfile相关内容分享-Hadolint

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,可用于后续的语义分析。


相关文章
|
Java 测试技术 Spring
Spring 支持的建议类型
【8月更文挑战第22天】
156 0
|
安全 Java
《Java安全编码标准》一导读
对于使用Java编程语言开发的软件系统,这个编码规范具有广泛的影响。
7443 0
|
Java 编译器
Lambda表达式
Lambda表达式
130 0
mac 终端命令kill掉某个指定端口
用mac电脑开发时,有时候会遇到端口占用的问题,导致我们,不得不去结束这个端口。 第一步在终端命令输入: lsof  -i : 端口号(如:lsof -i:8080) 第二步: kill -9 PID (如:kill -9 41848) 如果这个端口存在会出现如上列表,圈红框的那些pid号我们只需要使用 kill -9  PID (圈红框的编号:如:kill -9 41848)就可以结束这个端口进程了!如有多个pid就就需要多次执行kill -9 PID 相同的PID只需要执行一次就可以了。
7320 0
|
网络协议 虚拟化
虚拟机可以ping通主机,但是主机ping不通虚拟机解决办法
虚拟机可以ping通主机,但是主机ping不通虚拟机解决办法
6781 1
|
2天前
|
数据采集 人工智能 安全
|
12天前
|
云安全 监控 安全
|
3天前
|
自然语言处理 API
万相 Wan2.6 全新升级发布!人人都能当导演的时代来了
通义万相2.6全新升级,支持文生图、图生视频、文生视频,打造电影级创作体验。智能分镜、角色扮演、音画同步,让创意一键成片,大众也能轻松制作高质量短视频。
1023 151