Container Security: From Docker Images to Kubernetes Runtime
A layered look at container images and runtime hardening, followed by the separate identity and policy controls Kubernetes adds.
Containers package an application and its dependencies, but they do not create a security boundary equivalent to a separate virtual machine. Containers typically share the host kernel and rely on namespaces, cgroups, capabilities, seccomp, and runtime configuration for isolation. A vulnerable image can expose a workload; a privileged runtime configuration can turn a container compromise into a host or cluster incident.
Security has at least two layers: building and running a container image, and operating workloads through an orchestrator. Docker focuses on image building and container runtime behavior. Kubernetes schedules and manages containers and adds its own API, identity, admission, and network controls. One layer does not replace the other.
Image construction and dependency risk
Use a minimal, trusted base image and install only required packages. Smaller images reduce included components but do not guarantee safety; maintainers, provenance, and timely updates matter. Pinning by digest makes a build reproducible, though updates then require a deliberate digest refresh. Generate and review an inventory such as an SBOM, scan operating system and language dependencies, and set an update process for newly disclosed vulnerabilities. A scan is a point-in-time signal, not proof that an image is free of vulnerabilities.
Use multi-stage builds so compilers and test tools do not ship in the runtime image. Exclude secrets and irrelevant files from the build context with .dockerignore. Avoid copying credentials into layers: deleting a file in a later layer does not necessarily remove it from earlier image history. Use build secret mechanisms for build-time access and rotate any credentials exposed in an image. Docker's build guidance recommends trusted minimal bases, fewer unnecessary packages, and non-root runtime users.
Restricting container privileges
Run applications as a non-root user when possible. Drop Linux capabilities the process does not need, avoid --privileged, avoid host PID or network namespaces, and use read-only filesystems where the application supports them. Apply seccomp and AppArmor or SELinux profiles where available. Mount only necessary paths and avoid writable host mounts. These controls reduce impact but do not eliminate kernel vulnerabilities or guarantee isolation from a compromised host.
Secrets should be provided at runtime from an appropriate secret manager or orchestrator integration, with short-lived access and narrow scope. Environment variables can appear in diagnostics or process metadata, so use them with an understanding of the platform's exposure model. Do not bake credentials into images or source repositories.
Kubernetes adds orchestration security
Kubernetes RBAC controls access to the cluster API. Create namespace-scoped Roles and RoleBindings for routine workloads and operators, and avoid broad cluster-admin grants. Permission to create or modify Pods can indirectly grant access to service accounts, mounted secrets, or node resources, so review workload creation rights as powerful permissions. Kubernetes RBAC guidance calls out these escalation paths.
Pod Security Standards and admission enforcement constrain workload settings such as privilege escalation, host namespaces, and capabilities. Start with a suitable baseline and enforce the restricted profile where workloads can support it. Define runAsNonRoot, a read-only root filesystem if feasible, dropped capabilities, seccomp, and resource limits in the Pod security context. Exceptions for system agents should be isolated and documented.
Kubernetes NetworkPolicies express allowed L3/L4 communication between Pods and external entities. They only take effect when the cluster network plugin implements policy enforcement. Policies are additive, and ingress and egress isolation are independent; test them with the actual CNI and DNS requirements. Kubernetes explains these semantics and prerequisites.
Use workload identity rather than embedding cloud credentials in Pod manifests. Restrict service account token mounting when a workload does not need Kubernetes API access. Protect the API server, audit sensitive operations, and monitor runtime events such as unexpected shells, writes to sensitive paths, privilege changes, or outbound connections that do not match workload behavior.
Practical example and common errors
A hardened application image might use a pinned runtime base, copy only built artifacts, set an unprivileged UID, and expose only the intended process. At deployment, the Pod would run with a restricted security context, a service account with only required access, and a NetworkPolicy that permits ingress from the expected frontend and egress to the required data service. This is a conceptual example; actual syntax and compatible settings vary by application, Kubernetes version, and cluster plugin.
Common mistakes include trusting a latest tag, assuming a vulnerability scan fixes vulnerable dependencies, running as root because it is convenient, mounting the Docker socket, granting privileged mode, using wildcard RBAC, and writing a NetworkPolicy without a supporting CNI. Another failure is introducing default-deny egress without allowing DNS, telemetry, or required service endpoints, which can silently break the application.
Recommendations and limits
Build from reviewed sources, scan and update continuously, sign or otherwise verify artifacts, and promote the same immutable artifact between environments. Enforce non-root and pod security requirements at admission where feasible. Use least privilege for both Kubernetes API identities and cloud workload identities. Apply network controls and collect audit and runtime signals. Prioritize remediation by exploitability, exposure, and workload role, not scanner severity alone.
A lab could build a small API image, compare a default-root container with a non-root version, then deploy it into a disposable cluster with namespace RBAC and a NetworkPolicy. This describes a learning exercise, not a completed deployment. Validate behavior before enforcing policy in a production namespace.
Conclusion
Container security starts with the image and continues through runtime configuration, cluster identity, admission controls, and network policy. Docker hardening limits what a container can do; Kubernetes hardening governs how workloads are authorized, placed, and connected. Both layers need to be represented in the software delivery process that creates and deploys the artifact.