Skip to contents

Adds an ARG instruction to define a variable that users can pass at build-time to the builder using the --build-arg flag.

Usage

dfi_arg(dockerfile, name, default = NULL)

Arguments

dockerfile

A dockerfile object

name

Argument name

default

Default value (optional)

Value

An updated dockerfile object with the ARG instruction added

Details

Build arguments are only available during the build of a Docker image and not when a container is running. They can be used to parameterize the build process, allowing users to specify values like versions or configuration options at build time.

When building the image, use:

docker build --build-arg R_VERSION=4.4.0 -t my-image .

See also

dfi_env() for runtime environment variables & Official Docker ARG documentation

Other dockerfile instruction functions: dfi_add(), dfi_cmd(), dfi_copy(), dfi_entrypoint(), dfi_env(), dfi_expose(), dfi_from(), dfi_healthcheck(), dfi_label(), dfi_maintainer(), dfi_onbuild(), dfi_run(), dfi_shell(), dfi_stopsignal(), dfi_user(), dfi_volume(), dfi_workdir()

Examples

# Define an argument with no default
df <- dockerfile() |>
  dfi_arg("R_VERSION") |>
  dfi_from(paste0("rocker/r-ver:", "$R_VERSION"))
df
#> ARG R_VERSION
#> FROM rocker/r-ver:$R_VERSION 
   
# Define an argument with a default value
df <- dockerfile() |>
  dfi_arg("R_VERSION", "4.4.0") |>
  dfi_from(paste0("rocker/r-ver:", "$R_VERSION"))
df
#> ARG R_VERSION=4.4.0
#> FROM rocker/r-ver:$R_VERSION