TL;DR
Running Docker images from untrusted sources can be risky. This guide explains common attack vectors and how to protect your system.
Understanding the Risks
Untrusted Docker images are a potential gateway for attackers. They can contain malicious code that compromises your host machine or network. Here’s what you need to know:
1. Image Source & Verification
- Only use trusted registries: Prefer official repositories like Docker Hub (but even there, be cautious) and verified publishers.
- Check image tags: Understand the tag’s meaning. Tags like
latestare often mutable and less reliable than specific version numbers. - Verify publisher signatures: If available, verify the image’s signature to ensure it hasn’t been tampered with. Docker Content Trust (DCT) can help with this.
docker trust inspect:
2. Container Privileges
- Run containers as non-root: Avoid running containers with root privileges whenever possible. Use the
USERinstruction in your Dockerfile or the--userflag when running.docker run --user: : - Limit capabilities: Drop unnecessary Linux capabilities. Capabilities grant containers specific privileges; remove those not required.
docker run --cap-drop ALL --cap-add NET_BIND_SERVICE: - Avoid privileged mode: Never run containers in
--privilegedmode unless absolutely necessary. This gives the container almost full access to the host system.
3. Network Security
- Use network policies: Restrict network access for containers. Allow only necessary communication between containers and external networks. Tools like Docker’s networking features or third-party network policy engines can help.
- Port mapping: Carefully consider which ports you expose from the container to the host. Only expose essential ports.
docker run -p 8080:80: - Firewall rules: Implement firewall rules on your host system to control inbound and outbound traffic to containers.
4. Filesystem Access
- Read-only root filesystem: Mount the container’s root filesystem as read-only to prevent modifications.
docker run --read-only: - Volume mounts: Be cautious with volume mounts. Avoid mounting sensitive host directories into containers unless absolutely necessary. If you must, ensure appropriate permissions are set.
- Tmpfs mounts: Use
tmpfsmounts for temporary files to avoid writing data to the persistent filesystem.docker run --mount type=tmpfs,destination=/tmp:
5. Image Scanning
- Scan images for vulnerabilities: Use image scanning tools (e.g., Trivy, Clair, Snyk) to identify known security vulnerabilities in the image’s layers and dependencies.
trivy image: - Automate scans: Integrate image scanning into your CI/CD pipeline to automatically detect vulnerabilities before deploying images.
6. Resource Limits
- Set resource limits (CPU, memory): Limit the amount of CPU and memory a container can consume to prevent denial-of-service attacks or resource exhaustion.
docker run --memory 512m --cpu-shares 512:
7. Monitoring & Logging
- Monitor container activity: Monitor containers for suspicious behavior, such as unexpected network connections or file modifications.
- Centralized logging: Collect and analyze container logs to detect potential security incidents.
8. cyber security Best Practices
Regularly update Docker itself and the underlying host operating system with the latest security patches.

