Environment Variables
Updated Mar 11, 2022 ·
Overview
Environment variables allow us to use configurable values in our code without modifying the code itself. This flexibility is beneficial for changing settings without redeploying applications.
Ways to Define Environment Variables
-
Dockerfile
- Use the
ENV
directive to set variables. - Automatically available in the container.
- Use the
-
Environment File
- Store variables in a
.env
file. - Load them when starting a container.
- Store variables in a
-
Shell Environment Variables
- Set variables in the terminal session.
- Pass them to the container at runtime.
-
Compose File
- Define variables in
docker-compose.yml
. - Simplifies configuration for multi-container applications.
- Define variables in