For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/iac_security/iac_rules/dockerfile-should-specify-base-image.md.
A documentation index is available at /llms.txt.
Dockerfiles must include a FROM instruction that specifies a base image to ensure the built image has the intended runtime and dependencies. Without it, the build may unintentionally produce a bare scratch image that lacks essential components.
This rule inspects dockerfile_container resources and checks the command entries for at least one FROM instruction.
Resources that do not contain a FROM instruction are flagged. To remediate, add a top-level FROM <image> line to explicitly declare the base image.
# Negative case: Proper Dockerfile with FROM statementsFROMimageasbaseLABELmaintainer="backend-team@example.com"LABELdescription="Backend API service"# Install build dependenciesRUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
git \
&& rm -rf /var/lib/apt/lists/*WORKDIR/build# Copy source codeCOPY . /build/# Build the applicationRUN make build# Negative case: Multi-stage build with proper second FROMFROMimage2LABELmaintainer="backend-team@example.com"LABELdescription="Production backend API"LABELversion="1.5.0"# Install runtime dependenciesRUN apt-get update && apt-get install -y --no-install-recommends \
ca-certificates \
curl \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*WORKDIR/app# Copy built artifacts from builder stageCOPY --from=base /build/dist /app/# Create application userRUN groupadd -r apiuser &&\
useradd -r -g apiuser -d /app -s /sbin/nologin apiuser &&\
chown -R apiuser:apiuser /app# Set environment variablesENVAPP_ENV=production \
PORT=8080\
LOG_LEVEL=info
# Expose application portEXPOSE8080# Health checkHEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3\
CMD curl -f http://localhost:8080/health ||exit1# Switch to non-root userUSERapiuser# Start the applicationCMD["/app/server"]
Non-Compliant Code Examples
# Positive case: Dockerfile without FROM statement (missing base image)LABELmaintainer="broken-team@example.com"LABELdescription="Invalid file without base image"# Install packages (this won't work without a base image)RUN apt-get update && apt-get install -y \
curl \
wget \
vimWORKDIR/app# Set environment variablesENVAPP_ENV=production \
PORT=8080# Copy application filesCOPY . /app/# Positive case: RUN command without a base image contextRUNecho"hello"# Expose portEXPOSE8080# This Dockerfile is invalid because it doesn't start with FROMCMD["echo","This will never execute"]
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.