Linux & Servers guides, explanations and reader checklists on whilenetworking.com

Securing Docker Containers at the Kernel Level with Seccomp Profiles and AppArmor

Container security is often treated as a layer of configuration atop an application, but true resilience requires hardening the underlying operating system…

Container security is often treated as a layer of configuration atop an application, but true resilience requires hardening the underlying operating system kernel. While user-space tools like seccomp (Secure Computing Mode) and AppArmor (Application Armor) are frequently overlooked in favor of simple network policies or image scanning, they represent the first line of defense against kernel exploits and privilege escalation. When a container is compromised, attackers often seek to leverage host kernel vulnerabilities. By restricting system calls and defining mandatory access control rules early, you significantly reduce the attack surface before any application code executes.

This guide provides a practical approach to implementing these security modules in modern Linux environments, specifically focusing on Debian-based distributions like Ubuntu or Kali Linux, which serve as common bases for many container orchestration platforms. We will explore how to configure these mechanisms without breaking your applications, identify common pitfalls, and provide a real-world example of securing a Python web service.

Understanding the Kernel Security Layers

To secure containers effectively, one must understand the distinct roles of seccomp and AppArmor. These tools operate at different levels of the Linux kernel architecture but work synergistically to create a “zero-trust” environment for each container.

Seccomp acts as a filter between user-space applications and the kernel. It allows administrators to define which system calls (syscalls) a process is permitted to make. By default, containers inherit the host’s syscalls, but this broad access can be dangerous if an application contains a vulnerability that triggers a specific, dangerous syscall (such as ptrace or mount). With seccomp, you can whitelist only the necessary calls for your application to function, blocking everything else by default.

AppArmor, on the other hand, operates in Mandatory Access Control (MAC) mode. Unlike Discretionary Access Control (DAC), which relies on file and folder permissions set by the user, MAC enforces a strict profile that dictates what files, network sockets, and devices a process can access. Even if an attacker bypasses standard file permissions, AppArmor will deny access unless explicitly allowed in the profile. This is particularly useful for preventing unauthorized reads of sensitive host files or writes to critical system directories.

Configuring Seccomp Profiles for Minimal Permissions

Implementing seccomp involves creating a JSON policy that lists allowed syscalls. The goal is to start with a minimal set and add exceptions only when necessary. Most modern containers can run on a restricted profile by default, but legacy applications may require specific adjustments.

You can generate a base profile using the seccomp-generate tool or manually curate one from the Docker documentation. Once you have your JSON file (e.g., minimal.json), you must attach it to your container definition. In Docker Compose or Kubernetes, this is done via the security_opt parameter:

security_opt:
  - seccomp=docker-default # Or a custom profile like /etc/seccomp/custom.json

It is crucial to test these profiles in isolation before deploying to production. A common mistake is assuming that a “default” profile allows all network calls; often, specific socket operations need explicit whitelisting. If your application fails to start due to a blocked syscall, review the logs for EACCES errors related to system calls and adjust the JSON policy accordingly. Always ensure that critical syscalls like socket, connect, read, write, and exit_group are included in any custom profile.

Implementing AppArmor Profiles for Filesystem Isolation

While seccomp restricts what the process can do (via syscalls), AppArmor restricts what the process can touch. To implement AppArmor, you first need to ensure the kernel module is loaded (lsmod | grep apparmor) and that the Docker daemon is configured to use it. This is typically handled by setting apparmor="on" in the Docker daemon configuration file (/etc/docker/daemon.json).

For new applications, generating a profile from scratch is the safest route. You can run your application inside a container with AppArmor profiling enabled but unconfined initially. Once the application runs successfully, inspect the generated log files (usually found in /var/log/kern.log or dmesg) to see which file paths were accessed and denied. Use this information to create a custom profile that strictly limits access to only those required paths.

For existing applications, you can attach an existing profile from the Docker AppArmor profiles repository. However, be aware that these generic profiles may be too restrictive for complex enterprise apps. You will likely need to write a custom profile. A typical pattern involves defining allow rules for the application’s working directory and specific configuration files, while denying all other access:

# Example snippet of an AppArmor profile logic
#include <tunables/global>
profile docker-app-docker-default flags=rx,attach_disconnected {
  # Allow access to required directories
  /app/** rxt,
  /etc/app/config* r,
  # Deny everything else by default
  deny @other network,
}

Real-World Example: Securing a Python Flask Application

Consider a scenario where you are deploying a Python Flask API. By default, this container might have access to the host’s network stack and read any file on the filesystem. Let’s harden it using both mechanisms.

  1. Define Seccomp: Create a seccomp-flask.json that allows standard HTTP server syscalls but blocks ptrace (preventing process debugging/attaching) and dangerous system calls like mount.
  2. Define AppArmor: Generate a profile that restricts the container to /app, /var/log/app, and specific ports, denying access to /etc/shadow or host root directories.

In your Dockerfile or deployment script:

COPY seccomp-flask.json /etc/seccomp/custom.json
# Run with security options
CMD ["python", "app.py"]

Launch the container with these flags:

docker run -d \
  --name flask-api \
  --security-opt seccomp=/etc/seccomp/custom.json \
  --security-opt apparmor=docker-flask-custom \
  my-image:latest

Monitor the application. If it crashes, check if a required library is trying to make a blocked syscall or access a denied file path. Adjust the profiles iteratively until stability is achieved without opening unnecessary security holes.

Common Mistakes and Best Practices

Several pitfalls frequently undermine container security efforts. First, never disable these features entirely just because of a “compatibility issue.” Instead, spend time troubleshooting the specific error logs to find the missing permission. Second, avoid blindly copying generic profiles from the internet; they often contain outdated rules or allow dangerous syscalls by accident. Always audit the JSON or profile text before deployment.

Third, remember that these tools do not replace image scanning or runtime monitoring. They are complementary layers. If an attacker gains root inside a container via a CVE in the application code, seccomp and AppArmor may prevent them from exploiting kernel vulnerabilities to escape to the host, but they cannot stop all attacks. Therefore, keep your base images updated and scan for known CVEs regularly.

Finally, document your profiles. If you create custom seccomp or AppArmor rules, store them in version control. This ensures that when a team member spins up a new environment, the security posture remains consistent across different machines and cloud providers.

Conclusion

Securing Docker containers at the kernel level is not optional for high-stakes environments; it is a fundamental requirement for defense-in-depth strategies. By leveraging seccomp to restrict system calls and AppArmor to enforce strict filesystem boundaries, you create a robust barrier against privilege escalation and lateral movement. While configuration requires careful iteration and testing, the payoff is a significantly hardened infrastructure that limits the damage potential of any single compromised container. Start with minimal profiles, test thoroughly, and maintain detailed documentation to ensure your security posture evolves alongside your applications.