16

用 Hadolint 检查 Dockerfile

 4 years ago
source link: https://www.lbbniu.com/8244.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Hadolint 是使用不明觉厉的 Haskell 实现的 Dockerfile linter,其实现依据来自于 Docker 官网推荐的 Dockerfile 最佳实践。

在 Mac 下的安装只要使用简单的brew install hadolint就能够完成安装,其它平台也有各自的支持方式。

用法非常简单:hadolint <dockerfile>即可,例如我们编写一个简单的 Dockerfile:

$ cat << EOF > /tmp/Dockerfile                                                                             master  ✱
heredoc> FROM alpine
heredoc> CMD ["sleep", "3600"]
heredoc> EOF

$ hadolint /tmp/Dockerfile
/tmp/Dockerfile:1 DL3006 Always tag the version of an image explicitly

可以使用–ignore参数忽略指定的问题,如果是固定配置,可以使用-c参数指定配置文件。例如下面的配置文件中,指定了忽略的问题列表以及可信的镜像仓库:

ignored:
  - DL3000
  - SC1010

trustedRegistries:
  - docker.io
  - my-company.com:5000

其检测范围包括在 README 有非常详细的描述,并在连接中给出了建议。

hadolint 提供了很多集成选项,可以集成到 Jenkins、Gitlab 等自动化流程中使用,还能在 vim、VSCode、Atom 等编辑器中集成,从而在开发阶段就对 Dockerfile 进行检查,提高镜像质量。

  • README: https://github.com/hadolint/hadolint/blob/master/README.md

  • 集成选项: https://github.com/hadolint/hadolint/blob/master/docs/INTEGRATION.md


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK