Course contentsShow
← Cloud and Infrastructure
Lesson 2269 of 2,98948. Secrets ManagementFree lesson

Container Image Secrets Anti-Patterns

Learn common mistakes like hardcoding secrets in Dockerfiles and why these practices create security vulnerabilities.

Container Image Secrets Anti-Patterns

What you'll learn: How hardcoding secrets in container images creates serious security vulnerabilities and what mistakes to avoid.

The Problem with Baked-In Secrets

When building Docker images, it's tempting to simply copy configuration files or embed credentials directly into your Dockerfile. This is dangerous because container images are immutable snapshots — once a secret is in there, it's permanently part of that image's history, even if you delete the file in a later layer.

Think of it like writing your password in permanent marker on a photocopied document. Even if you scribble it out later, the original photocopy still exists with the password visible.

Common Anti-Patterns

Hardcoded Secrets in Dockerfiles

# ❌ WRONG - Secret is now in the image
ENV DATABASE_PASSWORD="MyS3cr3tP@ssw0rd"

Any user with access to the image can extract this using docker inspect or by examining the image layers.

Copying Secret Files During Build

# ❌ WRONG - Secret becomes part of the image
COPY ./secrets/api-key.txt /app/config/
RUN configure-app

Even if you RUN rm /app/config/api-key.txt afterward, the file exists in an earlier layer and can be recovered.

Embedding Secrets in ARG Values

# ❌ WRONG - Build args are visible in image metadata
ARG API_TOKEN=abc123xyz

Build arguments appear in docker history output and are discoverable.

Why This Matters

Container images often get pushed to registries, shared across teams, or stored in repositories where multiple people (or automated systems) have read access. A compromised image means compromised credentials, leading to unauthorized access, data breaches, and lateral movement in your infrastructure.

Key Takeaway: Never embed secrets directly in container images — they persist in image layers and metadata, creating long-lived security vulnerabilities that spread wherever the image is distributed.