Docker Security Basics
Containers feel like tiny locked rooms, and many beginners assume they are automatically safe. A container shares the operating system kernel with its host, so careless settings can expose the whole machine. Good security habits cost little effort and prevent serious problems.
How Containers Isolate Programs
Docker builds isolation from two Linux features.
Namespaces give each container its own view of the system
(its own process list, network, and file system)
Cgroups limit how much CPU, memory, and disk a container can use
Picture an apartment building. Each apartment has its own door and its own rooms, but every apartment shares the same foundation and plumbing. A virtual machine is closer to a detached house with its own foundation.
Virtual Machines Containers +------+ +------+ +------+ +------+ | App | | App | | App | | App | | OS | | OS | +------+ +------+ +------+ +------+ | Docker Engine | | Hypervisor | | Shared Kernel | +---------------+ +----------------+
The Main Risk Areas
+------------------+----------------------------------------+ | Area | What can go wrong | +------------------+----------------------------------------+ | Images | old software with known flaws | | Container config | too many permissions | | Secrets | passwords stored inside images | | Network | ports open to the whole internet | | Docker daemon | an exposed control socket | +------------------+----------------------------------------+
Habit 1: Trust Your Image Sources
Pull images from official repositories and verified publishers. Random images from unknown authors may contain outdated or harmful software. Check the Docker Hub badge for Docker Official Image or Verified Publisher before you pull.
Habit 2: Keep Images Small
Every extra tool in an image adds a possible weakness. Slim and Alpine base images carry fewer packages, so they present fewer targets.
python:3.12 about 1 GB many packages python:3.12-slim about 150 MB fewer packages python:3.12-alpine about 60 MB minimal packages
Habit 3: Pin Versions
The tag latest changes without warning. Pin a specific version so every build produces the same result.
FROM node:20.11-alpine (predictable) FROM node:latest (unpredictable)
Habit 4: Avoid Privileged Mode
The --privileged flag gives a container almost the same power as the host. Use it only for rare system tools and never for ordinary applications.
Habit 5: Protect the Docker Socket
The file /var/run/docker.sock controls the Docker engine. A container that mounts this file can start, stop, and inspect every other container on the host. Mount it only when a trusted tool truly needs it.
Habit 6: Publish Only Needed Ports
The flag -p 8080:80 opens the port on every network interface. Bind to localhost when only local access is required.
docker run -p 127.0.0.1:8080:80 my-app local access only docker run -p 8080:80 my-app reachable from outside
Habit 7: Update Regularly
Rebuild images often so base image fixes reach your application. Update the Docker engine itself as new versions arrive.
Security Checklist
- Choose official, slim base images.
- Pin version tags.
- Run as a non-root user.
- Scan images for known flaws.
- Keep secrets out of images.
- Limit ports, memory, and permissions.
- Rebuild and redeploy on a regular schedule.
Print the checklist and apply it to your own images today.
Real-World Scenario
A developer finds a convenient image on a public registry that bundles a web server and several tools. The image has no description, and the author is unknown. The developer runs it with the Docker socket mounted, because a tutorial suggested the setup. The image contains an old library with a published weakness, and the mounted socket gives any intruder control of every container on the host. A few small choices turned a harmless experiment into a serious risk.
A safer path uses an official image, a pinned version, no socket mount, and a localhost-only port. The application works the same way, and the dangerous doors stay closed.
Layers of Defense
Layer 1 trusted, small, scanned image Layer 2 non-root user inside the container Layer 3 limited permissions and read-only files Layer 4 restricted network exposure Layer 5 secrets kept outside the image Layer 6 updated host and Docker engine
Security experts call this approach defense in depth. No single layer promises perfect safety, and several layers together make an attack difficult and expensive.
Understanding the Shared Kernel
The kernel is the core of the operating system, and every container on a host talks to the same one. A serious flaw in the kernel can affect all containers at once. Keep the host operating system patched, and consider extra isolation tools such as seccomp profiles and AppArmor, which Docker enables by default on most Linux systems. These tools limit which system calls a container may make, so even a compromised program has fewer ways to reach the kernel.
