We're excited to announce that you can now lint your Dockerfile on every build with Depot! No more configuring a separate Dockerfile linter outside or before your build in CI.
Now available in depot v.2.17.0 is the ability to lint a Dockerfile on any Docker image build you do with Depot. This means you can now run a Dockerfile lint on every build with Depot! No more configuring a Dockerfile linter outside of your actual image build.
How it works
The new Dockerfile linter uses the popular open-source project, hadolint, which helps you build best practice Docker images. It's a fast, lightweight, and easy-to-use linter already used in the Docker community. We're also very excited to be sponsoring the project.
Dockerfile linting with Depot
To use the new Dockerfile linter, you can add the --lint flag to your depot build or depot bake commands:
The linter runs before the build starts. It outputs any lint errors, warnings, or info messages at the end of the Docker image build. For example, we see here it's complaining about my Docker image build having back-to-back run instructions.
The Dockerfile linting doesn't fail the build by default. But, you can enable this behavior via the --lint-fail-on flag:
Here we can see that the build failed because we enabled the --lint-fail-on flag with the info level. This value can be info, warning, or error depending on your goal.
Linting a Dockerfile in CI
If you're not using GitHub Action for continuous integration, you can use the new flags as shown above.
For GitHub Actions, we've updated our depot/build-push-action and depot/bake-action actions to support these new flags. You can enable the Dockerfile linter by setting the lint input to true:
Here is an example of linting a Dockerfile using our build-push-action. It does a git clone, sets up our CLI, and builds the docker image with Dockerfile linting enabled:
Dockerfile lint functionality works with our bake-action as well. This allows you to build best practice docker images for all images in a build definition file. See our post on building many images via bake for more details.
Why we built this
Linting a Dockerfile is a great way to catch errors during static analysis. It improves the quality of your images and helps you build best practice Docker images. It's one of those best practices that improve code quality and catches mistakes during a code review. But it's an extra hop away today. You have to run hadolint dockerfile outside your actual build process.
With Depot, we are already providing you with a faster experience for building your Docker container, so adding the ability to lint your Dockerfile on every build was a natural next step for us. We want to make it as easy as possible for you to build high-quality Docker images as fast as possible, and this is an excellent step in that direction.