mediumMultiple Select
XK0-006 Practice Question: Which two of the following are valid methods to…
Which two of the following are valid methods to pass environment variables to a Docker container at runtime? (Select TWO.)
⚠ Common exam trap
CompTIA often tests the distinction between build-time instructions (`ENV`, `ARG`) and runtime options (`-e`, `--env-file`), so the trap here is confusing the `ENV` instruction in the Dockerfile (which sets variables at build time) with the `-e` option (which sets variables at runtime).
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Defining variables in a .env file and using --env-file
Option A is correct because `docker run --env-file <file>` reads a file of KEY=VALUE lines (commonly named .env) and injects those variables into the container's environment at runtime. Option B is correct because `docker run -e KEY=VALUE` (or `--env`) sets an environment variable directly on the container when it starts. Option C is not a runtime method: the Dockerfile `ENV` instruction bakes variables into the image at build time, so they exist by default but are not passed at runtime. Option D is invalid because `export` executed inside a running container only affects that shell session, not the container's startup environment. Option E is incorrect because the Dockerfile `ARG` instruction defines build-time variables, which are not automatically available as runtime environment variables.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
Defining variables in a .env file and using --env-file
Why this is correct
Passing `--env-file` reads the named file and injects each key-value pair as an environment variable inside the container at start-up, satisfying the runtime requirement without baking values into the image. This differs from build-time `ARG`, which cannot populate container environment variables after the image is built.
- ✓
Using the -e option in docker run
Why this is correct
Passing `-e KEY=value` to `docker run` injects the variable directly into the container's process environment at start-up, satisfying the runtime requirement. Unlike build-time `ARG` or `ENV` in a Dockerfile, which bake values into the image, `-e` supplies configuration dynamically per container instance without rebuilding.
- ✗
Using the ENV instruction in the Dockerfile
Why it's wrong here
The ENV instruction sets variables at image build time, baking them into the image layers, so they are not passed at container runtime. It is tempting because ENV is the standard way to define defaults for an image, and it would be correct when the value is static and identical for every container started from that image.
- ✗
Using the export command inside the container
Why it's wrong here
Export runs a shell builtin inside a running container's process, affecting only that shell session, not the container's configured environment at creation. It is tempting because export does set variables for processes, but that is for interactive shell use, not runtime injection.
- ✗
Using the ARG instruction in the Dockerfile
Why it's wrong here
ARG defines build-time variables that are not persisted in the final image unless used with ENV.
Go deeper
Related to this question
Learn chapter
Managing Storage and File Systems
Key term
Value
Value is the perceived worth, benefit, or usefulness that a service, product, or activity delivers to stakeholders, especially customers and the business.
Key term
Shell
A shell is a computer program that provides a user interface to access an operating system's services, typically by accepting text commands.
About these practice questions
Courseiva writes every XK0-006 question from scratch — 781 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This XK0-006 practice question is part of Courseiva's free CompTIA certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the XK0-006 exam.