Junior — Middle
How can variable values be passed to a template during its creation?
sobes.tech AI
Answer from AI
When creating an image (for example, a Docker image), variable values can be passed in several ways:
-
Via build arguments
- Using the
ARGdirective in the Dockerfile and the--build-argparameter during build. - Allows passing values that are only available during the image build.
Example Dockerfile:
ARG APP_VERSION=1.0 FROM python:3.9 RUN echo "Application version: $APP_VERSION"Build command:
- Using the
docker build --build-arg APP_VERSION=2.0 -t myapp:2.0 .
2. **Via environment variables**
- Using the `ENV` directive in the Dockerfile to set variables that will be available in the container at runtime.
Example:
```Dockerfile
ENV APP_ENV=production
- Via configuration files or templates
- Using template engines or scripts that substitute variable values into configuration files before building the image.
Thus, to pass variable values into an image during its creation, ARG and --build-arg are most commonly used in Docker.