Docker Security Fundamentals
Docker Security Fundamentals
Docker makes it easy to package and run applications in containers, but containerization does not automatically make an application secure. Docker security is a combination of container isolation, Linux kernel security, daemon protection, image security, filesystem permissions, capabilities, resource controls, secrets management, and secure operational practices.
1. What Is Docker Security?
Docker security is the practice of protecting:
• Docker hosts
• Docker daemon
• Container processes
• Container images
• Container filesystems
• Networks
• Secrets and credentials
• Docker APIs
• Volumes
• Container runtime configuration
• Supply chain and image dependencies
A useful way to visualize Docker security is as multiple layers: host security, Docker Engine security,
container security, and image/supply-chain security.
2. Docker Is Not a Virtual Machine
One of the most important concepts in Docker security is understanding the difference between
container isolation and virtual-machine isolation.
A container shares the host's Linux kernel. Docker relies heavily on Linux kernel features such as
namespaces and cgroups for isolation and resource control.
This means a container should be treated as an isolated process environment, not as an independent
operating system.
3. Linux Namespaces
Linux namespaces provide an important isolation mechanism for containers. They allow processes
to have different views of system resources.
Important namespaces include PID, network, mount, IPC, UTS, and user namespaces.
Namespaces provide isolation, but they are only one part of Docker's security model. Docker combines
namespaces with capabilities, seccomp, user namespaces, filesystem permissions, and other controls.
4. Linux Capabilities
Linux capabilities divide some traditionally privileged operations into smaller permission units.
Docker uses a restricted set of capabilities for containers by default.
For example:
docker run --cap-drop=ALL nginx
You can selectively add a required capability:
docker run --cap-drop=ALL --cap-add=NET_BIND_SERVICE nginx
The principle is least privilege: give the container only the privileges it actually requires.
5. Why --privileged Is Dangerous
The --privileged option significantly changes the security boundaries normally applied to a container.
Avoid using --privileged simply because an application or troubleshooting command fails. Instead,
identify the exact operation that requires additional permission and grant only the required access.
6. Seccomp
Seccomp, short for Secure Computing Mode, is a Linux kernel feature that can restrict which system
calls a process can make.
Docker uses a default seccomp profile for containers. A custom profile can be supplied when required:
docker run --security-opt seccomp=/path/to/profile.json nginx
Keep the default profile unless there is a specific reason to customize it.
7. AppArmor and SELinux
Docker can work with Linux security systems such as AppArmor and SELinux. These mechanisms provide
additional access-control policies around processes and resources and can provide additional hardening
alongside Docker's other security controls.
8. Run Containers as Non-Root
A practical Docker security practice is to avoid running application processes as root unless root
privileges are genuinely required.
Example:
FROM python:3.12-slim
RUN useradd --create-home appuser
WORKDIR /app
COPY . .
RUN chown -R appuser:appuser /app
USER appuser
CMD ["python", "app.py"]
Running as a non-root user reduces the privileges available to the application process.
9. Docker Rootless Mode
Docker provides Rootless mode, which allows both the Docker daemon and containers to run without root
privileges. Rootless mode uses user namespaces and is intended to reduce the impact of vulnerabilities
in the daemon and container runtime.
10. Protect the Docker Socket
On Linux, Docker clients normally communicate with the Docker daemon through a Unix socket such as:
/var/run/docker.sock
Access to the Docker daemon is highly privileged. Controlling the Docker daemon can effectively provide
root-level control over the host.
Treat Docker socket access as a high-privilege operation.
11. Be Careful With Docker Socket Mounts
A configuration such as:
volumes:
- /var/run/docker.sock:/var/run/docker.sock
gives a container access to the Docker daemon socket.
Do not mount the Docker socket simply because a
container 'needs Docker'. First determine whether daemon-level access is really required and whether a
more restricted design is possible.
12. Remote Docker Daemon Security
Exposing the Docker daemon remotely introduces additional security considerations. Avoid exposing an
unauthenticated Docker API on an accessible network.
For secure remote access, Docker documents approaches including SSH and TLS. Remote daemon access should
be protected with appropriate authentication and encryption.
13. Docker Image Security
Security begins when the image is built, not only when the container starts.
A mature image workflow is:
Source Code → Dockerfile → Build Image → Security Scan → Review → Registry → Deployment
Potential image concerns include base image provenance, vulnerable packages, unnecessary packages,
unpinned dependencies, and accidentally included secrets.
14. Never Put Secrets in Dockerfiles
Avoid embedding passwords, API keys, credentials, or private configuration into Dockerfiles or images.
For example, avoid:
ENV DB_PASSWORD="MyPassword123"
and avoid copying credential files directly into images.
Use an appropriate secret-management mechanism for the deployment environment and inject secrets at
runtime when practical.
15. Use Minimal Base Images
A large image can contain more packages than an application needs. Reducing unnecessary components
can reduce the number of packages that need to be maintained and assessed for vulnerabilities.
However, the smallest possible image is not always the correct choice. Compatibility, debugging,
updates, and operational requirements also matter.
16. Keep Images Updated
Container security is not a one-time activity. Dependencies can become vulnerable after an image is
built.
A practical workflow is:
Build → Scan → Update dependencies → Rebuild → Scan again → Deploy
Regular rebuilds and updates should be part of the container lifecycle.
17. Docker Network Security
Containers communicate over Docker networks. A production design should avoid exposing every service
publicly.
A common application flow is:
Internet → Frontend/Reverse Proxy → API → Database
The database normally does not need direct public exposure. Network segmentation and limited port exposure
reduce unnecessary attack surface.
18. Read-Only Filesystems
If an application does not need to modify its root filesystem, consider:
docker run --read-only nginx
Applications that need writable temporary locations can be given specific writable mounts instead of
making the entire filesystem writable.
19. Resource Limits Are Also a Security Concern
Resource exhaustion can affect availability. Docker supports CPU and memory constraints.
Example:
docker run --memory=512m --cpus=1.0 myapp:1.0
Resource controls should be considered alongside availability and operational resilience.
20. A Secure Docker Container Example
A simplified hardened example is:
docker run -d \
--name secure-app \
--read-only \
--cap-drop=ALL \
--memory=512m \
--cpus=1 \
myapp:1.0
These settings demonstrate read-only filesystem use, reduced capabilities, and resource limits.
Whether every option is appropriate depends on the application.
21. Docker Security Layers
Docker security is best understood as defense in depth:
Application Security
Image / Supply Chain
Container Configuration
Capabilities / Seccomp
Namespace Isolation
Docker Engine / Daemon
Linux Kernel
Host Security
No single security feature should be considered sufficient.
22. Common Docker Security Mistakes
1. Running everything as root
2. Using --privileged unnecessarily
3. Exposing the Docker API without protection
4. Mounting the Docker socket unnecessarily
5. Putting passwords in images
6. Using untrusted images
7. Never scanning images
A mature workflow addresses these risks during build, deployment, and runtime.
23. Practical Docker Security Checklist
Image:
• Is the base image trusted and maintained?
• Are dependencies updated?
• Has the image been scanned?
• Are secrets excluded?
Container:
• Does the application need root?
• Can capabilities be dropped?
• Is --privileged really necessary?
• Can the filesystem be read-only?
• Are resource limits appropriate?
Network:
• Does the application need public exposure?
• Does the database need public exposure?
• Are unnecessary ports exposed?
Docker Engine:
• Who can access Docker?
• Is docker.sock protected?
• Is remote Docker access required?
• If required, is it appropriately authenticated and encrypted?
Host:
• Is the operating system maintained?
• Is SSH protected?
• Is the firewall configured?
• Are unnecessary services disabled?
24. Production Docker Security Architecture
A practical production model can use:
Internet
↓
Load Balancer
↓
Reverse Proxy
↓
Application Container
↓
Internal Network
↓
Alongside runtime security:
Source → Build → Image Scan → Registry → Controlled Deployment → Monitoring → Patch/Rebuild
This separates public traffic from internal application and database communication while integrating
security into the image lifecycle.
25. Docker Security: Core Principles
1. Least privilege — give applications only the privileges they require.
2. Defense in depth — use multiple security controls.
3. Minimize attack surface — reduce unnecessary packages, ports, privileges, mounts, and services.
4. Protect the Docker daemon — daemon access is highly privileged.
5. Don't trust the container boundary blindly — containers share the host kernel.
6. Secure the image supply chain — security starts before runtime.
7. Run applications as non-root where practical.
8. Monitor and update — vulnerabilities change over time.
26. Interview Questions
Beginner
1. Is Docker a virtual machine?
2. What are Linux namespaces?
3. What are Linux capabilities?
4. Why should containers avoid running as root?
5. What is Docker Rootless mode?
Intermediate
6. What is seccomp?
7. What does --cap-drop=ALL do?
8. Why is --privileged potentially dangerous?
9. Why is Docker socket access sensitive?
10. Why shouldn't Docker's remote API be exposed without protection?
Advanced
11. Explain Docker security using defense in depth.
12. What happens if an attacker compromises a container?
13. Why is mounting /var/run/docker.sock risky?
14. How would you harden a Docker container?
A strong answer to a hardening question should consider:
Trusted image → scan → non-root → drop unnecessary capabilities → appropriate seccomp →
read-only filesystem where practical → minimal network exposure → resource limits →
protected Docker daemon.
27. Key Takeaway
Docker security is not one feature. It is a layered model combining secure images, secure runtime
configuration, kernel isolation, daemon protection, host security, network controls, and secure
operations.
The core mindset is:
Start with the minimum privileges, minimum access, minimum exposed surface, and minimum required
components — and add permissions only when the application actually needs them.