Sub topic 6
Passing configuration with environment variables
This chapter assumes you can build and run the Node.js image from the earlier chapters, and that you know how docker run publishes a port. You should also know that a process reads environment variables from its parent.
Why configuration stays out of the image
An image should be the same in development, testing, and production. Values that change between those places, such as a database URL, a log level, or a port, belong outside the image. Docker passes them to the container as environment variables when the container starts.
The Node.js app reads them from process.env:
const http = require("http");
const port = Number(process.env.PORT) || 3000;
const greeting = process.env.GREETING || "Hello";
http
.createServer((req, res) => {
res.end(`${greeting} from port ${port}\n`);
})
.listen(port, () => console.log(`Listening on ${port}`));Three ways to set variables
Docker gives you three common sources. Each one applies at a different time.
| Source | When it applies | Typical use |
|---|---|---|
| `ENV` in the Dockerfile | Baked into the image as a default | `NODE_ENV=production` |
| `-e` or `--env` on `docker run` | One container, overrides the image | `GREETING=Hi` |
| `--env-file` on `docker run` | One container, many values from a file | A set of settings per environment |
The Dockerfile from the earlier chapter already sets ENV NODE_ENV=production. Every container started from that image gets this value unless you override it.
Set a value at run time with -e:
docker run --rm -d -p 3000:3000 -e GREETING=Hi --name myapp myapp:1.0
curl http://localhost:3000
docker stop myappIf you pass -e GREETING with no value, Docker copies the variable from your current shell. If the shell does not have it, the variable is not set in the container.
For many values, put them in a file with one NAME=value per line:
GREETING=Hi
LOG_LEVEL=debugdocker run --rm -d -p 3000:3000 --env-file .env.dev --name myapp myapp:1.0Lines starting with # are comments. Do not wrap values in quotes, because the env file keeps the quote characters as part of the value.
Which value wins
When the same name appears more than once, the most specific source wins. A -e flag overrides --env-file, and both override ENV in the image. You can check what a running container received:
docker exec myapp printenv GREETING
docker inspect --format '{{json .Config.Env}}' myappCommon errors
- Secrets in the image. A value set with
ENVor copied in withCOPYis stored in the image layers, and anyone who can pull the image can read it. Pass secrets at run time instead. This is also why.envis listed in.dockerignore. - Changing the value without restarting. Variables are fixed when the container starts. Editing the env file does nothing to a running container. Stop it and run it again.
- Strings, not numbers. Every value arrives as a string.
process.env.PORTis"3000", so convert it withNumber()as the example does. - Port mismatch. If you set
PORT=4000in the container but still publish-p 3000:3000, the app listens on 4000 and the published port reaches nothing. Change the container side too:-p 3000:4000.
Exercise
You start a container with docker run --env-file .env.dev -e GREETING=Yo myapp:1.0. The file contains GREETING=Hi, and the Dockerfile has ENV GREETING=Hello. What value does the app see, and what happens if you edit .env.dev while the container runs?
Show answer
The app sees Yo, because the -e flag overrides both the env file and the image default. Editing .env.dev while the container runs has no effect, because variables are fixed at start. You must stop and restart the container to apply the change.