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


相关文章
|
缓存 监控 网络协议
Dockerfile文件全面详解
Docker 可以通过读取 Dockerfile 中的指令自动构建镜像。Dockerfile 是一个文本文档,其中包含了用户创建镜像的所有命令和说明。
487 0
|
缓存 Shell Go
DockerFile文件详解
DockerFile文件详解
214 0
|
运维 应用服务中间件 Linux
四、DockerFile解析
四、DockerFile解析
|
应用服务中间件 Linux nginx
|
Ubuntu 容器
Dockerfile 解析--文件结构
Dockerfile 解析--文件结构
175 0
|
Java Shell Linux
41-Dockerfile-Dockerfile简介
41-Dockerfile-Dockerfile简介
|
应用服务中间件 Docker 容器
docker 自动构建,基于Dockerfile文件
原文地址:http://kekefund.com/2017/03/06/docker-dockerfile-generated/ 1,Dockerfile的编写 在centos中创建一个目录:/mydata/data/dockertest/,新建Dockerfile文件 vim Dockerfile # Verison 0.
1318 0
|
Linux 应用服务中间件 开发工具
DockerFile 学习
DockerFile 学习
191 0
DockerFile 学习
|
运维 Java Shell
Dockerfile学习
Dockerfile学习笔记
|
Ubuntu 应用服务中间件 Shell
认识 Dockerfile 文件之镜像构建
了解 Dockerfile 指令语法并熟练掌握,如何编写 Dockerfile 文件来定制一个镜像。
272 1
认识 Dockerfile 文件之镜像构建

热门文章

最新文章

相关实验场景

更多