> ## Documentation Index
> Fetch the complete documentation index at: https://r2r-patch-decruft-openapi-json.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Environment

# R2R Troubleshooting Guide: Missing Environment Variables

When deploying R2R, missing environment variables can cause containers to fail to start or lead to unexpected behavior. This guide will help you identify and resolve issues related to missing environment variables.

## 1. Identifying the Problem

Signs that you might be dealing with missing environment variables:

* Containers exit immediately after starting
* Error messages in logs mentioning undefined or null values
* Specific features or integrations not working as expected

## 2. Common Missing Environment Variables

Here are some critical environment variables for R2R:

* Database credentials (e.g., `POSTGRES_USER`, `POSTGRES_PASSWORD`)
* API keys (e.g., `OPENAI_API_KEY`, `ANTHROPIC_API_KEY`)
* Configuration settings (e.g., `CONFIG_NAME`, `CONFIG_PATH`)

## 3. Checking for Missing Variables

### 3.1 Review Docker Compose File

1. Open your `docker-compose.yml` file.
2. Look for the `environment` section under the `r2r` service.
3. Ensure all required variables are listed.

Example:

```yaml
services:
  r2r:
    environment:
      - POSTGRES_USER=${POSTGRES_USER:-postgres}
      - OPENAI_API_KEY=${OPENAI_API_KEY:-}
      - CONFIG_NAME=${CONFIG_NAME:-}
      # ... other variables
```

### 3.2 Check .env File

1. Ensure you have a `.env` file in the same directory as your `docker-compose.yml`.
2. Verify that all variables used in `docker-compose.yml` are defined in `.env`.

Example `.env` file:

```
POSTGRES_USER=myuser
OPENAI_API_KEY=sk-...
CONFIG_NAME=default
```

### 3.3 Verify Environment in Running Container

If the container starts but behaves unexpectedly:

1. Access the container's shell:
   ```
   docker exec -it r2r-container-name /bin/bash
   ```
2. Check environment variables:
   ```
   env | grep VARIABLE_NAME
   ```

## 4. Resolving Missing Variables

### 4.1 Update .env File

1. Add any missing variables to your `.env` file.
2. Ensure values are correct and properly formatted.

### 4.2 Use Default Values

In `docker-compose.yml`, provide default values for non-sensitive variables:

```yaml
environment:
  - VARIABLE_NAME=${VARIABLE_NAME:-default_value}
```

### 4.3 Inject Variables at Runtime

For sensitive data, inject variables when running the container:

```bash
docker run -e SENSITIVE_VAR=value ...
```

Or with docker-compose:

```bash
SENSITIVE_VAR=value docker-compose up
```

### 4.4 Use Docker Secrets

For enhanced security, consider using Docker secrets for sensitive data:

1. Create a secret:
   ```
   echo "mysecretvalue" | docker secret create my_secret -
   ```

2. Use in `docker-compose.yml`:
   ```yaml
   secrets:
     - my_secret
   ```

## 5. Specific R2R Environment Variables

Ensure these key R2R variables are set:

* `CONFIG_NAME` or `CONFIG_PATH`: Specifies which configuration to use.
* `POSTGRES_*`: Database connection details.
* `OPENAI_API_KEY`: If using OpenAI services.
* `ANTHROPIC_API_KEY`: If using Anthropic models.
* `OLLAMA_API_BASE`: For local LLM integration.
* `HATCHET_CLIENT_TOKEN`: For Hatchet integration.

## 6. Debugging Tips

* Use `docker-compose config` to see the final composed configuration with resolved variables.
* Temporarily echo sensitive variables in your container's entrypoint script for debugging (remove in production).
* Check for typos in variable names both in `docker-compose.yml` and `.env` files.

## 7. Best Practices

* Use version control for your `docker-compose.yml`, but not for `.env` files containing secrets.
* Consider using a secret management service for production deployments.
* Document all required environment variables in your project's README.
* Use CI/CD pipelines to validate the presence of required variables before deployment.

By following this guide, you should be able to identify and resolve issues related to missing environment variables in your R2R deployment. Remember to always handle sensitive data securely and never commit secrets to version control.
