Docs/Rules/Dockerfile (D01-D40)

Dockerfile Rules (D01-D40)

40 rules for Dockerfile security and best practices. Deducts from the Security Score.

Base Image Security (D01-D05)

IDSevPtsDescriptionFix
D01CRIT3Using FROM with :latest tagPin to specific version: FROM ubuntu:22.04
D02HIGH3FROM without any tag or digestAdd explicit version tag: FROM node:20-alpine
D03HIGH3No non-root USER instructionAdd USER 1001 before CMD instruction
D04MED2Using ADD instead of COPYReplace ADD with COPY unless archive extraction needed
D05HIGH3Using large base image instead of slim/alpineUse alpine, slim, or distroless variant

Secrets & Sensitive Data (D06-D10)

IDSevPtsDescriptionFix
D06CRIT5Secret in ENV or ARG instructionUse --secret mount or runtime env vars instead
D07HIGH3COPY of sensitive files into imageUse Docker secrets or mount at runtime
D08MED2Git clone with embedded credentialsUse SSH agent forwarding or build-time --secret mount
D09MED2ssh-keygen or SSH key generation in DockerfileGenerate SSH keys at runtime or mount from secret manager
D10HIGH3Curl piped to shell (pipe install pattern)Download, verify checksum, then execute separately

Build Instructions (D11-D15)

IDSevPtsDescriptionFix
D11MED2CMD in shell form instead of exec formUse exec form: CMD ["npm", "start"]
D12MED2ENTRYPOINT in shell formUse exec form: ENTRYPOINT ["./app"]
D13MED2No HEALTHCHECK instructionAdd HEALTHCHECK CMD curl -f http://localhost/ || exit 1
D14LOW1No WORKDIR instructionAdd WORKDIR /app before COPY and RUN
D15LOW1No EXPOSE instructionAdd EXPOSE 8080 to document container port

Package Management (D16-D20)

IDSevPtsDescriptionFix
D16HIGH3apt-get install without --no-install-recommendsAdd --no-install-recommends to apt-get install
D17MED2apt-get upgrade or dist-upgrade in DockerfileRemove apt-get upgrade - pin specific versions
D18MED2apk add without --no-cacheAdd --no-cache: RUN apk add --no-cache curl
D19MED2pip install without --no-cache-dirAdd --no-cache-dir to pip install
D20MED2pip install without version pinningPin versions or use requirements.txt

Multi-Stage & Layer Optimization (D21-D25)

IDSevPtsDescriptionFix
D21HIGH3No multi-stage buildUse multi-stage: FROM node AS build ... FROM alpine
D22MED2COPY . . without .dockerignoreCreate .dockerignore with .git, node_modules, .env
D23MED2Multiple consecutive RUN instructionsCombine RUN commands with && on single layer
D24MED2Package install without cleanup in same layerAdd && rm -rf /var/lib/apt/lists/* in same RUN
D25MED2COPY before package.json (bad cache order)COPY package*.json first, then COPY . .

Security Hardening (D26-D30)

IDSevPtsDescriptionFix
D26HIGH3SETUID/SETGID binaries not removedRUN find / -perm /6000 -exec chmod a-s {} +
D27CRIT3chmod 777 - world-writable permissionsUse chmod 755 for dirs, chmod 644 for files
D28HIGH3Using sudo in DockerfileRemove sudo - run as root before USER, then switch
D29MED2No checksum verification for downloaded filesVerify: curl -o file URL && sha256sum -c checksum.txt
D30MED2No init process (PID 1 signal handling)Install tini: ENTRYPOINT ["tini", "--"]

Best Practices (D31-D35)

IDSevPtsDescriptionFix
D31LOW1No LABEL instruction for metadataAdd LABEL maintainer="you" version="1.0"
D32MED2Multiple CMD instructionsKeep only one CMD at the end of Dockerfile
D33MED2Multiple ENTRYPOINT instructionsKeep only one ENTRYPOINT instruction
D34LOW1No .dockerignore file referencedCreate .dockerignore with .git, node_modules, .env
D35LOW1No Hadolint or Dockerfile linting configuredAdd .hadolint.yaml and run hadolint in CI

Advanced (D36-D40)

IDSevPtsDescriptionFix
D36MED2Using VOLUME for application dataDefine volumes in docker-compose or K8s, not Dockerfile
D37HIGH3Running as root at end of DockerfileAdd USER 1001 after root operations, before CMD
D38MED2SHELL override without justificationOnly override SHELL for PowerShell or specific features
D39MED2ADD with URL sourceReplace ADD URL with RUN curl + sha256sum verification
D40HIGH3Using --privileged or dangerous capabilitiesDrop all capabilities and add only what is needed
Total: 40 rules across 8 sections. All deduct from the Security Score.