Sign inSign up

datoma/hadolint

By datoma

โ€ขUpdated almost 5 years ago

hadolint as Docker Container

Image
0

1.6K

datoma/hadolint repository overview

Linux/OSX Build Status Windows Build status GPL-3 licensed GitHub release Github downloads pipecat

โ Haskell Dockerfile Linter

A smarter Dockerfile linter that helps you build best practiceโ  Docker images. The linter is parsing the Dockerfile into an AST and performs rules on top of the AST. It is standing on the shoulders of ShellCheckโ  to lint the Bash code inside RUN instructions.

:globe_with_meridians: Check the online version on hadolint.github.io/hadolintโ  Screenshot

โ How to use

You can run hadolint locally to lint your Dockerfile.

hadolint <Dockerfile>
hadolint --ignore DL3003 --ignore DL3006 <Dockerfile> # exclude specific rules
hadolint --trusted-registry my-company.com:500 <Dockerfile> # Warn when using untrusted FROM images

Docker comes to the rescue to provide an easy way how to run hadolint on most platforms. Just pipe your Dockerfile to docker run:

docker run --rm -i hadolint/hadolint < Dockerfile

โ Install

You can download prebuilt binaries for OSX, Windows and Linux from the latest release pageโ . However, if it doesn't work for you, please fall back to Docker, brew or source installation.

If you are on OSX you can use brewโ  to install hadolint.

brew install hadolint

On Windows you can use scoopโ  to install hadolint.

scoop install hadolint

As shown before, hadolint is available as a Docker container:

docker pull hadolint/hadolint

If you need a Docker container with shell access, use the Debian or Alpine variants of the Docker image:

docker pull hadolint/hadolint:latest-debian
docker pull hadolint/hadolint:latest-alpine

You can also build hadolint locally. You need Haskellโ  and the stackโ  build tool to build the binary.

git clone https://github.com/hadolint/hadolint
cd hadolint
stack install

โ Configure

hadolint supports specifying the ignored rules using a configuration file. The configuration file should be in yaml format. This is one valid configuration file as an example:

ignored:
  - DL3000
  - SC1010

Additionally, hadolint can warn you when images from untrusted repositories are being used in Dockerfiles, you can append the trustedRegistries keys to the configuration file as shown below:

ignored:
  - DL3000
  - SC1010

trustedRegistries:
  - docker.io
  - my-company.com:5000

Configuration files can be used globally or per project. By default, hadolint will look for a configuration file in the current directory with the name .hadolint.yaml

The global configuration file should be placed in the folder specified by XDG_CONFIG_HOME, with the name hadolint.yaml. In summary, the following locations are valid for the configuration file, in order or preference:

  • $PWD/.hadolint.yaml
  • $XDG_CONFIG_HOME/hadolint.yaml
  • ~/.config/hadolint.yaml

In windows, the %LOCALAPPDATA% environment variable is used instead of XDG_CONFIG_HOME

Additionally, you can pass a custom configuration file in the command line with the --config option

hadolint --config /path/to/config.yaml Dockerfile

To pass a custom configuration file (using relative or absolute path) to a container, use the following command:

docker run --rm -i -v ./your/path/to/hadolint.yaml:/root/.config/hadolint.yaml hadolint/hadolint < Dockerfile

โ Inline ignores

It is also possible to ignore rules by using a special comment directly above the Dockerfile instruction you want to make an exception for. Ignore rule comments look like # hadolint ignore=DL3001,SC1081. For example:

# hadolint ignore=DL3006
FROM ubuntu

# hadolint ignore=DL3003,SC1035
RUN cd /tmp && echo "hello!"

Inline ignores will only work if place directly above the instruction.

โ Integrations

To get most of hadolint it is useful to integrate it as a check to your CI or to your editor to lint your Dockerfile as you write it. See our [Integration][] docs.

  • [Code Review Platform Integrations][]
  • [Continuous Integrations][]
  • [Editor Integrations][]

โ Rules

An incomplete list of implemented rules. Click on the error code to get more detailed information.

  • Rules with the prefix DL originate from hadolint. Take a look at Rules.hs to find the implementation of the rules.

  • Rules with the SC prefix originate from ShellCheck (Only the most common rules are listed, there are dozens more)

Please [create an issue][] if you have an idea for a good rule.

