Containers: images, hosts and runtime limits
Unit 4 of 5 in Cloud identity, network and data protection.
You will be able to build a leaner image without secrets in it, scan and lint it, measure a Docker host against a benchmark and run a container with fewer privileges.
Lesson
What a container isolates, and what it does not
Docker's security page says there are four areas to consider: the kernel's namespaces and control groups, the attack surface of the Docker daemon, loopholes in the container configuration profile and the kernel's hardening features. When you start a container, Docker creates a set of namespaces and control groups for it. Namespaces give the first form of isolation, so processes in a container cannot see processes in other containers or on the host, and each container gets its own network stack. Control groups account for and limit resources such as memory and CPU, which helps stop one container from exhausting the machine, but the page says they play no part in stopping one container from reaching another's data.
The daemon needs root privileges unless you opt in to rootless mode, and the page says only trusted users should be allowed to control it, because Docker lets you share a host directory with a container without limiting the container's access rights. So someone who can start containers can alter the host's filesystem. By default Docker starts containers with a restricted set of capabilities, an allow list of the few root-like powers a container needs, so that root inside a container has much less power than real root.
Building an image with care
Docker's build guide says the first step to a secure image is choosing a base image from a trusted source and keeping it small, and that a small image with minimal dependencies can considerably lower the attack surface. It suggests a build image and a slimmer production image, with a multi-stage build so the result holds only the files needed to run the app. Image tags are mutable, so the guide says to pin base image versions, and because images are immutable snapshots it says to rebuild regularly so you pick up fixes. If a service can run without privileges, use USER to switch to a non-root user.
The distroless lab builds the same small service twice, once on a full base and once on a distroless base, and measures the difference in image size, vulnerability count and whether a shell exists.
A deleted secret is not deleted
Docker's page on build secrets says build arguments and environment variables are inappropriate for passing secrets to a build, because they persist in the final image. It recommends secret mounts or SSH mounts, which make a secret temporarily available inside the build container for the duration of one instruction.
The secrets lab shows why with your own test project: you build an image the naive way, delete the secret file in a later step, and then recover it from the image's earlier layer. Then you rebuild with a secret mount and show it is gone. Layers are history; deleting a file in a later layer hides it and does not remove it.
Lint it and scan it
hadolint is a Dockerfile linter. Its project page says it parses the Dockerfile into an abstract syntax tree and applies rules to it, and runs ShellCheck over the commands inside RUN instructions. It reads the text, so it catches a mistake before an image exists.
Trivy scans a built image. Its documentation says it scans the files inside container images for vulnerabilities, misconfigurations, secrets and licenses, with vulnerability and secret scanning on by default. The result is a table of packages with installed and fixed versions and severity ratings.
Both labs end with the same honest limit. A clean lint is not a secure image, and a short vulnerability list is not a safe system: neither tool knows how the container is run, what it can reach or whether the application itself is sound.
Measure the host, then limit the runtime
Docker Bench for Security is described on its project page as a script that checks dozens of common best practices for deploying Docker containers in production. The tests are automated and based on the CIS Docker Benchmark v1.6.0. It is meant to let the Docker community self-assess hosts and containers. The lab runs it on your own host, reads the PASS, WARN, INFO and NOTE lines and relates a check to a docker run flag, and it warns that some warnings are artifacts of your environment, not real findings.
The runtime-hardening lab then limits a deliberately over-privileged container one control at a time and proves each by making the old behavior fail: run as non-root, drop capabilities, forbid gaining new privileges, make the root filesystem read-only and filter system calls. Those five map directly to the securityContext settings you will meet in Kubernetes in the next unit.
Key points
- Namespaces isolate, control groups limit resources, and the daemon is a powerful thing to expose; only trusted users should control it.
- Choose a small trusted base, use multi-stage builds, pin versions, rebuild often and run as a non-root user.
- Build arguments and environment variables persist in the image; use build secret mounts instead.
- A linter reads the Dockerfile, a scanner reads the image, a benchmark reads the host; none of them proves the system is secure.
Sources
- Docker Docs: Docker Engine security
- Docker Docs: Building best practices
- Docker Docs: Build secrets
- hadolint: Dockerfile linter (project page on GitHub)
- Trivy documentation: Container image target
- Docker Bench for Security (project page on GitHub)
Practice in the labs
Assess your own Docker host, lint a Dockerfile, recover and then prevent a leaked secret, scan an image, harden a runtime and shrink an image. All run on your own computer with Docker; nothing touches a system you do not own.
- Assess YOUR OWN Docker host with Docker Bench for Security and fix what it flags: You run the benchmark script on your own host, start a weak and a hardened container and re-run one check to prove a fix.
- Lint a Dockerfile for security with hadolint — catch the mistakes before the image exists: You lint a deliberately bad Dockerfile, count the findings, fix them and re-lint, ending on why a clean lint is not a secure image.
- Recover a "deleted" secret from Docker image layers — then keep it out with BuildKit secret mounts: You recover a deleted secret from an image's layers, then keep it out with a BuildKit secret mount and a multi-stage build.
- Scan a container image for vulnerabilities with Trivy: You scan an old public image, read the table of installed and fixed versions and watch the count fall on a newer image.
- Harden a container: non-root, drop capabilities, seccomp: You take an over-privileged container to a locked-down one and prove each control by making the un-hardened behavior fail.
- Cut a container's attack surface with a distroless image — and measure what you removed: You build one service on a full base and on distroless and put image size, vulnerability count and shell presence side by side.
Knowledge check
- Why does Docker say build arguments and environment variables are inappropriate for build secrets? Answer: They persist in the final image. Docker's page says build arguments and environment variables persist in the final image, so a secret passed that way can be read from the image later. Secret mounts expose the secret only for the build instruction.
- You delete a secret file in the last step of a Dockerfile. Is it gone from the image? Answer: No, it can still be recovered from an earlier layer. Image layers are a history. A later layer hides a file but the earlier layer still contains it, which the lab shows by recovering the file from the image's saved layers.
- What does Trivy scan for inside a container image, according to its documentation? Answer: Vulnerabilities, misconfigurations, secrets and licenses. Trivy's page says it scans the files inside container images for vulnerabilities, misconfigurations, secrets and licenses, with vulnerability and secret scanning enabled by default.
- A host scores well on Docker Bench for Security. What does that show? Answer: It passed the automated checks based on the CIS Docker Benchmark, which is not proof the system is secure. The tool automates dozens of best-practice checks from a benchmark. A good score means those checks passed, and the lab itself says what a benchmark cannot tell you about how things are used.
Back to the syllabus or see all classes.
Related
- Cloud identity, network and data protection
- Assess YOUR OWN Docker host with Docker Bench for Security and fix what it flags
- Lint a Dockerfile for security with hadolint — catch the mistakes before the image exists
- Recover a "deleted" secret from Docker image layers — then keep it out with BuildKit secret mounts
- Scan a container image for vulnerabilities with Trivy
- Harden a container: non-root, drop capabilities, seccomp
- Cut a container's attack surface with a distroless image — and measure what you removed
- All classes
- Free cyber labs