Photo by Rob Coates on Unsplash
Linters don't just enforce style guidelines, they also catch potential issues. hadolint (Haskell Dockerfile Linter) is the most popular linter for Dockerfiles, and it's incredibly easy to use.
hadolint parses your Dockerfiles into an abstract syntax tree and then checks rules on that tree. Not only does it have its own rules specific to Dockerfiles, it also uses ShellCheck to lint RUN instructions.
Usage
Assuming you already have Docker installed because we're talking about Dockerfiles, the easiest way to run hadolint is with Docker:
$ docker run --rm --interactive hadolint/hadolint < DockerfileWhere Dockerfile is a file that exists in your working directory, outside of the container.
There are some other ways to install hadolint, but running it from Docker is probably the easiest way to go.
Configuration
There are really only two things you can configure about hadolint:
- What
hadolintand ShellCheck rules to ignore - What container registries are "trusted"
Both of those can be configured in a $PWD/.hadolint.yaml file like so:
1ignored:
2 - DL3003
3 - DL3018
4
5trustedRegistries:
6 - docker.io
Or you can inline-configure ignored rules like so:
1# hadolint ignore=DL3007
2FROM alpine:latest
3
4# hadolint ignore=DL3003,DL3018
5RUN apk --update add --no-cache git && \
6 cd "$(mktemp -d)" && \
7 git pull "https://github.com/hadolint/hadolint.git"
Example output
Taking the above example and removing the ignored rules:
1FROM alpine:latest
2
3RUN apk --update add --no-cache git && \
4 cd "$(mktemp -d)" && \
5 git pull "https://github.com/hadolint/hadolint.git"
Here's the example output:
$ docker run --rm --interactive hadolint/hadolint < Dockerfile
/dev/stdin:1 DL3007 Using latest is prone to errors if the image will ever update. Pin the version explicitly to a release tag
/dev/stdin:3 DL3003 Use WORKDIR to switch to a directory
/dev/stdin:3 DL3018 Pin versions in apk add. Instead of `apk add <package>` use `apk add <package>=<version>`Adding to CircleCI
Linters become a lot more powerful when you add them to your CI pipeline. A linter without enforcement will surely be ignored over time.
Here's a sample of how you could add hadolint to a CircleCI .circleci/config.yml:
1version: 2.1
2
3executors:
4 docker:
5 docker:
6 - image: docker:stable
7
8jobs:
9 lint:
10 executor: docker
11 steps:
12 - checkout
13 - setup_remote_docker
14 - run:
15 name: Run hadolint
16 command: docker run --rm --interactive hadolint/hadolint < Dockerfile
17
18workflows:
19 version: 2
20 test:
21 jobs:
22 - lint
See "Publishing Docker Images with CircleCI" for a more complete guide on using CircleCI to publish Docker images.

Publishing Docker Images with CircleCI
Feb 15, 2021 · 9 min read
Publishing Docker images is a common CI/CD task, and the flexibility CircleCI offers makes it a great tool for the job.
Conclusion
Linters are a great tool to prevent team arguments over style, but they're a great tool for preventing potential errors - start using hadolint with your Dockerfiles today!



