Best Practices
Docker best practices and optimization rules
Run All Best Practices Rules
pathfinder scan --ruleset cpf/docker/best-practicesRules
apk add Without --no-cache
lowDetects RUN instructions using Alpine Linux's apk add command without the --no-cache flag, which unnecessarily increases Docker image size by 2-5 MB.
Deprecated MAINTAINER Instruction
infoDetects usage of the deprecated MAINTAINER instruction, which has been deprecated since Docker 1.13 in favor of LABEL instructions with standardized OCI metadata keys.
Missing HEALTHCHECK Instruction
lowDetects Dockerfiles without a HEALTHCHECK instruction, which prevents orchestrators from monitoring container health and automatically restarting failing containers.
pip install Without --no-cache-dir
lowDetects pip install without --no-cache-dir flag, which leaves pip cache in the image adding 50-200 MB depending on dependencies.
Base Image Uses :latest Tag
mediumDetects FROM instructions using :latest tag or no tag, which makes builds non-reproducible and can introduce unexpected breaking changes or security vulnerabilities.
Avoid apk upgrade
mediumDetects use of apk upgrade in Dockerfiles, which creates unpredictable, non-reproducible builds.
Avoid apt-get upgrade
mediumDetects use of apt-get upgrade or dist-upgrade in Dockerfiles, which creates unpredictable, non-reproducible images.
Avoid dnf update
mediumDetects use of dnf update in Dockerfiles, which creates unpredictable, non-reproducible builds.
Avoid --platform Flag with FROM
lowDetects FROM instructions with --platform flag, which reduces portability by hardcoding platform architecture.
Avoid yum update
mediumDetects use of yum update in Dockerfiles, which creates unpredictable, non-reproducible builds.
Avoid zypper update
mediumDetects use of zypper update in Dockerfiles, which creates unpredictable, non-reproducible builds.
Missing -y flag for apt-get
lowDetects apt-get install without -y flag, which can cause builds to hang waiting for user input.
Missing -y flag for dnf
lowDetects dnf install without -y flag for non-interactive builds.
Missing dnf clean all
lowDetects dnf install commands without subsequent dnf clean all, which unnecessarily increases image size.
Missing Image Version
highDetects FROM instructions using latest tag or no tag, creating non-reproducible builds.
Missing -y flag for yum
lowDetects yum install without -y flag for non-interactive builds.
Missing yum clean all
lowDetects yum install without yum clean all, leaving package cache and increasing image size.
Missing zypper clean
lowDetects zypper install without zypper clean, which increases image size.
Nonsensical Command
lowDetects RUN commands using cd which doesn't persist between instructions. Use WORKDIR instead.
Prefer apt-get over apt
lowDetects use of apt instead of apt-get, which provides better script stability in Dockerfiles.
Prefer COPY Over ADD
lowDetects use of ADD instruction when COPY would suffice. ADD has implicit behavior that can be surprising and create security risks.
Prefer JSON Notation for CMD/ENTRYPOINT
lowDetects shell form for CMD/ENTRYPOINT which doesn't handle signals correctly. Use exec form (JSON) for proper signal handling.
Remove apt Package Lists
lowDetects apt-get install without removing /var/lib/apt/lists/*, which wastes image space.
Missing pipefail in Shell Commands
mediumDetects RUN instructions using pipes without set -o pipefail, which masks failures in piped commands.
Use Absolute Path in WORKDIR
lowDetects WORKDIR with relative paths, which can be confusing and error-prone.
Install Only One of wget or curl
lowDetects installation of both wget and curl, which wastes space. Choose one tool for downloads.
Use WORKDIR Instead of cd
lowDetects use of cd in RUN commands without WORKDIR. WORKDIR is more explicit and persistent.
apt-get Without --no-install-recommends
lowDetects apt-get install without --no-install-recommends flag, which installs unnecessary packages increasing image size and attack surface.