Docker, start to ship · Chapter 24 of 26
Secrets are not env vars
By the end of this chapter you can find a secret baked into an image, build one without it, and give a container its secret as a read-only file instead. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Production habits
- 4 tasks
- Docker
- In the all labs plan
The first 2 chapters of Docker, start to ship are free. This one comes with the all labs plan.
Why you need this
The payment provider wrote to Meera: Tapri's live key was used from an unknown address. Bunty built chai-24-api:1.0 with the key written into its Dockerfile, and that image went to a contractor. Ravi has already made a new key (it is in secrets/payment.key), and Dev wants to know exactly what leaked, and that the next image carries no key at all.
Bunty's fix, already in app/Dockerfile, passes the key in when the image is built instead. Everything you have learned about images says what goes into a build stays in the image, so that needs checking before anything ships.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Find the key inside chai-24-api:1.0
Find the payment key that Bunty put inside the image chai-24-api:1.0.
1. Look at how the image was built (its history), and at its settings.
2. Find the key. It starts with tapri_live_.
3. Save the whole key in found.txt, in the chapter folder.
4. Run cat found.txt to check it.
To pass: found.txt has the whole key, from tapri_live_ to the end.
- Task 2
Build chai-24-api:2.0 with no key, and delete 1.0
Build a new image with no key in it, and delete the leaky one.
1. Read app/Dockerfile. Bunty's new version still puts the key into the image.
2. Change app/Dockerfile so the build needs no key at all.
3. Build the image from the app folder as chai-24-api:2.0.
4. Delete the image chai-24-api:1.0.
To pass: chai-24-api:2.0 has no PAYMENT_KEY setting. Neither key (the old one or Ravi's new one) is in its environment, its history or its files under /app. The image chai-24-api:1.0 is gone.
- Task 3
Run chai-24-api from 2.0 with the key as a file
Run the payments API with Ravi's new key as a file, not as a variable.
1. Read app/server.js. It says which file it reads the key from.
2. Run a container named chai-24-api from chai-24-api:2.0, in the background, with its port 8024 published on the lab's port 8024.
3. Mount the file secrets/payment.key, read-only, at /run/secrets/payment_key in the container.
4. Run curl localhost:8024.
To pass: chai-24-api is running from chai-24-api:2.0. localhost:8024 says the key came from a file, and shows the last four characters of the key in secrets/payment.key. The key is not in the container's environment, and the mounted file is read-only.
- Task 4 · interview question
Interview: why env vars are bad for secrets
Interview question: why is an environment variable a bad place for a secret?
1. Think: which people can see a container's environment?
2. Think: what else can see it, such as other processes and logs?
3. Write one or two sentences in your own words in answer.txt, in the chapter folder.
To pass: answer.txt says who or what can see a secret in an environment variable.
What you will learn
- docker history --no-trunc and docker inspect: read the instructions and settings an image was built with.
- Layers keep what later steps delete: docker save and tar dig it out.
- A secret as a read-only file: mounted into the container when it runs, never part of the image.
More in Production habits
Build "Secrets are not env vars" yourself
Tapri’s menu site works on one laptop and nowhere else. You ship the machine. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Docker, start to ship are free. This one comes with the all labs plan.