Article · Kubernetes & Containers
Building Better Container Images: A Practical Guide
Build and test a non-root container image. Pin image content, keep build secrets out of layers and validate runtime permissions before deployment.
In this article
Build container images from an appropriate maintained base, pin the content used for a release, keep secrets outside image layers, and run the application with minimal permissions. Test the resulting artifact as the same user and architecture you intend to deploy.
Build a small, complete example
This lab requires Docker with BuildKit. Use an empty directory. The BusyBox image below is an illustrative shell application, not a Python base image or a recommendation for every workload.
cat > Dockerfile <<'EOF'
FROM docker.io/library/busybox@sha256:a7dbc319b1438115a207e11930dc4bb3e643dcb3ec0bdd694bb36cd8fad289ec
WORKDIR /app
USER 10001:10001
CMD ["sh", "-c", "id -u; echo container-ready"]
EOF
docker build --pull -t cloudhero-image-lab:local .
docker run --rm --read-only --cap-drop=ALL --security-opt=no-new-privileges cloudhero-image-lab:localExpect UID 10001 and container-ready. An image digest identifies registry content; an image ID is not interchangeable with a multi-platform manifest digest. Check the platforms published by your base image. Pinning also requires a deliberate rebuild and update process.
Pass build secrets through secret mounts
Do not put credentials in ARG, ENV or a copied file that is later deleted: layers and build metadata can preserve them. BuildKit secret mounts expose a secret only to the instruction that needs it. Never print or copy it into build output.
# Illustrative Dockerfile instruction; adapt to your artifact download.
RUN --mount=type=secret,id=artifact_token \
test -s /run/secrets/artifact_token
# Supply a local secret file when building:
# docker build --secret id=artifact_token,src=./artifact-token .Validate more than image size
Scan the final image and its dependencies, inspect the configured user and entrypoint, and test startup and shutdown. A trusted registry and a small image are useful inputs; neither establishes that an image is vulnerability-free. Runtime secrets can be mounted from a secret manager. Anyone permitted to inspect the workload may still obtain its configuration.
Clean up the lab
docker image rm cloudhero-image-lab:localFor Kubernetes runtime secrets, see our tested External Secrets guide.
Sources and further reading
Keep exploring.
All resources →Article · Kubernetes & Containers
What is Kubernetes, and When Should You Use It?
Kubernetes is an open-source system for deploying and managing containerized applications. It schedules workloads across nodes and provides APIs for releases, networking and scaling. Use it…
Article · Kubernetes & Containers
Choosing Between Amazon EKS and Amazon ECS for Your Startup: A Comprehensive Guide
Choose Amazon ECS when your team wants an AWS container platform with fewer Kubernetes-specific decisions. Choose Amazon EKS when Kubernetes APIs, operators or existing platform tooling…
Article · Kubernetes & Containers
Understanding Karpenter: Kubernetes Node Management Solution
Karpenter provisions Kubernetes nodes around the requirements of pending Pods and manages their lifecycle. On Amazon EKS, it can reduce manual capacity planning and help consolidate…
Work with CloudHero
Put the ideas to work.
Discuss the engineering work your team needs.
CloudHero Weekly
Keep up with what changes in production.
Cloud, AI and DevOps developments, with original sources and practical engineering judgment.


