Learn how to use environment variables in Runpod Pods for configuration, security, and automation
Environment variables are key-value pairs that you can configure for your . 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.
Environment variables are dynamic values that exist in your Pod’s operating system environment. They act as a bridge between your Pod’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 Pod metadata and system information.
Configure application behavior without modifying code.
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:
Copy
# Set a model name that your application can readMODEL_NAME=llama-2-7b-chatAPI_ENDPOINT=https://api.example.com/v1MAX_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.Pod metadata access: Runpod provides predefined environment variables that give your application information about the Pod’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.
You can check if environment variables are properly set by running commands in your Pod’s terminal:
Copy
# View a specific environment variableecho $ENVIRONMENT_VARIABLE_KEY# List all environment variablesenv# Search for specific variablesenv | grep RUNPOD
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.
Follow these guidelines when working with environment variables:Security considerations:
Never hardcode secrets: Use Runpod 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.