For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/iac_security/iac_rules/dockerfile-from-or-maintainer-cannot-be-triggered-within-onbuild.md.
A documentation index is available at /llms.txt.
ONBUILD instructions must not trigger FROM, MAINTAINER, or another ONBUILD, because these directives can change the base image or persisted metadata in downstream builds. This can create recursive or unexpected build behavior that undermines build integrity and supply-chain security.
This rule inspects dockerfile_container resources for Dockerfile ONBUILD instructions and verifies that the triggered subcommand is not FROM, MAINTAINER, or ONBUILD.
Resources where ONBUILD triggers FROM, MAINTAINER, or ONBUILD are flagged and should be refactored to invoke safer build actions (for example, RUN or COPY).
Secure ONBUILD example:
ONBUILD RUN apt-get update && apt-get install -y curl
Compliant Code Examples
FROMmaven:3-jdk-8LABELmaintainer="java-platform@example.com"LABELdescription="Maven base image for Java applications"LABELversion="1.0.0"# Set Maven environment variablesENVMAVEN_CONFIG=/root/.m2 \
MAVEN_OPTS="-XX:+TieredCompilation -XX:TieredStopAtLevel=1"# Install additional build toolsRUN apt-get update && apt-get install -y --no-install-recommends \
git \
openssh-client \
&& rm -rf /var/lib/apt/lists/*# Configure Maven settingsRUN mkdir -p /root/.m2 &&\
echo'<settings><localRepository>/root/.m2/repository</localRepository></settings>' > /root/.m2/settings.xml# Create application directoryRUN mkdir -p /usr/src/appWORKDIR/usr/src/app# Negative case 1: ONBUILD ADD is allowed (correct)ONBUILDADD . /usr/src/app# Copy Maven wrapper if presentONBUILDCOPY mvnw* ./ONBUILDCOPY .mvn .mvn# Download dependencies (will be cached)ONBUILDCOPY pom.xml ./ONBUILDRUN mvn dependency:go-offline# Copy source and buildONBUILDCOPY src ./src# Negative case 2: ONBUILD RUN is allowed (correct)ONBUILDRUN mvn install# Package the applicationONBUILDRUN mvn clean package -DskipTests# Set default commandCMD["mvn","--version"]
Non-Compliant Code Examples
FROMdebian:bullseye-slimLABELmaintainer="base-images@example.com"LABELdescription="Base image with incorrect ONBUILD usage"# Install common dependenciesRUN apt-get update && apt-get install -y --no-install-recommends \
curl \
ca-certificates \
git \
&& rm -rf /var/lib/apt/lists/*WORKDIR/app# Positive case 1: ONBUILD FROM is not allowed (incorrect)ONBUILDFROMdebian# Set up common build patternsONBUILDRUN mkdir -p /app/logs# Positive case 2: ONBUILD MAINTAINER is not allowed (incorrect)ONBUILDMAINTAINERRon Weasley# Configure build-time dependenciesONBUILDRUN apt-get update && apt-get install -y build-essential# Set environment variablesENVAPP_ENV=production \
LOG_LEVEL=info
# Create application userRUN groupadd -r appuser &&\
useradd -r -g appuser appuser# Expose default portEXPOSE8080USERappuserCMD["/bin/bash"]
1
2
rulesets:- Dockerfile # Rules to enforce .
Request a personalized demo
Get Started with Datadog
Ask AI
AI-generated responses may be inaccurate. Verify important info.