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

Sources

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.

Knowledge check

  1. 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.
  2. 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.
  3. 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.
  4. 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