Environment variables are a common way to configure applications at runtime. Unikraft Cloud supports passing environment variables to instances during deployment through many methods. This tutorial discusses each below and orders them by superseeding precedence (from most powerful to weakest).
Setting environment variables
Below are all the ways you can set environment variables for your Unikraft Cloud instances. The order they're presented is from the least precedence to the highest precedence.
Dockerfile ENV
Disclaimer
At the moment environment variables from the Dockerfile are passed in the image but not read by the Unikraft Cloud platform.
Please use the other methods described below in the meantime.
You can set environment variables in your Dockerfile using the ENV instruction in your target FROM stage.
The kraft CLI automatically reads them when building the image and sets them in the resulting packaged image.
Code
Kraftfile env
Disclaimer
At the moment environment variables from the Kraftfile are passed in the image but not read by the Unikraft Cloud platform.
Nonetheless, kraft cloud deploy can still read them and set them in the instance.
For other use cases, please use the other methods described below in the meantime.
You can set environment variables in your Kraftfile using the env field.
kraft automatically reads them when building the image and sets them in the resulting packaged image.
Code
This method takes precedence over the Dockerfile.
It will override environment variables with the same name set in the Dockerfile.
Kraft flags
When deploying an instance using the kraft cloud deploy or kraft cloud instance create commands, you can pass environment variables using the --env (or -e) flag.
You can specify many environment variables by using the flag many times.
The flag works similarly to the docker run -e flag.
Code
This method takes precedence over the Dockerfile and Kraftfile.
It will override environment variables with the same name set in the previous two methods.
Wrapper script
Create a wrapper script that sets the environment variables before executing the main app.
Check what the original entrypoint of your app is and call it after setting the variables.
For example, if the original entrypoint is /entrypoint.sh, you can create a new script wrapper.sh like this:
Code
Then you change either your Dockerfile or Kraftfile to use this wrapper script as the entrypoint:
Code
This method has the highest precedence and will override environment variables set in the cli, Dockerfile, or Kraftfile as it hardcodes them in the script.
Thus it's recommended to do this only for variables that you set and don't change.
Conclusion
Out of the four ways to set environment variables in Unikraft Cloud instances you should pick the ones that work best for you.
Consider that the Dockerfile and Kraftfile can set variables for the image itself, while the kraft CLI and wrapper script set them at runtime.
Learn more
- The
kraft cloudcommand-line tool reference, and in particular the deploy subcommand. - The Dockerfile reference for understanding how to set environment variables in Dockerfiles.
- The Kraftfile reference for understanding how to set environment variables in Kraftfiles.