Table of Contents
What Containers Are and Why Security Matters
Containers are lightweight, portable units of software that package an application with all its dependencies, libraries, and configuration files. Docker popularized containers by making them easy to create and manage, while Kubernetes emerged as the standard platform for orchestrating containers at scale. Together, they have transformed how software is developed, deployed, and operated.
From a security perspective, containers introduce both benefits and challenges. On the positive side, containers isolate applications from each other and from the host system, limit the attack surface by including only necessary components, and enable consistent deployment configurations. On the challenging side, containers can be misconfigured, built from vulnerable base images, granted excessive privileges, and deployed at a scale that makes manual security review impossible.
Container adoption has surged, with the majority of organizations now running containerized workloads in production. This makes container security a critical skill for anyone involved in modern software development and operations.
Container vs Virtual Machine Isolation
Understanding the isolation model is essential for assessing container security. Virtual machines (VMs) provide strong isolation by running a complete guest operating system on virtualized hardware. Each VM has its own kernel, which means a compromise of one VM does not directly affect others or the host system.
Containers share the host operating system's kernel. They are isolated through Linux kernel features like namespaces (which separate process trees, network stacks, and file systems) and cgroups (which limit resource usage). While this isolation is effective under normal circumstances, it is fundamentally weaker than VM isolation because all containers share the same kernel. A kernel vulnerability can potentially be exploited to escape a container and access the host or other containers.
This does not mean containers are insecure. It means that container security requires additional measures beyond what VMs provide by default. Defense in depth, running containers with minimal privileges, and keeping the host kernel updated are essential practices.
Common Container Vulnerabilities
Vulnerable Base Images
Container images are built in layers, starting from a base image that typically includes a minimal operating system and runtime. If the base image contains known vulnerabilities, every container built from it inherits those vulnerabilities. Public registries like Docker Hub contain millions of images, many of which are outdated, unmaintained, or built with vulnerable components.
Using hash verification to confirm the integrity of container images before deployment helps ensure that images have not been tampered with and match their expected checksums. Always pull images from trusted sources and verify their signatures.
Excessive Container Privileges
Running containers as root is one of the most common and dangerous misconfigurations. If a container runs as root and an attacker exploits a vulnerability within the container, they have root-level access to the container's filesystem and potentially a path to escaping to the host system.
The --privileged flag in Docker gives a container nearly unrestricted access to the host system, including device access and kernel capability manipulation. This flag should almost never be used in production and exists primarily for specific development and testing scenarios.
Exposed Secrets
Applications need credentials to access databases, APIs, and other services. A common mistake is embedding these secrets directly in container images, either in the Dockerfile, environment variables baked into the image, or configuration files. Anyone who gains access to the image, whether through a registry or by pulling it from a compromised host, can extract these secrets.
Unpatched Runtime Vulnerabilities
Docker Engine, containerd, and Kubernetes themselves have had security vulnerabilities. The container runtime is a critical component, and vulnerabilities in it can undermine all container isolation. Keeping the container runtime and orchestration platform updated is as important as patching the host operating system.
Docker Security Best Practices
Use minimal base images. Choose base images like Alpine Linux, distroless images, or scratch images that contain only what your application needs. Fewer components mean fewer potential vulnerabilities and a smaller attack surface.
Never run as root. Add a USER instruction in your Dockerfile to specify a non-root user. If your application requires root during build time for installing packages, switch to a non-root user before the final runtime instruction.
Use multi-stage builds. Multi-stage Dockerfiles allow you to compile code and install build dependencies in one stage, then copy only the final artifacts to a clean runtime image. This prevents build tools, source code, and development dependencies from appearing in the production image.
Manage secrets properly. Use Docker secrets, Kubernetes secrets, or external secret management tools like HashiCorp Vault to inject credentials at runtime rather than embedding them in images. Never include passwords, API keys, or certificates in your Dockerfile or image layers.
Set resource limits. Use cgroups to limit CPU, memory, and storage for each container. This prevents a compromised container from consuming all host resources in a denial-of-service attack and limits the impact of resource-exhaustion vulnerabilities.
Enable read-only filesystems. Run containers with the --read-only flag to prevent the containerized application from writing to the filesystem. If the application needs to write temporary files, mount specific writable volumes only where necessary.
Scanning Container Images
Automated vulnerability scanning should be integrated into your container build pipeline. Scanners analyze image layers and compare installed packages against vulnerability databases.
Trivy is a free, open-source scanner that checks container images for known vulnerabilities in OS packages and application dependencies. It integrates easily into CI/CD pipelines and provides clear, actionable output.
Snyk Container provides vulnerability scanning with remediation advice, suggesting which base image version to upgrade to for fixing specific vulnerabilities.
Docker Scout, built into Docker Desktop, analyzes images and provides security recommendations directly in the Docker development workflow.
Integrate scanning into your CI/CD pipeline so that every image is scanned before it is pushed to a registry. Define policies that block deployment of images with critical or high-severity vulnerabilities. This shifts security left, catching problems during development rather than after deployment.
Kubernetes-Specific Security Considerations
Kubernetes adds orchestration complexity that introduces additional security considerations. Enable Role-Based Access Control (RBAC) to restrict who can deploy, modify, and access resources within the cluster. Use Network Policies to control which pods can communicate with each other, implementing micro-segmentation within the cluster. Enable Pod Security Standards to prevent pods from running with dangerous configurations like privileged mode or host network access.
Kubernetes secrets are base64-encoded by default, not encrypted. Use encryption at rest for the etcd datastore and consider external secret management solutions for sensitive credentials. Regularly audit your cluster configuration against benchmarks like the CIS Kubernetes Benchmark to identify and remediate misconfigurations.
Container security is a continuous practice, not a one-time configuration. As your containerized applications evolve, maintain vigilance over image vulnerabilities, runtime configurations, and access controls to keep your deployments protected.
Share this article

Raimundo Coelho
Cybersecurity specialist and technology professor with over 20 years of experience in IT. Graduated from Universidade Estácio de Sá. Writing practical guides to help you protect your data and stay safe in the digital world.