Safeguard
Developer Tools•

Dockerizing an AI-built prototype: which four lines should you change?

Four Dockerfile changes to make before an AI-built prototype ships: pin the base image, drop root, keep secrets out, and ignore files you should not copy.

Safeguard Security Team
3 min read

Before an AI-built prototype ships in a container, change four things: pin the base image to a specific version or digest, set a non-root user, keep secrets out of the image, and add a .dockerignore so local files are not copied in. Generated Dockerfiles often skip all four.

Why generated Dockerfiles need a pass

An assistant writes a Dockerfile that builds and runs, which is what you asked for. The defaults it uses are the convenient ones: a floating tag, the root user, and a COPY . . that grabs everything in the folder. None break the demo and all matter in production.

Change 1: pin the base image

Docker's documentation notes that image tags are mutable, so a publisher can update a tag to point to a new image. That helps you pick up fixes, but it also means the same Dockerfile can build something different tomorrow. Pin to a specific version, or to a digest for stronger repeatability, and update it deliberately.

Before: a bare tag such as latest. After: a specific version, ideally with its digest.

Change 2: do not run as root

The USER instruction sets the user for the rest of the build stage and for the commands that run at runtime. Create an unprivileged user in the image and switch to it before the final command. If the process is compromised, it then has fewer rights inside the container.

Change 3: keep secrets out of the image

Anything copied or set at build time can end up in an image layer, and images get pushed, cached and shared. Pass secrets at runtime through your platform's secret mechanism rather than writing them into the Dockerfile or the build context. If a secret was baked in, rotate it.

Change 4: add a .dockerignore

A COPY . . instruction includes every file in the build context. A .dockerignore file excludes the ones you do not want, such as local environment files, the git folder, notes and build output. It also makes builds smaller and faster, which Docker's build guidance mentions.

Quick table

ItemTypical generatedBetter
Base imageFloating tagPinned version or digest
UserRootDedicated unprivileged user
SecretsIn build args or copied filesSupplied at runtime
Build contextEverythingFiltered by .dockerignore

Also worth doing

Use a multi-stage build so build tools do not reach the final image, install only what the app needs, and scan the built image before you push it.

Where Safeguard fits

Safeguard offers container image scanning alongside dependency scanning and SBOM generation, so you can check the image you actually built. Try the free scan at gold.safeguard.sh/scan.

Try it yourself

Click through it at your own pace, or open it full screen. Look at which layer each finding comes from.

Quick check

Is pinning by digest overkill for a prototype?

For a throwaway, a specific version is enough. For anything heading to production, a digest gives you the strongest repeatability.

Does a non-root user break things?

Sometimes it exposes file permission issues, which is a good thing to find before launch. Fix the permissions rather than going back to root.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.