Setting Environment Variables Using Docker
When running a Docker container, you can set environment variables using the-e flag. For example, the command below sets the APP_COLOR environment variable to “pink”:
APP_COLOR while launching the simple-webapp-color container.
Configuring Environment Variables in Kubernetes Pods
Kubernetes allows you to define environment variables within your pod definitions. In the pod manifest, environment variables are listed under theenv property, which is an array. Each entry in the array should specify:
name: The name of the environment variable.value: The corresponding value assigned to that environment variable.
simple-webapp-color image will have APP_COLOR set to “pink”.
Leveraging ConfigMaps and Secrets for Environment Variables
Instead of hardcoding values into your pod manifest, you can enhance flexibility and security by referencing external configuration sources such as ConfigMaps or Secrets. This approach simplifies maintenance and helps protect sensitive information. To define an environment variable directly, use:APP_COLOR from the specified external resource.
Using ConfigMaps and Secrets promotes better security practices and easier management of configuration drift. Ensure these objects are updated consistently with your application requirements.
Avoid hardcoding sensitive data directly into your manifests. Always use Secrets when dealing with sensitive information such as passwords or API keys.
Summary
In this article, we covered:- How to set environment variables using a Docker command.
- Defining environment variables within Kubernetes pod definitions.
- The benefits of leveraging ConfigMaps and Secrets for managing environment configurations.