LoginSignup
1
1

GitHub Actions Hadolint でDockerfileを静的解析する

Last updated at Posted at 2024-05-09

概要

Hadolint(Haskell Dockerfile Linter)
https://github.com/hadolint/hadolint

HadolintはHaskellで実装されているDockerfileのLinterツールです。
ベストプラクティスなDockerイメージの構築を手伝ってくれる便利なツールです。

Gitシリーズ記事まとめ

利用するアクション

Hadolintのアクションが用意されているのでこちらを使います。

実装

  • .github/workflows/hadolint-testing.yaml
.github/workflows/hadolint-testing.yaml
name: Hadolint Testing
on:
  pull_request:
    types: [synchronize, opened, reopened]
jobs:
  hadolint-testing:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: hadolint/hadolint-action@v3.1.0
        with:
          recursive: true

recursive オプションを有効にすると Dockerfile を再帰的に検索してくれます。
本当は複数のDockerfileをパス指定したかったのですが、オプションとしては用意されてなさそうでした。

Dockerfileの配置場所としては下記の3箇所に置きましたが問題なく検査してくれました。

infra
└── docker
   ├── mysql
   │  └── Dockerfile
   ├── nginx
   │  └── Dockerfile
   └── php
      └── Dockerfile

実行結果

ScreenShot 2024-05-09 13.32.02.png

実行自体は秒で終わってます。

Error: infra/docker/php/Dockerfile:19 DL3008 warning: Pin versions in apt get install. Instead of `apt-get install <package>` use `apt-get install <package>=<version>`
Error: infra/docker/php/Dockerfile:19 DL3009 info: Delete the apt-get lists after installing something

エラーについてはHadolintのWikiに直し方も合わせて記載があります。

DL3009 info: Delete the apt-get lists after installing something に関してはこれです。

ルール変更したい場合

プロジェクトルートに .hadolint.yaml を配置するか config オプションで設定ファイルを参照してくれます。

詳細はこちらです。
https://github.com/hadolint/hadolint#configure

今回は特にルールは変更しませんでした。

業務改善度: ★★★★★

これを設定しておけば綺麗なDockerfileを書かざるを得なくなるのでおすすめです。
もっと早く設定しておけばよかった!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1