CodeBlanks Logo Icon CodeBlanks

Summary - Environment

Combine ENV, ARG and -e to write a configurable image.

Summary: environment

In this group:

  • -e NAME=value gives a variable at run time; -e NAME takes it from the host.
  • ENV adds a permanent default to the image; -e overrides it.
  • ARG is valid only in the build; it's filled with --build-arg and before FROM can choose the base image.
  • --env-file loads many variables from a file; secret files aren't put in the repository.
  • WORKDIR is the directory for the following instructions; RUN cd isn't persistent.
docker build --build-arg SURUM=1.5.0 -t api:1.5.0 .
docker run -d --env-file app.env -e LOG_LEVEL=debug api:1.5.0

Practice Build and run settings

Write two commands: build api:1.5.0 from the current folder with the build argument SURUM=1.5.0; then start a container named api in the background with the app.env file and LOG_LEVEL=debug.

Starter code

terminal
# buraya yaz

Sample solution

terminal
docker build --build-arg SURUM=1.5.0 -t api:1.5.0 .
docker run -d --name api --env-file app.env -e LOG_LEVEL=debug api:1.5.0

Expected output

[+] Building 2.9s (8/8) FINISHED
 => naming to docker.io/library/api:1.5.0 done
b4c5d6e7f809

Questions and answers

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

1 What is done to also read a build argument's value in the running container?

  • Nothing; ARG stays automatically
  • -e ARG is written
  • It isn't possible
  • It is passed to ENV with ENV NAME=$NAME Correct answer

Explanation: ARG disappears after the build; ENV stays in the image.

2 Override the env file value with a command option.

Question
docker run -d --env-file app.env ____ LOG_LEVEL=debug api:1.0
Answer
docker run -d --env-file app.env -e LOG_LEVEL=debug api:1.0

Explanation: -e takes precedence over the value in --env-file.

3 Match.

Term Meaning
-e Run-time variable
ENV Image default
ARG Build variable
--env-file Load from file

All lessons in Beginner