Dockerfileint
is an npm module that analyzes a Dockerfile and looks for common traps, mistakes and helps enforce best practices:
Start unit tests with npm test
From the command line:
./bin/dockerfilelint <path/to/Dockerfile>
- This should be the first command in the Dockerfile
- Base image should specify a tag
- Base image should not use latest tag
- Support
FROM scratch
without a tag - Support the
FROM <image>@<digest>
syntax - Allow config to specify "allowed" base layers
- Should be followed by exactly 1 parameter (@ sign)
- sudo is not included in the command
- apt-get [install | upgrade | remove] should include a -y flag
- apt-get install commands should include a
--no-install-recommends
flag - apt-get install commands should be paired with a
rm -rf /var/lib/apt/lists/*
in the same layer - Avoid running
apt-get upgrade
orapt-get dist-upgrade
- Never run
apt-get update
withoutapt-get install
on the same line - apk add commands should include a
--no-cache
flag or be paired with an--update
flag withrm -rf /var/cache/apk/*
in the same layer - apk add support for --virtual flag
- handle best practices for yum operations and cleanup
- Only a single
CMD
layer is allowed - Better handling of escaped quotes
- Detect exec format with expected variable substitution
- Format should be key=value
- Only the container port should be listed
- All ports should be exposed in a single cache layer (line)
- The same port number should not be exposed multiple times
- Exposed ports should be numeric and in the accepted range
- Format of
ENV
- Best practice of only using a single
ENV
line to reduce cache layer count
- Command should have at least 2 parameters
- Source command(s) cannot be absolute or relative paths that exist outside of the current build context
- Commands with wildcards or multiple sources require that destination is a directory, not a file
- If an
ADD
command could be aCOPY
, thenCOPY
is preferred - Using
ADD
to fetch remote files is discouraged because they cannot be removed from the layer
- Implement checking (similar to ADD)
- Do not
COPY
multiple files on a single command to best use cache
- Support
- Format
- Any build steps after VOLUME is declare should not change VOLUME contents
- If JSON format, double quotes are required
- Should be followed by exactly 1 parameter
- Validate that it has exactly 1 parameter
-
WORKDIR
can only expand variables previously set inENV
commands
- Support
- Prevent redefining the built in ARGs (proxy)
- Support
- Validate input
- Only present one time
- Only valid Dockerfile commands are present
- All commands should have at least 1 parameter
- Check that commands are written as upper case commands