Best Practice
Best practices and optimization rules
Run All Best Practice Rules
pathfinder scan --ruleset cpf/docker/best-practiceRules
apt-get Without --no-install-recommends
LOWapt-get install without --no-install-recommends. This installs unnecessary packages, increasing image size and attack surface.
Avoid apk upgrade
MEDIUMAvoid 'apk upgrade' in Dockerfiles. Use specific base image versions instead for reproducible builds.
Avoid apt-get upgrade
MEDIUMAvoid apt-get upgrade in Dockerfiles. Use specific base image versions instead.
Avoid dnf update
MEDIUMAvoid 'dnf update' in Dockerfiles. Use specific base image versions for reproducible builds.
Avoid --platform Flag with FROM
LOWFROM with --platform flag reduces portability. Let Docker handle platform selection.
Avoid yum update
MEDIUMAvoid 'yum update' in Dockerfiles. Use specific base image versions for reproducible builds.
Avoid zypper update
MEDIUMAvoid 'zypper update' in Dockerfiles. Use specific base image versions for reproducible builds.
Deprecated MAINTAINER Instruction
INFOMAINTAINER instruction is deprecated. Use LABEL org.opencontainers.image.authors instead.
Missing -y flag for apt-get
LOWapt-get install without -y flag. Add -y or --yes for non-interactive builds.
Missing -y flag for dnf
LOWdnf install without -y flag. Add -y for non-interactive builds.
Missing dnf clean all
LOWRUN uses 'dnf install' without 'dnf clean all'. This increases image size.
Missing HEALTHCHECK Instruction
LOWNo HEALTHCHECK instruction. Container health cannot be monitored by orchestrators, reducing reliability and observability.
Missing -y flag for yum
LOWyum install without -y flag. Add -y for non-interactive builds.
Missing yum clean all
LOWRUN instruction uses 'yum install' without 'yum clean all'. This leaves package cache and increases image size.
Missing zypper clean
LOWRUN uses 'zypper install' without 'zypper clean'. This increases image size.
Nonsensical Command
LOWRUN command uses 'cd' which doesn't persist. Use WORKDIR instead.
Prefer apt-get over apt
LOWUse apt-get instead of apt for better script stability in Dockerfiles.
Prefer COPY Over ADD
LOWUse COPY instead of ADD for simple file operations. ADD has implicit behavior that can be surprising.
Prefer JSON Notation for CMD/ENTRYPOINT
LOWUse JSON notation (exec form) for CMD/ENTRYPOINT for proper signal handling.
Remove apt Package Lists
LOWapt-get install without removing /var/lib/apt/lists/*. This wastes image space.
Missing pipefail in Shell Commands
MEDIUMRUN instruction uses pipes without 'set -o pipefail'. This masks failures in piped commands.
Use Absolute Path in WORKDIR
LOWWORKDIR should use absolute paths starting with /.
Install Only One of wget or curl
LOWInstalling both wget and curl wastes space. Choose one tool for downloads.
Use WORKDIR Instead of cd
LOWUse WORKDIR instruction instead of 'cd' in RUN commands.
Base Image Uses :latest Tag
MEDIUMBase image uses ':latest' tag or no tag (defaults to latest). This makes builds non-reproducible.
apk add Without --no-cache
LOWapk add without --no-cache. Package cache remains in image, increasing size by 2-5 MB.
pip install Without --no-cache-dir
LOWpip install without --no-cache-dir. Pip cache remains in image, adding 50-200 MB depending on dependencies.
Missing Image Version
HIGHFROM instruction uses 'latest' tag or no tag. Specify explicit versions for reproducible builds.