Summary - Environment
- Docker & Kubernetes course
- Beginner
- Lesson 60/82
- 5 min
Combine ENV, ARG and -e to write a configurable image.
Summary: environment
In this group:
-e NAME=valuegives a variable at run time;-e NAMEtakes it from the host.ENVadds a permanent default to the image;-eoverrides it.ARGis valid only in the build; it's filled with--build-argand before FROM can choose the base image.--env-fileloads many variables from a file; secret files aren't put in the repository.WORKDIRis the directory for the following instructions;RUN cdisn'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.0Practice 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
# buraya yazSample solution
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.0Expected 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.
docker run -d --env-file app.env ____ LOG_LEVEL=debug api:1.0
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 |