CodeBlanks Logo Icon CodeBlanks

The -e flag

Learn to pass environment variables with -e.

The -e flag

Environment variables are the way to run the same image with different settings in different environments (development, test, production). -e NAME=value gives the container a variable; several -e flags can be written:

docker run --rm -e AD=Ayşe -e DIL=tr alpine sh -c 'echo "$AD $DIL"'
# Ayşe tr

If no value is written (-e TOKEN), the variable is taken from the host variable of the same name.

Seeing the variables

All environment variables inside a container are seen with env. docker inspect shows them too; that is why passwords given with -e can be read by anyone with access to the container:

$ docker exec db env | grep POSTGRES
POSTGRES_PASSWORD=gizli
POSTGRES_DB=app
$ docker inspect -f '{{.Config.Env}}' db

In production, Docker secrets or Kubernetes Secrets are preferred for secret values.

Practice A configured database

Start a container named db from postgres:16 in the background with three environment variables: POSTGRES_USER=app, POSTGRES_PASSWORD=gizli, POSTGRES_DB=magaza_db.

Starter code

terminal
# buraya yaz

Expected output

92a3b4c5d6e7

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 In docker run -e TOKEN img, where does TOKEN's value come from?

  • From the TOKEN environment variable on the host Correct answer
  • It is always empty
  • From the image
  • It errors

Explanation: This is one way to keep a password out of the command history.

2 Give the container an environment variable.

Question
docker run --rm ____ DIL=tr alpine env
Answer
docker run --rm -e DIL=tr alpine env

Explanation: -e (env) takes NAME=value.

3 Match.

Term Meaning
-e NAME=value Assign a variable
-e AD Take from the host
env List the variables
docker inspect Shows the settings

All lessons in Beginner