hadolint で Dockerfile の構文をチェックする
Today I Learned
Dockerfile を使った GitHub Actions の Actions を作成している。
Dockerfile は見よう見まねで書いているので、ベストプラクティスに沿った書き方をしたい。
そんなときに利用できるのが、hadolint は、Dockerfile の Lint ツールである。
hadolint をインストールする
Mac であれば homebrew を使ってインストールできる。
$ brew install hadolint
hadolint を実行する
Dockerfile をチェックするには、Dockefile があるディレクトリで hadolint Dockerfile
を実行するだけ。
hadolint でチェックできる内容は、Rules に記載されている。
たとえば、Docker イメージのバージョンで latest
を指定した Dockerfile に対して、hadolint を実行したとする。
Dockerfile
FROM node:latest
すると、warning が出力される。latest
を指定すると、Docker イメージがアップデートしたときに勝手にバージョンがあがってしまう。
バグの温床になりかねないため、バージョンを指定するほうが良いとされている。
$ hadolint Dockerfile
Dockerfile:1 DL3007 warning: Using latest is prone to errors if the image will ever update. Pin the version explicitly to a release tag
GitHub Actions で hadolint を実行する
GitHub Actions で自動でチェックしたい場合には、hadolint-action を利用できる。
たとえば、Pull Request で Dockerfile が変更されていたら hadolint を実行したい場合には、次のワークフローを作成する。
hadolint.yml
name: hadolint
on:
pull_request:
paths:
- "Dockerfile"
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]
- uses: hadolint/hadolint-[email protected]
with:
dockerfile: Dockerfile
先ほどの latest
を指定した Dockerfile を含む Pull Request の CI は、エラーで落ちるようになる。