RuleDescription
DL3000โ Use absolute WORKDIR.
DL3001โ For some bash commands it makes no sense running them in a Docker container like ssh, vim, shutdown, service, ps, free, top, kill, mount, ifconfig.
DL3002โ Last user should not be root.
DL3003โ Use WORKDIR to switch to a directory.
DL3004โ Do not use sudo as it leads to unpredictable behavior. Use a tool like gosu to enforce root.
DL3005โ Do not use apt-get upgrade or dist-upgrade.
DL3006โ Always tag the version of an image explicitly.
DL3007โ Using latest is prone to errors if the image will ever update. Pin the version explicitly to a release tag.
DL3008โ Pin versions in apt-get install.
DL3009โ Delete the apt-get lists after installing something.
DL3010โ Use ADD for extracting archives into an image.
DL3011โ Valid UNIX ports range from 0 to 65535.
DL3012โ Provide an email address or URL as maintainer.
DL3013โ Pin versions in pip.
DL3014โ Use the -y switch.
DL3015โ Avoid additional packages by specifying --no-install-recommends.
DL3016โ Pin versions in npm.
DL3017โ Do not use apk upgrade.
DL3018โ Pin versions in apk add. Instead of apk add <package> use apk add <package>=<version>.
DL3019โ Use the --no-cache switch to avoid the need to use --update and remove /var/cache/apk/* when done installing packages.
DL3020โ Use COPY instead of ADD for files and folders.
DL3021โ COPY with more than 2 arguments requires the last argument to end with /
DL3022โ COPY --from should reference a previously defined FROM alias
DL3023โ COPY --from cannot reference its own FROM alias
DL3024โ FROM aliases (stage names) must be unique
DL3025โ Use arguments JSON notation for CMD and ENTRYPOINT arguments
DL3026โ Use only an allowed registry in the FROM image
DL3027โ Do not use apt as it is meant to be a end-user tool, use apt-get or apt-cache instead
DL3028โ Pin versions in gem install. Instead of gem install <gem> use gem install <gem>:<version>
DL3029โ Do not use --platform flag with FROM.
DL3030โ Use the -y switch to avoid manual input yum install -y <package>
DL3031โ Do not use yum update
DL3032โ yum clean all missing after yum command.
DL3033โ Specify version with yum install -y <package>-<version>
DL3034โ Non-interactive switch missing from zypper command: zypper install -y
DL3035โ Do not use zypper update.
DL3036โ zypper clean missing after zypper use.
DL3037โ Specify version with zypper install -y <package>[=]<version>.
DL3038โ Use the -y switch to avoid manual input dnf install -y <package>
DL3039โ Do not use dnf update
DL3040โ dnf clean all missing after dnf command.
DL3041โ Specify version with dnf install -y <package>-<version>
DL3042โ Avoid cache directory with pip install --no-cache-dir <package>.
DL4000โ MAINTAINER is deprecated.
DL4001โ Either use Wget or Curl but not both.
DL4003โ Multiple CMD instructions found.
DL4004โ Multiple ENTRYPOINT instructions found.
DL4005โ Use SHELL to change the default shell.
DL4006โ Set the SHELL option -o pipefail before RUN with a pipe in it
SC1000โ $ is not used specially and should therefore be escaped.
SC1001โ This \c will be a regular 'c' in this context.
SC1007โ Remove space after = if trying to assign a value (or for empty string, use var='' ...).
SC1010โ Use semicolon or linefeed before done (or quote to make it literal).
SC1018โ This is a unicode non-breaking space. Delete it and retype as space.
SC1035โ You need a space here
SC1045โ It's not foo &; bar, just foo & bar.
SC1065โ Trying to declare parameters? Don't. Use () and refer to params as $1, $2 etc.
SC1066โ Don't use $ on the left side of assignments.
SC1068โ Don't put spaces around the = in assignments.
SC1077โ For command expansion, the tick should slant left (` vs ยด).
SC1078โ Did you forget to close this double-quoted string?
SC1079โ This is actually an end quote, but due to next char, it looks suspect.
SC1081โ Scripts are case sensitive. Use if, not If.
SC1083โ This {/} is literal. Check expression (missing ;/\n?) or quote it.
SC1086โ Don't use $ on the iterator name in for loops.
SC1087โ Braces are required when expanding arrays, as in ${array[idx]}.
SC1095โ You need a space or linefeed between the function name and body.
SC1097โ Unexpected ==. For assignment, use =. For comparison, use [ .. ] or [[ .. ]].
SC1098โ Quote/escape special characters when using eval, e.g. eval "a=(b)".
SC1099โ You need a space before the #.
SC2002โ Useless cat. Consider cmd < file | .. or cmd file | .. instead.
SC2015โ Note that A && B || C is not if-then-else. C may run when A is true.
SC2026โ This word is outside of quotes. Did you intend to 'nest '"'single quotes'"' instead'?
SC2028โ echo won't expand escape sequences. Consider printf.
SC2035โ Use ./*glob* or -- *glob* so names with dashes won't become options.
SC2039โ In POSIX sh, something is undefined.
SC2046โ Quote this to prevent word splitting
SC2086โ Double quote to prevent globbing and word splitting.
SC2140โ Word is in the form "A"B"C" (B indicated). Did you mean "ABC" or "A\"B\"C"?
SC2154โ var is referenced but not assigned.
SC2155โ Declare and assign separately to avoid masking return values.
SC2164โ Use cd ... || exit in case cd fails.

โ Develop

If you are an experienced Haskeller we would be really thankful if you would tear our code apart in a review.

โ Setup
  1. Clone repository

    git clone --recursive [email protected]:hadolint/hadolint.git
    
  2. Install the dependencies

    stack install
    
โ REPL

The easiest way to try out the parser is using the REPL.

# start the repl
stack repl
# overload strings to be able to use Text
:set -XOverloadedStrings
# import parser library
import Language.Docker
# parse instruction and look at AST representation
parseText "FROM debian:jessie"
โ Tests

Run unit tests.

stack test

Run integration tests.

./integration_test.sh
โ AST

Dockerfile syntax is fully described in the [Dockerfile reference][]. Just take a look at [Syntax.hs][] in the language-docker project to see the AST definition.

โ Alternatives

Tag summary

Content type

Image

Digest

Size

5.8 MB

Last updated

almost 5 years ago

docker pull datoma/hadolint