Dockerfile Linting and Security
Intermediate10 min
Lint Dockerfiles for best practice violations and security issues using Hadolint to catch problems before building.
Prerequisites
- -Docker installed
Steps
1
Lint a Dockerfile with Hadolint
Run Hadolint to check for Dockerfile best practice violations.
$ docker run --rm -i hadolint/hadolint < Dockerfile
Hadolint checks shell commands with ShellCheck and validates Dockerfile instructions against best practices.
2
Lint with specific rules ignored
Suppress specific rules that do not apply to your use case.
$ docker run --rm -i hadolint/hadolint hadolint --ignore DL3008 --ignore DL3009 - < Dockerfile
DL3008 warns about pinning apt package versions. DL3009 warns about deleting apt lists.
3
Check for USER instruction
Verify the Dockerfile does not run as root in production.
$ grep -n 'USER' Dockerfile || echo 'WARNING: No USER instruction found. Container will run as root.'
Running containers as root is a security risk. Always add a USER instruction for production images.
4
Verify no secrets in build args
Check that no sensitive values are passed as build arguments, which are visible in image history.
$ docker history myapp:latest --no-trunc | grep -i 'ARG\|ENV' | grep -i 'password\|secret\|key\|token' || echo 'No obvious secrets found'
Full Script
FAQ
Discussion
Loading comments...