0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

BitBucket Pipelines + hadolintでDockerfileのLintを組み込む

Posted at

概要

https://github.com/hadolint/hadolint/ でDockerfileをLintに通して静的解析できる
個人的にはBitbucketを使っていて、CI回せていないのでこれを機にBitBucket Pipelines(ここら辺を参考にさせて頂きました)を導入したりもしたのでメモ

hadolintの使い方

macなら brew install hadolint で使える
自分の環境で出た内容

$ hadolint Dockerfile
Dockerfile:4 DL3018 Pin versions in apk add. Instead of `apk add <package>` use `apk add <package>=<version>`
Dockerfile:7 SC1090 Can't follow non-constant source. Use a directive to specify location.
Dockerfile:7 SC2039 In POSIX sh, 'source' in place of '.' is undefined.
Dockerfile:8 DL3013 Pin versions in pip. Instead of `pip install <package>` use `pip install <package>==<version>`
Dockerfile:10 DL3013 Pin versions in pip. Instead of `pip install <package>` use `pip install <package>==<version>`

dockerイメージで実行

docker run -v $PWD:/root --rm hadolint/hadolint:latest-debian hadolint /root/Dockerfile

でも使える。hadolintのバージョンを固定したい時はこっちでtag指定した方がいい

rule一覧

https://github.com/hadolint/hadolint/wiki
https://github.com/koalaman/shellcheck/wiki

hadolintはShellCheckというシェルスクリプトの構文Lintツールと同じ解析をする

DL3018

Alpine Linuxなどを使っているとapkというパッケージ管理ツールを使って必要なパッケージをダウンロードしたりするが、hadolintのruleではDockerfileに apk add {インストールしたいパッケージ} 書く時は <package>=<version> とバージョンを指定した記法にすることが求められる。環境間の差異は仮想環境構築時には確かによくないので従った書き方にした。

DL3013

DL3018と同じでpythonのパッケージ管理ツールpipでインストールするライブラリのバージョンを固定しなさいというもの

SC1090

https://github.com/koalaman/shellcheck/wiki/SC1090
shellcheckのruleでは $APP_HOME/hoge のように変数を使ってruntimeに値の決まる動的なパスを許していない。これはignoreした

##hadolintのruleをオフにしたい時

# hadolint ignore=SC1090 と書くか、 .hadolint.yaml

ignored:
  - SC1090

と書く

プロジェクトにhadolintを置いたのでBitBucket Pipelinesを始めた

BitBucket Pipelines

https://bitbucket.org/{BitBuckerのユーザーネーム}/{レポジトリ名}/addon/pipelines/home あたりから設定を追加できます。
bitbucket-pipelines.ymlをレポジトリのトップに置くことでCIを設定できますが、BitbucketのUI上からテンプレートを指定して追加できます

クリーンショット 2018-11-24 19.57.35.jpg

bitbucket-pipelines.yml

bitbucket-pipelines.yml

# This is a sample build configuration for Python.
# Check our guides at https://confluence.atlassian.com/x/x4UWN for more examples.
# Only use spaces to indent your .yml configuration.
# -----
# You can specify a custom docker image from Docker Hub as your build environment.
image: hadolint/hadolint:latest-debian

pipelines:
  default:
    - step:
        script: # Modify the commands below to build your repository.
          - hadolint Dockerfile

適当なブランチにbitbucket-pipelines.ymlを置いてpushしてもPipelinesはスタートせず、web上から設定したら動き出した。なぜ…

Bitbucket Pipelinesはdockerイメージを指定して動かす。自前のdockerイメージをビルドさせて動かすこともできるらしいけどそこまでやっていない
hadolint/hadolint:latest もあるけど hadolint/hadolint:latest-debian でないと動かなかった

実行結果

スクリーンショット 2018-11-24 20.28.08__.jpg

hadolintを実行して何も問題がないと何も出力されない(なのでキャプチャは問題なし)

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?