ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Dockerfile Build Args for Dynamic Configuration

Dockerfile

Goal -- WPM

Ready
Exercise Algorithm Area
1ARG APP_ENV=production
2ARG API_URL=https://api.example.com
3ARG LOG_LEVEL=info
4
5FROM alpine:latest
6
7# Install necessary tools (e.g., for configuration processing if needed)
8RUN apk update && apk add --no-cache bash curl
9
10# Create a script to set environment variables based on ARGs
11RUN echo "export APP_ENV=${APP_ENV}" > /etc/app_env.sh && \
12echo "export API_URL=${API_URL}" >> /etc/app_env.sh && \
13echo "export LOG_LEVEL=${LOG_LEVEL}" >> /etc/app_env.sh
14
15# Source the script to make variables available in the current shell context
16# Note: This only affects the build process. For runtime, ENV is needed.
17RUN . /etc/app_env.sh && echo "Build-time API URL: $API_URL"
18
19# Set runtime environment variables from build arguments
20ENV APP_ENV=${APP_ENV}
21ENV API_URL=${API_URL}
22ENV LOG_LEVEL=${LOG_LEVEL}
23
24# Copy application code (assuming a simple script for demonstration)
25COPY app.sh /app/app.sh
26RUN chmod +x /app/app.sh
27
28# Command to run the application, which will use the runtime ENV vars
29CMD ["/app/app.sh"]
Algorithm description viewbox

Dockerfile Build Args for Dynamic Configuration

Algorithm description:

This Dockerfile demonstrates how to use build arguments (`ARG`) to inject dynamic configuration into an application during the Docker build process. It defines several `ARG`s with default values and then uses them to set runtime `ENV` variables. This allows for flexible customization of the application's behavior (e.g., environment, API endpoints, logging levels) without modifying the source code or the Dockerfile itself for each configuration.

Algorithm explanation:

Build arguments (`ARG`) are variables that can be passed to a Docker build. They are only available during the build process and can be used to parameterize Dockerfile instructions. In this example, `APP_ENV`, `API_URL`, and `LOG_LEVEL` are defined as `ARG`s with default values. These `ARG`s are then used to set runtime environment variables (`ENV`). This is a common pattern for configuring applications. The `RUN` command demonstrates how `ARG`s can be used during the build itself (e.g., to generate configuration files or scripts). The `ENV` instruction makes these variables available to the running container. This approach provides a powerful way to customize builds for different environments (development, staging, production) or different deployments without maintaining multiple Dockerfiles. It's crucial to distinguish between `ARG` (build-time only) and `ENV` (build-time and runtime).

Pseudocode:

Define build arguments with default values.
Set base image.
Install any necessary build tools.
Create a script to set environment variables from build arguments.
Use build arguments to set runtime environment variables.
Copy application code.
Set execute permissions for application script.
Define the command to run the application using runtime environment variables.