Skip to contents

Adds a HEALTHCHECK instruction to tell Docker how to test if a container is still working properly.

Usage

dfi_healthcheck(
  dockerfile,
  command,
  interval = NULL,
  timeout = NULL,
  start_period = NULL,
  retries = NULL
)

Arguments

dockerfile

A dockerfile object

command

Health check command

interval

Interval between checks (e.g., "30s")

timeout

Timeout for checks (e.g., "5s")

start_period

Initial grace period (e.g., "5s")

retries

Number of retries

Value

An updated dockerfile object with the HEALTHCHECK instruction added

Details

The HEALTHCHECK instruction tells Docker how to determine if a container is healthy. The command should exit with 0 if the container is healthy, or with 1 if it's unhealthy. This is particularly useful for web applications or services where you want to ensure they're running correctly.

Parameter meanings:

  • interval: How often to run the check (default: 30s)

  • timeout: Maximum time the check can take (default: 30s)

  • start-period: Grace period before counting retries (default: 0s)

  • retries: Number of consecutive failures needed to mark unhealthy (default: 3)

See also

dk_template_shiny() for a template for Shiny apps, dk_template_plumber() for a template for Plumber APIs, & Official Docker HEALTHCHECK documentation

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

Examples

# Simple health check using curl
df <- dockerfile() |>
  dfi_from("rocker/shiny:4.4.0") |>
  dfi_healthcheck("curl -f http://localhost:3838/ || exit 1",
                   interval = "30s",
                   timeout = "10s",
                   retries = 3)
df
#> FROM rocker/shiny:4.4.0
#> HEALTHCHECK --interval=30s --timeout=10s --retries=3 CMD curl -f http://localhost:3838/ || exit 1