CD1: Containers done well

Lesson 3 of 5 · reading 15–25 minutes; practice and project time is on the module page

Running containers safely

🎯You'll be able to

Run an app as an unprivileged user, keep secrets out of images, and set limits so one container can't starve the rest.

🏠The everyday picture

A hotel gives each guest a key card that opens their own room, not a master key. It also doesn't print the safe's code on the room's welcome card, where every later guest would read it.

📘What it really is

📝A worked example

Input: docker run --rm --entrypoint id routerani:latest -u (starter image), then the same for your routerani:1.0.0.

Expected output: 0 for the starter (root) and 1000 for yours. Then docker history --no-trunc routerani:latest | grep -i password prints nothing, because no password is baked in. In the starter Terraform, the password routerani123 is in main.tf instead: still a problem, which CD3 fixes with a generated password.

🌍Where it's used

📝Case scenario

A startup published its app image to a public registry for a partner. The Dockerfile had ENV AWS_SECRET_ACCESS_KEY=... for a build step. Within a day, someone pulled the image, read the key from docker inspect and started expensive servers on the company's account. The fix was to remove the key, rotate it, delete the published tag and pass credentials only at run time.

🖱️Try it

Run docker run --rm --memory 64m --entrypoint node routerani:1.0.0 -e "const a=[]; for(;;) a.push(Buffer.alloc(1e6))" and then echo $?. Success: the command stops with exit code 137, which means the process was killed for using too much memory.

⚠️Common mistakes

🛠️For your project

Project 1 test 3 fails if the container runs as root, and test 2 fails if a database password is written in a .tf file.

✅Quick check

Why is ENV API_KEY=... in a Dockerfile a problem?

Show the answer

Anyone who can pull the image can read it with docker inspect or docker history.

What happens when a container goes over its memory limit?

Show the answer

The kernel kills the process (exit code 137) instead of letting it slow down the host.

← PreviousA Dockerfile that builds small and fast
ProLearners Assist