Environment variables

Environment variables configure a container at start without rebuilding its image: the same image runs in staging and in production with different values. -e, --env-file and environment: in Compose set them.

How it works

Values given at run time override the ENV defaults of the image. docker exec api env or docker inspect show what the container really received. In Compose, ${VAR} in the file is replaced from the shell or from a .env file before the container starts.

What breaks

Changing a value means recreating the container: docker restart keeps the old environment. A missing variable often crashes the app at start, so read docker logs first. And never bake a password into the image with ENV.

In a terminal

docker run -d --name api -e PORT=3000 --env-file ./prod.env orders-api
docker exec api env

See it break, then fix it

Write the Dockerfile or the compose file, type the docker commands, and watch a simulated server run them through reboots, upgrades and traffic. Free to start.