有没有办法lint Dockerfile?

elo*_*one 13 docker dockerfile

如果Dockerfile写错了,例如:

CMD ["service", "--config", "/etc/service.conf] (缺少报价)

有没有办法在建造之前将它弄脏以检测到这样的错误?

Dev*_*evy 13

尝试:

我用,和RUN,对一个简单的Docker文件进行了一个简单的测试.很聪明地将相同的规则违规归为一类,但由于缺乏对Bash代码的静态分析,因此无法尽可能彻底地进行检查.ADDENVCMDdockerlinterhadolinterShellcheck

尽管dockerlinter它的范围不足,但它似乎更容易安装.npm install -g dockerlinter将会这样做,而编译hadolinter需要一个Haskell编译器和构建环境,需要永远编译.

$ hadolint ./api/Dockerfile
L9 SC2046 Quote this to prevent word splitting.
L11 SC2046 Quote this to prevent word splitting.
L8 DL3020 Use COPY instead of ADD for files and folders
L10 DL3020 Use COPY instead of ADD for files and folders
L13 DL3020 Use COPY instead of ADD for files and folders
L18 DL3020 Use COPY instead of ADD for files and folders
L21 DL3020 Use COPY instead of ADD for files and folders
L6 DL3008 Pin versions in apt get install. Instead of `apt-get install <package>` use `apt-get install <package>=<version>`
L6 DL3009 Delete the apt-get lists after installing something
L6 DL3015 Avoid additional packages by specifying `--no-install-recommends`

$ dockerlint ./api/Dockerfile
WARN:  ADD instruction used instead of COPY on line 8, 10, 13, 18, 21
ERROR: ./api/Dockerfile failed.
Run Code Online (Sandbox Code Playgroud)

2018年更新.由于hadolint现在有官方Docker存储库,您可以快速获取可执行文件:

id=$(docker create hadolint/hadolint:latest)
docker cp "$id":/bin/hadolint .
docker rm "$id"
Run Code Online (Sandbox Code Playgroud)

这是一个静态编译的可执行文件(根据ldd hadolint),所以它应该运行,无论安装的库.关于如何构建可执行文件的参考:https://github.com/hadolint/hadolint/blob/master/docker/Dockerfile.

  • @LuísBianchin是的,它是由同一作者提供的 - 只要您信任将Dockerfile粘贴到第三方,当然. (3认同)

Abd*_*aly -1

我不太熟悉go,但看起来您可以简单地调用Parse方法,就像在此处的测试套件中所做的那样。如果没有返回错误,那么你的 lint 就通过了。我假设在开发过程中暴露给脚本或调用的东西是微不足道的。