Hardening Configs
Docker configuration files, such as the daemon configuration (/etc/docker/daemon.json) and container runtime settings, are critical attack surfaces if left unsecured. Misconfigurations can expose sensitive data, allow privilege escalation, or enable unauthorized access to Docker APIs. This section outlines strategies to harden these files by limiting permissions, restricting API access, and disabling unused features.
Securing Docker Daemon Configuration¶
The Docker daemon configuration file (/etc/docker/daemon.json) defines critical runtime parameters. Ensure it is protected against unauthorized modifications and configured to minimize attack surfaces.
Key Hardening Steps¶
- Avoid root privileges: Run Docker as a non-root user (e.g.,
nobodyor a dedicated service account). - Limit storage drivers: Use secure, production-ready storage drivers like
overlay2. - Secure the Docker socket: Restrict access to the Unix socket (
/var/run/docker.sock) to trusted processes. - Disable insecure registries: Avoid exposing internal registries to untrusted networks.
Example: Secure daemon.json¶
{
"user": "nobody",
"storage-driver": "overlay2",
"registry-mirrors": ["https://registry-1.docker.io"],
"hosts": ["unix://var/run/docker.sock"]
}
Additional Safeguards¶
- Systemd capabilities: Use
systemdto drop unnecessary capabilities: - File permissions: Ensure the config file has strict permissions:
Restricting Docker API Access¶
The Docker API provides powerful control over containers but must be secured to prevent unauthorized access.
Key Hardening Steps¶
- Bind the API to localhost: Avoid exposing the API over TCP/IP.
- Enable TLS: Use encrypted connections with client certificates.
- Limit API endpoints: Restrict access to specific endpoints (e.g.,
GET /containers).
Example: TLS Configuration¶
- Generate TLS certificates (e.g.,
server.pemandserver-key.pem). - Configure
daemon.json: - Use client certificates to authenticate:
Access Control¶
- Use reverse proxies (e.g., NGINX) with authentication layers to gate access to the Docker API.
- Avoid exposing the API over public networks unless absolutely necessary.
Disabling Unused Features¶
Unused Docker features can introduce vulnerabilities. Disable or remove them to reduce the attack surface.
Key Hardening Steps¶
- Disable experimental features: These are often unstable and insecure.
- Remove unused plugins: Use
docker plugin rmto uninstall unused plugins. - Limit container capabilities: Use
--cap-dropto restrict capabilities (e.g.,CAP_NET_ADMIN).
Example: Restricting Capabilities¶
Additional Recommendations¶
- Audit running containers: Use
docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Status}}"to identify unnecessary containers. - Prune unused resources: Regularly clean up stopped containers and unused images:
Key takeaways¶
- Secure the Docker daemon configuration by using non-root users, secure storage drivers, and strict file permissions.
- Restrict Docker API access to localhost and enforce TLS for remote connections.
- Disable unused features like experimental modes and unnecessary plugins to minimize risks.