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


相关文章
|
1月前
|
Ubuntu Docker 容器
深入理解Dockerfile:构建镜像的详细解释与常用命令(下)
Docker 是一种流行的容器化平台,可将应用程序和其依赖项打包到一个独立的、可移植的容器中。Dockerfile 是构建 Docker 镜像的文本文件,它包含了一系列的指令和配置,用于定义镜像的构建过程。本文将深入解释 Dockerfile 的工作原理,并介绍常用的 Dockerfile 指令和构建命令,以帮助读者更好地理解和使用 Docker。
102 0
|
1月前
|
缓存 Shell Go
dockerfile笔记
dockerfile笔记
49 0
|
1月前
|
缓存 Ubuntu 应用服务中间件
dockerfile文件详解(常用命令)
dockerfile文件详解(常用命令)
65 0
|
1月前
|
应用服务中间件 nginx Docker
docker系列:dockerfile及其用法解析
docker系列:dockerfile及其用法解析
129 0
|
1月前
|
应用服务中间件 nginx Docker
深入理解Dockerfile:构建镜像的详细解释与常用命令(上)
Docker 是一种流行的容器化平台,可将应用程序和其依赖项打包到一个独立的、可移植的容器中。Dockerfile 是构建 Docker 镜像的文本文件,它包含了一系列的指令和配置,用于定义镜像的构建过程。本文将深入解释 Dockerfile 的工作原理,并介绍常用的 Dockerfile 指令和构建命令,以帮助读者更好地理解和使用 Docker。
782 0
|
11月前
|
Java Shell Linux
41-Dockerfile-Dockerfile简介
41-Dockerfile-Dockerfile简介
|
11月前
|
缓存 Shell Go
DockerFile文件详解
DockerFile文件详解
171 0
|
Linux Docker 容器
Dockerfile 文件结构、docker镜像构建过程详细介绍
本文是博主学习docker 镜像制作的记录,希望对大家有所帮助
272 0
Dockerfile 文件结构、docker镜像构建过程详细介绍
|
Ubuntu 应用服务中间件 Shell
认识 Dockerfile 文件之镜像构建
了解 Dockerfile 指令语法并熟练掌握,如何编写 Dockerfile 文件来定制一个镜像。
239 1
认识 Dockerfile 文件之镜像构建
|
Docker 容器
Dockerfile笔记
照着写就行
93 0
Dockerfile笔记