Environment variables are a common way to configure various buildpacks at build-time.
Below are a few ways you can do so. All of them use the samples repository for
simplicity. The following documentation assumes that all participating buildpack
either use clear-env = false as the default in their
buildpack.toml,
or if they use clear-env = true, that they merge in filtered user supplied
environment variables.
--env)The --env parameter must be one of the following:
VARIABLE=VALUEVARIABLE, where the value of VARIABLE will be taken from the local environmentexport FOO=BAR
pack build sample-app \
--env "HELLO=WORLD" \
--env "FOO" \
--builder cnbs/sample-builder:noble \
--buildpack samples/buildpacks/hello-world/ \
--buildpack samples/apps/bash-script/bash-script-buildpack/ \
--path samples/apps/bash-script/
docker run sample-app
The following environment variables were set and available to buildpacks at build-time:
| Name | Value | Source |
|---|---|---|
HELLO |
WORLD |
hard-coded inline value |
FOO |
BAR |
current environment |
--env-file)The --env-file parameter must be a path to a file where each line is one of the following:
VARIABLE=VALUEVARIABLE, where the value of VARIABLE will be taken from the current environmentexport FOO=BAR
echo -en "HELLO=WORLD\nFOO" > ./envfile
pack build sample-app \
--env-file ./envfile \
--builder cnbs/sample-builder:noble \
--buildpack samples/buildpacks/hello-world/ \
--buildpack samples/apps/bash-script/bash-script-buildpack/ \
--path samples/apps/bash-script/
docker run sample-app
The following environment variables were set and available to buildpacks at build-time:
| Name | Value | Source |
|---|---|---|
HELLO |
WORLD |
hard-coded value in file |
FOO |
BAR |
current environment |
NOTE: Variables defined using
--envtake precedence over variables defined in--env-file.
The --descriptor parameter must be a path to a file which follows the project.toml schema.
Without the --descriptor flag, pack build will use the project.toml file in the application directory if it exists.
You can define environment variables in an env table in the file, and pass those into the application.
cat >> samples/apps/bash-script/project.toml <<EOL
[[io.buildpacks.build.env]]
name="HELLO"
value="WORLD"
EOL
pack build sample-app \
--builder cnbs/sample-builder:noble \
--buildpack samples/buildpacks/hello-world/ \
--buildpack samples/apps/bash-script/bash-script-buildpack/ \
--path samples/apps/bash-script/
docker run sample-app
The following environment variables were set and available to buildpacks at build-time:
| Name | Value | Source |
|---|---|---|
HELLO |
WORLD |
hard-coded value in file |
NOTE: Variables defined using
--envor--env-filetake precedence over variables defined in theproject.toml.
NOTE:
project.tomlcan’t detect environment variables (so, for instance, if one ranexport FOO=BARand addedname=FOOto theproject.toml, it wouldn’t detect any value set forFOO).