build_image() builds a container image from a Dockerfile using either
podman or docker. It auto-detects which tool is available on the system
unless tool is specified explicitly. Use dry_run = TRUE to preview the
exact command that would be run without executing it.
Usage
build_image(
dockerfile = "Dockerfile",
tag = NULL,
platform = "linux/amd64",
tool = NULL,
dry_run = FALSE,
verbose = FALSE,
comments = FALSE
)Arguments
- dockerfile
A character string. Path to the
Dockerfileto build from. Defaults to"Dockerfile"in the current working directory.- tag
A character string or
NULL. The full image tag to assign to the built image, including the registry prefix, e.g."registry.doit.wisc.edu/netid/myimage". IfNULL, no tag is applied and the image is identified only by its image ID. Defaults toNULL.- platform
A character string or
NULL. The target platform for the container image. Defaults to"linux/amd64", which is the architecture used by most HPC and HTC clusters. Set to"linux/arm64"for ARM-based systems (Apple Silicon, AWS Graviton). Set toNULLto let the container tool build for the host architecture.- tool
A character string or
NULL. The container tool to use for building. One of"podman"or"docker". IfNULL(the default), the function auto-detects which tool is available, preferringpodmanif both are found.- dry_run
Logical. If
TRUE, prints the command that would be run without executing it. Useful for verifying the command before committing to a potentially slow build. Defaults toFALSE.- verbose
Logical. If
TRUE, prints progress messages at each step. Defaults toFALSE.- comments
Logical. If
TRUE, prints explanatory context before each step – what the command does, why it is needed, and common pitfalls. Useful for first-time users learning the container build workflow. Defaults toFALSE.
Details
When the target platform differs from the host architecture (e.g.
building linux/amd64 on an Apple Silicon Mac), build_image()
automatically uses docker buildx build instead of docker build, and
includes --load to ensure the image is available in the local store.
For podman, --platform is passed directly to podman build.
Prerequisites
Before calling build_image(), ensure the following are in place:
A
Dockerfileexists atdockerfile. Usegenerate_dockerfile()to create one if needed.An
renv.lockfile is present in the build context – the generatedDockerfileuses it to restore the R package environment inside the container.Either
podmanordockeris installed and the daemon (fordocker) or the Podman service is running. Verify withpodman infoordocker infoin a terminal.
Cross-platform builds
Building for a different architecture than the host requires emulation.
On Apple Silicon Macs, building linux/amd64 images uses QEMU emulation
under Podman, which can be slow and unstable. Docker Desktop handles
cross-platform builds more reliably via buildx and Rosetta 2.
If builds fail with QEMU segfaults, consider:
Using Docker Desktop instead of Podman (
tool = "docker")Building on a native x86_64 machine (e.g. via GitHub Actions)
Building directly on the target cluster if it supports container builds
Tagging convention for CHTC
For UW-Madison CHTC, the full tag format is:
registry.doit.wisc.edu/<netid>/<image-name>:<version>
For example: registry.doit.wisc.edu/erwin.lares/my-analysis:1.0.0
The version tag defaults to latest if omitted. Using explicit version
tags (e.g. 1.0.0) is recommended for reproducibility – latest will
be overwritten each time you push.
Examples
if (FALSE) { # \dontrun{
# Build for linux/amd64 (default) with auto-detected tool
build_image()
# Build and tag for CHTC registry
build_image(tag = "registry.doit.wisc.edu/netid/my-analysis:1.0.0")
# Build for the host architecture (no --platform flag)
build_image(platform = NULL)
# Build for ARM64 (e.g. local use on Apple Silicon)
build_image(platform = "linux/arm64")
# Preview the build command without running it
build_image(
tag = "registry.doit.wisc.edu/netid/my-analysis:1.0.0",
dry_run = TRUE
)
# Guided build for first-time users
build_image(
tag = "registry.doit.wisc.edu/netid/my-analysis:1.0.0",
verbose = TRUE,
comments = TRUE
)
} # }
