gpt4 book ai didi

docker - 有没有办法对 Dockerfile 进行 lint?

转载 作者:IT老高 更新时间:2023-10-28 12:45:09 24 4
gpt4 key购买 nike

如果一个 Dockerfile 写有错误例如:

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

有没有办法在构建之前对其进行 lint 检测以检测此类错误?

最佳答案

试试:

我使用 RUNADDENVCMD dockerlinter 很聪明地将相同的违规行为分组在一起,但它无法像 hadolinter 那样彻底检查,这可能是由于缺少 Shellcheck静态分析 Bash 代码。

虽然 dockerlinter 在它可以 lint 的范围内不足,但它似乎确实更容易安装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.

2018 年更新。由于 hadolint 现在有官方的 Docker 仓库,你可以快速获取可执行文件:

id=$(docker create hadolint/hadolint:latest)
docker cp "$id":/bin/hadolint .
docker rm "$id"

或者你可以使用这个命令

docker container run --rm -i hadolint/hadolint hadolint - < Dockerfile

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

关于docker - 有没有办法对 Dockerfile 进行 lint?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28182047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com