Environment variables in are key-value pairs that you can configure for your Bnodes. They are accessible within your containerized application and provide a flexible way to pass configuration settings, secrets, and runtime information to your application without hardcoding them into your code or container image.
What are environment variables?
Environment variables are dynamic values that exist in your Bnode’s operating system environment. They act as a bridge between your Bnode’s configuration and your running applications, allowing you to:
- Store configuration settings that can change between deployments.
- Pass sensitive information like API keys securely.
- Access Bnode metadata and system information.
- Configure application behavior without modifying code.
- Reference Brightnode secrets in your containers.
When you set an environment variable in your Bnode configuration, it becomes available to all processes running inside that Bnode’s container.
Why use environment variables in Bnodes?
Environment variables offer several key benefits for containerized applications:
Configuration flexibility: Environment variables allow you to easily change application settings without modifying your code or rebuilding your container image. For example, you can set different model names, API endpoints, or processing parameters for different deployments:
# Set a model name that your application can read
MODEL_NAME=llama-2-7b-chat
API_ENDPOINT=https://api.example.com/v1
MAX_BATCH_SIZE=32
Security: Sensitive information such as API keys, database passwords, or authentication tokens can be injected as environment variables, keeping them out of your codebase and container images. This prevents accidental exposure in version control or public repositories.
Bnode metadata access: Brightnode provides predefined environment variables that give your application information about the Bnode’s environment, resources, and network configuration. This metadata helps your application adapt to its runtime environment automatically.
Automation and scaling: Environment variables make it easier to automate deployments and scale applications. You can use the same container image with different settings for development, staging, and production environments by simply changing the environment variables.
Setting environment variables
You can configure up to 50 environment variables per Bnode through the Brightnode interface when creating or editing a Bnode or Bnode template.
During Bnode creation
- When creating a new Bnode, click Edit Template and expand the Environment Variables section.
- Click Add Environment Variable.
- Enter the Key (variable name) and Value.
- Repeat for additional variables.
In Bnode templates
- Navigate to My Templates in the console.
- Create a new template or edit an existing one.
- Add environment variables in the Environment Variables section.
- Save the template for reuse across multiple Bnodes.
Using secrets
For sensitive data, you can reference Brightnode secrets in environment variables using the BRIGHTNODE_SECRET_ prefix. For example:
API_KEY={{ BRIGHTNODE_SECRET_my_api_key }}
DATABASE_PASSWORD={{ BRIGHTNODE_SECRET_db_password }}
Updating environment variables
To update environment variables in your Bnode:
- Navigate to the Bnodes section of the console.
- Click the three dots to the right of the Bnode you want to update and select Edit Bnode.
- Click the Environment Variables section to expand it.
- Add or update the environment variables.
- Click Save to save your changes.
When you update environment variables your Bnode will restart, clearing all data outside of your volume mount path (/workspace by default).
Accessing environment variables
Once set, environment variables are available to your application through standard operating system mechanisms.
Verify variables in your Bnode
You can check if environment variables are properly set by running commands in your Bnode’s terminal:
# View a specific environment variable
echo $ENVIRONMENT_VARIABLE_KEY
# List all environment variables
env
# Search for specific variables
env | grep BRIGHTNODE
Accessing variables in your applications
Different programming languages provide various ways to access environment variables:
Python:
import os
model_name = os.environ.get('MODEL_NAME', 'default-model')
api_key = os.environ['API_KEY'] # Raises error if not found
Bnode.js:
const modelName = process.env.MODEL_NAME || 'default-model';
const apiKey = process.env.API_KEY;
Bash scripts:
#!/bin/bash
MODEL_NAME=${MODEL_NAME:-"default-model"}
echo "Using model: $MODEL_NAME"
Brightnode-provided environment variables
Brightnode automatically sets several environment variables that provide information about your Bnode’s environment and resources:
| Variable | Description |
|---|
BRIGHTNODE_POD_ID | The unique identifier assigned to your Bnode. |
BRIGHTNODE_DC_ID | The identifier of the data center where your Bnode is located. |
BRIGHTNODE_POD_HOSTNAME | The hostname of the server where your Bnode is running. |
BRIGHTNODE_GPU_COUNT | The total number of GPUs available to your Bnode. |
BRIGHTNODE_CPU_COUNT | The total number of CPUs available to your Bnode. |
BRIGHTNODE_PUBLIC_IP | The publicly accessible IP address for your Bnode, if available. |
BRIGHTNODE_TCP_PORT_22 | The public port mapped to SSH (port 22) for your Bnode. |
BRIGHTNODE_ALLOW_IP | A comma-separated list of IP addresses or ranges allowed to access your Bnode. |
BRIGHTNODE_VOLUME_ID | The ID of the network volume attached to your Bnode. |
BRIGHTNODE_API_KEY | The API key for making Brightnode API calls scoped specifically to this Bnode. |
PUBLIC_KEY | The SSH public keys authorized to access your Bnode over SSH. |
CUDA_VERSION | The version of CUDA installed in your Bnode environment. |
PYTORCH_VERSION | The version of PyTorch installed in your Bnode environment. |
PWD | The current working directory inside your Bnode. |
Common use cases
Environment variables are particularly useful for:
Model configuration: Configure which AI models to load without rebuilding your container:
MODEL_NAME=gpt-3.5-turbo
MODEL_PATH=/workspace/models
MAX_TOKENS=2048
TEMPERATURE=0.7
Service configuration: Set up web services and APIs with flexible configuration:
API_PORT=8000
DEBUG_MODE=false
LOG_LEVEL=INFO
CORS_ORIGINS=https://myapp.com,https://staging.myapp.com
Database and external service connections: Connect to databases and external APIs securely:
DATABASE_URL=postgresql://user:pass@host:5432/db
REDIS_URL=redis://localhost:6379
API_BASE_URL=https://api.external-service.com
Development vs. production settings: Use different configurations for different environments:
ENVIRONMENT=production
CACHE_ENABLED=true
RATE_LIMIT=1000
MONITORING_ENABLED=true
Port management: When configuring symmetrical ports, your application can discover assigned ports through environment variables. This is particularly useful for services that need to know their external port numbers.
For more details, see Expose ports.
Best practices
Follow these guidelines when working with environment variables:
Security considerations:
- Never hardcode secrets: Use Brightnode secrets for sensitive data.
- Use descriptive names: Choose clear, descriptive variable names like
DATABASE_PASSWORD instead of DB_PASS.
Configuration management:
- Provide defaults: Use default values for non-critical configuration options.
- Document your variables: Maintain clear documentation of what each environment variable does.
- Group related variables: Use consistent prefixes for related configuration (for example,
DB_HOST, DB_PORT, DB_NAME).
Application design:
- Validate required variables. Check that critical environment variables are set before your application starts. If the variable is missing, your application should throw an error or return a clear message indicating which variable is not set. This helps prevent unexpected failures and makes debugging easier.
- Type conversion: Convert string environment variables to appropriate types (such as integers or booleans) in your application.
- Configuration validation: Validate environment variable values to catch configuration errors early.