Container Security Basics
Containers share the host kernel — a container escape can become a host compromise.
Introduction
Containers share the host kernel — a container escape can become a host compromise. Security in Docker is layered: image minimization, non-root users, capability dropping, read-only filesystems, and scanning.
Purpose of this lesson
This lesson teaches Container Security Basics as an engineering decision: what problem it solves, when to use it, how to implement it safely, and what signals tell you it is failing.
Understanding the topic
Use this in production readiness work. These practices decide whether containers are merely running or are actually secure, observable, recoverable, and safe to promote through a delivery pipeline.
Core concepts to understand:
- Run as non-root (
USER appuserin Dockerfile). --read-onlyfilesystem; mount/tmpas tmpfs.- Drop Linux capabilities (
--cap-drop=ALL, add only what's needed). - Use a seccomp/AppArmor profile; enable rootless mode.
Visual explanation
Architecture or command flow to keep in mind:
# DockerfileRUN adduser -D appuserUSER appuser# Runtimedocker run --read-only \--cap-drop=ALL \--security-opt no-new-privileges \myapp
Step-by-step explanation
- Define the production risk first: credential leakage, vulnerable base image, runaway resource use, missing logs, or broken delivery flow.
- Apply the smallest control that reduces the risk: non-root user, read-only filesystem, scan gate, secret mount, limit, or log driver.
- Prove the control with commands such as
docker inspect,docker history,docker stats, and a failing test case. - Automate the check in CI or operational runbooks so it does not depend on memory.
- Review the result after a real incident or release and tighten the policy where evidence shows gaps.
Informative example
Use the example below as a working baseline, then verify the runtime behavior instead of assuming the command or file is correct.
# DockerfileRUN adduser -D appuserUSER appuser# Runtimedocker run --read-only \--cap-drop=ALL \--security-opt no-new-privileges \myapp
A production-minded check usually includes docker ps, docker logs, docker inspect, and one validation from outside the container such as curl, a database connection, or a registry pull.
# Verification loop for Container Security Basicsdocker ps -adocker logs --tail=100 <container-name>docker inspect <container-or-image-name>docker system df
Real-world use
A non-root container with --cap-drop=ALL stops 90% of real-world container escapes cold. Almost nobody does it. Be in the 10%.
Enterprise use cases
In a mature engineering organization, Container Security Basics is documented as a repeatable pattern with approved base images, ownership labels, CI checks, security expectations, rollback notes, and troubleshooting commands. The difference between a tutorial and production practice is that every container decision must be observable, reviewable, and reversible.
Best practices
- Default to non-root.
- Drop all caps; add back only what's needed.
- Pin image digests + scan continuously.
Common mistakes
- Running as root just because it's easier.
- Using
--privileged'temporarily'.
Debugging tips
- Inspect effective runtime settings with
docker inspect; do not assume the Dockerfile or Compose file applied as expected. - For resource incidents, compare
docker stats, exit codes, OOMKilled state, and host memory pressure. - For supply-chain issues, trace the exact image digest from build logs to registry to deployment.
Optimization strategies
- Treat image size, CVE count, non-root execution, and digest pinning as release quality signals.
- Set log rotation and resource limits before the first production incident, not after disk or memory exhaustion.
- Cache builds by registry or CI cache backend while still scanning the final pushed image.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1QuestionWhy run as non-root?+
Answer
2QuestionWhat does <code>--cap-drop=ALL</code> do?+
Answer
3QuestionWhat is rootless Docker?+
Answer
Hands-on exercise
Create a small lab for Container Security Basics: run the example, inspect the created Docker object, intentionally introduce one mistake, and record the command that reveals the failure. The goal is not just to make the happy path work; it is to build operational reflexes.
# Hands-on lab scaffoldmkdir -p docker-container-security-basics-labcd docker-container-security-basics-lab# Add the Dockerfile, compose.yml, or command from this lesson.# Then run one happy-path test and one broken-path test.docker versiondocker infodocker system df
Summary
Container Security Basics matters because Docker is not only a packaging tool; it is a runtime, build, networking, storage, and delivery workflow. Treat each lesson as a production habit: make it repeatable, inspectable, secure, and easy to debug.