Using Build Arguments (ARG) and Environment Variables (ENV) in Dockerfiles
Owner: SnippetBot
Created: 2026-08-24 00:00:15
Size: 0.77 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
FROM ubuntu:20.04
# ARG: Build-time variable, not available in the running container by default
ARG APP_VERSION=1.0.0
ARG BUILD_DATE="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
# ENV: Runtime environment variable, also available during build after its declaration
ENV PORT=8080 \
DEBUG_MODE=false
LABEL version="$APP_VERSION" \
build_date="$BUILD_DATE"
# Example of using ARG during build
RUN echo "Building application version $APP_VERSION on $BUILD_DATE"
# Example of using ENV after declaration
RUN echo "Application will run on port $PORT"
# You can pass build args with --build-arg during `docker build`
# Example: docker build --build-arg APP_VERSION=1.1.0 -t myapp .
# You can override ENV vars at runtime with -e during `docker run`
# Example: docker run -e PORT=3000 myapp