Linux Labs

Containers: Docker and compose / Stage 2 of 11

Ports, variables and restart

Not running Starting usually ~20 s
Leave the lesson

Step 3 of 8 · on a machine · 6 min

Environment variables

Settings are most often passed to an application in a container through environment variables.

The main process gets its environment variables from Docker. Some are set in the image, such as PATH. You add your own with the -e NAME=value option of docker run, one -e per variable. Many ready-made images are configured this way: the operating mode, a database address, a user name.

There are two ways to see what the process got. docker exec <name> env prints the environment inside a running container. docker inspect <name> prints the full description of the container as JSON, and the --format option picks a field from it — here the list of variables:

bash
sudo docker inspect --format '{{json .Config.Env}}' worker

Like ports, the environment is set at creation: you cannot change it on an existing container, only re-create the container with the right -e options.

The server runs a worker container from the worker:1 image, a practice application that checks a task queue every 5 seconds. It takes its operating mode from the APP_MODE variable and writes it to its log.

Exercise · do it

Switch worker to prod mode

Look at the log and the environment of the worker container, then switch the application to prod mode: a line with mode=prod must appear in the worker log. The image is worker:1, the container name stays the same.

Sign in and start The machine is available after signing in

Goals · 0 of 2

  • Give the application the prod mode not done yet
  • Keep worker running on the worker:1 image not done yet

Step goals

0 of 2
  • Give the application the prod mode — not done yet
  • Keep worker running on the worker:1 image — not done yet
Ubuntu 24.04

Step goals

0 of 2
  • Give the application the prod mode — not done yet
  • Keep worker running on the worker:1 image — not done yet
Loading…