What Is Container Security? Set Baselines from Image to Runtime
Container security is not just checking an image for vulnerable components. It is a set of connected controls for building trusted images, running them with limited privileges, restricting network and data access, and noticing unexpected behavior. A container does not have a separate operating-system kernel like a virtual machine; Linux containers rely on the host kernel. Image scanning or one isolation control therefore cannot remove every risk. This guide focuses on practical baselines from build to operations for Linux containers and Kubernetes. Windows containers, specialized runtimes, and managed control planes can differ in configuration and support.
Why container security has several layers
A container makes an application and its dependencies easier to package and ship, but the running process still uses a kernel, network, storage, and credentials. An old library in an image can expose a known vulnerability. Excessive Linux privileges or a host filesystem mount can give a compromised process a path to affect its node. Even a clean image can be put at risk by an overprivileged deployment identity or unrestricted outbound traffic.
Review these questions together: What did we build? Where did it come from? What privileges does it run with? What can it contact and read? How will we notice unexpected behavior? Each layer should help limit failures in another. This is defense in depth: it reduces risk but does not guarantee that compromise is impossible.
1. Build: control image contents and provenance
Build from a minimal base image where practical, copy only the files you need, and manage build-tool and dependency versions. Leaving shells, compilers, package caches, and test material out of the final image can reduce its attack surface and the number of components to review. Never put tokens or private keys in build logs or image layers. Deleting a file in a later layer may leave its contents in an earlier layer. Use a build system's dedicated secret-mount feature or inject secrets at runtime; revoke and replace any value that was exposed.
Vulnerability scanning can find packages with known issues, but a scan is not a safety verdict. Consider when the scanner's database was updated, how accurately it identifies packages, whether vulnerable code is reachable, available mitigations, and whether a fix exists. Prioritize findings by exposure, privileges, affected code paths, and remediation options rather than treating every result as equally urgent.
Image tags are convenient names, but a registry can move a tag to different content. Kubernetes documents a digest as a hash identifier for specific image content. Pinning a deployment to registry.example/app@sha256:… makes it easier to deploy the same content again. Pinning a digest helps reproducibility and change tracking; it does not prove who made the image or that it is safe. Verify signatures or provenance from a trusted builder separately, and configure a deployment policy to enforce that verification. See the container image signing guide for that topic.
2. Before deployment: keep development exceptions out of production
Privileged mode, host networking, shared host PID namespaces, and broad host-path mounts allowed for development can accidentally remain in production manifests. These options can weaken the container boundary or expose node resources. Before deployment, ask why each is needed, where it is needed, for how long, and whether a safer alternative exists. Start with privileged disabled and avoid hostNetwork, hostPID, hostIPC, and hostPath for ordinary application Pods.
Kubernetes Pod Security Standards provide three policy levels for namespaces. Privileged has few restrictions. Baseline limits known privilege-escalation paths and is a practical starting point. Restricted requires stronger settings, such as non-root execution, but can be less compatible. A common rollout is to enforce Baseline first, test applications, then trial Restricted in namespaces where workloads are ready. If admission rejects a Pod, investigate the required privilege or volume instead of adding a bypass label. Fix the application or document a narrowly scoped exception. Workloads that genuinely need elevated privileges, such as some node agents, need a separate trust boundary, constrained placement, and limited RBAC.
3. At runtime: express least privilege in configuration
The Kubernetes application security checklist recommends non-root execution, disabling privilege escalation, a read-only root filesystem, and removing unnecessary capabilities. The following is a starting point for one Linux container. If the application needs temporary files, mount only the required writable path separately, for example with emptyDir, and align file permissions with the image and volume configuration.
spec:
securityContext:
runAsNonRoot: true
seccompProfile:
type: RuntimeDefault
containers:
- name: app
image: registry.example/app@sha256:<reviewed-digest>
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]This is not a complete deployment manifest: it has no real image digest, UID, volume, ports, or probes, and cannot be deployed as written. Before applying these settings, check that the image starts as a non-root user, identify its writable paths, and test the application with the runtime's default seccomp profile and reduced capabilities. If a permission is genuinely needed, first see whether application behavior or file ownership can be fixed. Add only the specific required capability; do not use privileged: true to bypass all restrictions. Some security-context fields have different behavior or no effect depending on the runtime and operating system.
Use a service account and RBAC permissions scoped to each Pod's work. Consider setting automountServiceAccountToken: false when a workload does not call the Kubernetes API. Keep secrets out of ConfigMaps and images; use a Secret or an approved external secret store. Kubernetes Secrets do not necessarily have encryption at rest by default, so plan etcd encryption, minimal read access, access auditing, and a rotation process. See the Kubernetes Secret guide for storage and delivery details.
4. Network and cluster permissions: limit movement after compromise
If the default network behavior allows every Pod to communicate with every other Pod, a compromised service may be able to explore its neighbors. Use Kubernetes NetworkPolicy to select Pods, begin by denying ingress and egress where appropriate, and then allow only required traffic such as DNS, a database, or a specific API. Do not assume that creating a NetworkPolicy makes it effective: the cluster's CNI must support and enforce NetworkPolicy. After changing a policy, check both expected connections and paths that should be blocked.
Do not expose the Kubernetes API, kubelet API, or etcd directly to the public Internet. In cloud environments, restrict workload access to instance metadata services when it is unnecessary. Minimize API permissions, especially permissions to create or modify Pods and Deployments; those permissions can indirectly expand access by creating workloads with different credentials. Treat access to nodes and the control plane as a separate administrative boundary from application-container settings.
5. Operations: verify controls and investigate anomalies
At release time, record the image digest and the results of scanning and signature verification. Inspect the actual Pod template for privileged settings, host sharing, and unneeded tokens. During operations, collect appropriate Kubernetes audit and runtime logs. Define who investigates unexpected processes, network connections, denied actions, or image changes, and how long the evidence is retained. Prevent logs from collecting tokens, request bodies, or personal data, and control access to the logs themselves.
A baseline is not a one-time document. New vulnerabilities can affect images and dependencies; cluster versions, CNI plugins, and runtime settings change; and application permissions evolve. Revisit exceptions, practice rebuilding, patching, and rollback, and test safe recovery when a policy blocks a deployment. A clean scanner report or an admitted Pod is not proof that the entire system has no vulnerabilities.
Example rollout for a small service
Assume a web API runs in Kubernetes, accepts external requests, connects only to PostgreSQL, and does not need to call the Kubernetes API.
Build with pinned base-image and dependency versions, and keep secrets out of the image. Record scan and signature-verification results; deploy the reviewed image digest.
Test the application as non-root with a read-only root filesystem, the default seccomp profile, and no unnecessary capabilities. Provide a separate volume only for temporary paths that must be writable.
Disable automatic service-account-token mounting and test the namespace's Pod security level. Enforce the policy after confirming application compatibility.
Use NetworkPolicy to permit ingress only from the ingress controller and egress only to DNS and the required PostgreSQL port. Verify that the CNI enforces the policy.
After deployment, verify the actual digest, Pod settings, expected allowed and blocked connections, and log collection. If a policy prevents startup, use logs and denial events to find the cause and make a narrow fix instead of disabling protections broadly.
This scenario is illustrative. Data sensitivity and threat models may call for additional controls such as stronger authentication, encryption, sandboxed runtimes, backups, and incident response. Kubernetes' own security checklist is not a universal recipe; adapt it to the risks and implementation of your cluster.
Sources
[Kubernetes Security Checklist](https://kubernetes.io/docs/concepts/security/security-checklist/)
[Kubernetes Application Security Checklist](https://kubernetes.io/docs/concepts/security/application-security-checklist/)
[Pod Security Standards](https://kubernetes.io/docs/concepts/security/pod-security-standards/)
[Pod Security Admission](https://kubernetes.io/docs/concepts/security/pod-security-admission/)
[Linux kernel security constraints for Pods and containers](https://kubernetes.io/docs/concepts/security/linux-kernel-security-constraints/)
[Kubernetes container images: digests and tags](https://kubernetes.io/docs/concepts/containers/images/)
[NIST SP 800-190, Application Container Security Guide (2017)](https://csrc.nist.gov/pubs/sp/800/190/final) — foundational container-risk guidance; consider its publication date alongside current platform documentation.