Environment variables
In Divio, environment variables provide a powerful and flexible way to manage configuration information in your applications in the cloud and your local machine.
This functionality allows you to store sensitive information, such as passwords and API keys, outside your codebase and keep it secure.
Introduction
In Divio cloud applications, environment variables define and configure specific settings or parameters for an application's environment.
When an application is provisioned, environment variables specific to the environment are also automatically provisioned. Additionally, when a service is provisioned, such as a database, or an object storage, corresponding environment variables are also provisioned. Refer to the environment variables reference for a list of BUILT-IN environment variables.
Environment variables are displayed on the Env Variables view.
After adding or changing environment variables, the respective environment must be redeployed to apply the configuration.
Types
Your application relies on environment variables to configure its settings and access storage, databases, and other services.
Environment variables can be set independently for each environment, either in the cloud or locally. They can also be briefly categorized as:
- Built-in environment variables are automatically set by our infrastructure.
- Custom environment variables are user-configurable and can also be used to overwrite environment variables of the previous category (built-in).
We do not support the use of environment variables in the Dockerfile
, as they are not available at build time due to security reasons. The ENV
statement can be used for some configuration, but secrets should only be read by the application at runtime. This follows the Twelve-Factor principles.
Usage
It's important to note that environment variables should only apply to environments, not states or processes independent of a particular environment.
Your application should not be subject to any particular environment conditions during the build phase.
Here are some examples of how to integrate environment variables into your application:
- JavaScript
- Python
- PHP
- Java
- ASP.NET
- Ruby
- Go
// access environment variables
const DATABASE_URL = process.env.DATABASE_URL;
const SECRET_KEY = process.env.SECRET_KEY;
const DEBUG = process.env.DEBUG === 'True';
# access environment variables
DATABASE_URL = os.environ.get('DATABASE_URL')
SECRET_KEY = os.environ.get('SECRET_KEY')
DEBUG = os.environ.get('DEBUG', 'False') == 'True'
// access environment variables
$DATABASE_URL = getenv('DATABASE_URL');
$SECRET_KEY = getenv('SECRET_KEY');
$DEBUG = getenv('DEBUG') === 'True';
// access environment variables
String DATABASE_URL = System.getenv("DATABASE_URL");
String SECRET_KEY = System.getenv("SECRET_KEY");
boolean DEBUG = Boolean.parseBoolean(System.getenv("DEBUG"));
// access environment variables
string DATABASE_URL = Environment.GetEnvironmentVariable("DATABASE_URL");
string SECRET_KEY = Environment.GetEnvironmentVariable("SECRET_KEY");
bool DEBUG = bool.Parse(Environment.GetEnvironmentVariable("DEBUG") ?? "False");
# access environment variables
DATABASE_URL = ENV["DATABASE_URL"]
SECRET_KEY = ENV["SECRET_KEY"]
DEBUG = ENV["DEBUG"] == "True"
// access environment variables
DATABASE_URL := os.Getenv("DATABASE_URL")
SECRET_KEY := os.Getenv("SECRET_KEY")
DEBUG, _ := strconv.ParseBool(os.Getenv("DEBUG"))
Built-in variables
Built-in variables are automatically provided to each environment. They can be overridden by custom variables with varying degrees of sensitivity, such as hidden-valued environment variables.
Some of the most note-worthy built-in environment variables are:
DATABASE_URL
,<prefix>_DATABASE_DSN
: Each database provisioned in Services will have a corresponding environment variable, for exampleDEFAULT_DATABASE_DSN
(depending on the prefix applied - the default prefix is alwaysDEFAULT
). TheDEFAULT_DATABASE_DSN
is also exposed asDATABASE_URL
DOMAIN
: The primary domain of the environment's server.DOMAIN_ALIASES
: Other domains for the environment, separated by commas.GIT_BRANCH
: The cloud environment's Git branch.SECRET_KEY
: A generated random key that your application can use as a unique identifier for internal security purposes.STAGE
: The name of the environment (test
,live
, etc).<prefix>_STORAGE_DSN
: Each object storage provisioned in Services will have a corresponding environment variable, for exampleDEFAULT_STORAGE_DSN
(depending on the prefix applied - the default prefix is alwaysDEFAULT
).
Accessing environment variables
You can access the values of any environment variable by SSHing into the container of the environment and echoing the value. Depending on your Docker configuration this might look something like this:
echo $DATABASE_URL
You can find the SSH command on the dashboard of your application.
Local environment
In addition to cloud applications, your local application also uses environment variables, which can be configured in a file called .env-local
specified in the docker-compose.yml
file.
To adapt your docker-compose.yml
file to use environment variables, you can use the following example:
services:
web:
environment:
- DATABASE_URL=${DATABASE_URL}
- SECRET_KEY=${SECRET_KEY}
- DEBUG=${DEBUG}
or use a .env-local
file:
services:
web:
env_file:
- .env-local
and within the .env-local
file:
DATABASE_URL=postgres://user:password@localhost:5432/dbname
SECRET_KEY=your_secret_key
DEBUG=True