DockerSecurity

Container Security: Ten Practical, Documented Rules

Run as non-root, avoid --privileged, drop capabilities, use read-only filesystems, secrets and resource limits: a real hardening checklist.

Container Security: Ten Practical, Documented Rules
Table of contents
  1. 1. Run as a non-root user
  2. 2. Never use --privileged
  3. 3. Drop capabilities you don't need
  4. 4. Read-only filesystem
  5. 5. Keep passwords in secrets
  6. 6. Limit resources
  7. 7. Use images from trusted sources
  8. 8. Scan your images
  9. 9. Keep the host kernel up to date
  10. 10. Use healthchecks to notice failures early

A container is not a security boundary; it is an isolation layer. These ten practical, documented rules genuinely reduce the risk of a Docker stack.

1. Run as a non-root user

Many official images, such as WordPress and phpMyAdmin, already run their main process as www-data. If you write your own Dockerfile, do the same:

USER appuser

Or at run time: docker run --user 1000:1000 myimage. The goal: if a vulnerability inside the container is exploited, the attacker does not get root in the container, and more importantly does not get a path to root on the host.

2. Never use –privileged

This flag removes practically every restriction. If a tool really needs a special capability, grant only that one:

cap_add:
  - NET_ADMIN

3. Drop capabilities you don’t need

For many services, cap_drop: [ALL] followed by adding back the minimum required is a safer default.

4. Read-only filesystem

read_only: true
tmpfs:
  - /tmp

A static web server has nothing to write apart from its log and cache paths.

5. Keep passwords in secrets

A database password in plain Compose text or a stray env file leaks into logs and backups. Use the secrets section in Compose so the password reaches the container as a file.

6. Limit resources

deploy:
  resources:
    limits:
      memory: 512M

This stops a single leaking container from eating all of the server’s memory.

7. Use images from trusted sources

Official library/* images on Docker Hub are maintained and reviewed. Don’t run unknown images with no history behind them.

8. Scan your images

Tools such as docker scout or Trivy report known vulnerabilities in image layers and can be wired into CI.

9. Keep the host kernel up to date

Well-known container escape bugs are fixed at the Linux kernel level, so a regular apt upgrade of the server itself is part of your Docker security.

10. Use healthchecks to notice failures early

Monitoring the real state of services, rather than relying on “it’s running”, means that when something breaks you find out before your users do.

About the author

Arash Karimian

Network and infrastructure engineer with years of day-to-day work on Linux servers, firewalls and network tuning. Writes about networking, security and server troubleshooting.

All articles by Arash Karimian

Comments

No comments yet. For technical questions, include the details (OS version, the exact error and the command you ran) to get a precise answer.

Leave a comment