Skip to content

Commit

Permalink
Integrates Hadolint for Dockerfile linting (nebari-dev#917)
Browse files Browse the repository at this point in the history
* CI: Add Hadolint configuration and GitHub Action to lint Dockerfiles

* Fixing working directory path to run Hadolint

* fix hadolint file path

* DOCS: Add Hadolint specific docs in testing guide

* DOCS: Fix Vale errors

* Add a pre-commit hook for Hadolint

* Update hadolint pre-commit hooks

Co-authored-by: iameskild <eskild@doublee.io>
  • Loading branch information
HarshCasper and iameskild authored Nov 16, 2021
1 parent 2444a69 commit 07bba79
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,10 @@ jobs:
quay.io/${{ steps.meta.outputs.tags }}
push: ${{ github.event_name != 'pull_request' }}
labels: ${{ steps.meta.outputs.labels }}

- name: Lint Dockerfiles
uses: jbergstroem/hadolint-gh-action@v1
with:
dockerfile: ./qhub/template/\{\{\ cookiecutter.repo_directory\ \}\}/image/Dockerfile.${{ matrix.dockerfile }}
output_format: tty
error_level: 0
6 changes: 6 additions & 0 deletions .hadolint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
ignored:
- DL3048
- DL3008
- DL3013
- DL3003
17 changes: 14 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
- repo: https://github.com/psf/black
rev: 20.8b1
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
- repo: https://github.com/pycqa/flake8
rev: 3.8.4
hooks:
- id: flake8
- repo: https://github.com/hadolint/hadolint.git
rev: v2.3.0
hooks:
- id: hadolint-docker
entry: hadolint/hadolint:v2.3.0 hadolint
args: [
"--ignore", "DL3048",
"--ignore", "DL3008",
"--ignore", "DL3013",
"--ignore", "DL3003",
]
16 changes: 16 additions & 0 deletions docs/source/dev_guide/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ Then open the localhost (127.0.0.1) link that is in the terminal
[I 2021-04-05 17:37:17.346 ServerApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
```

## Linting Dockerfiles

To lint Dockerfiles, developers use a tool called [Hadolint](https://github.com/hadolint/hadolint). Hadolint is a Dockerfile linter that allows to discover issues with the Dockerfiles and recommends [best practices to be followed](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/). QHub CI automates Hadolint code reviews on every commit and pull request, reporting code style and error prone issues.

To run Hadolint locally you can either install it locally or use a container image. Instructions are available on the [install documentation for HadoLint](https://github.com/hadolint/hadolint#install). The `.hadolint.yml` on the root directory defines the ignored rules. To run Hadolint on Dockerfiles run:

```shell
hadolint ./qhub/template/\{\{\ cookiecutter.repo_directory\ \}\}/image/Dockerfile.conda-store
hadolint ./qhub/template/\{\{\ cookiecutter.repo_directory\ \}\}/image/Dockerfile.dask-gateway
hadolint ./qhub/template/\{\{\ cookiecutter.repo_directory\ \}\}/image/Dockerfile.dask-worker
hadolint ./qhub/template/\{\{\ cookiecutter.repo_directory\ \}\}/image/Dockerfile.jupyterhub
hadolint ./qhub/template/\{\{\ cookiecutter.repo_directory\ \}\}/image/Dockerfile.jupyterlab
```

Hadolint will report `error`, `warning`, `info` and `style` while linting Dockerfiles. In case of an error, the CI fails.

## Debug Kubernetes clusters

To debug Kubernetes clusters, we advise you to use [K9s](https://k9scli.io/), a terminal-based UI that aims to
Expand Down
1 change: 1 addition & 0 deletions tests/vale/styles/Google/Headings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ exceptions:
- Digital Ocean
- Spot
- Amazon Web Services
- Dockerfiles
2 changes: 2 additions & 0 deletions tests/vale/styles/vocab.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,5 @@ walkthrough
webapp
yaml
ClearML
Hadolint
Dockerfiles

0 comments on commit 07bba79

Please sign in to comment.