> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Env file

> Explains Telepresence env-file feature that captures a container's environment and writes it to a local file so local processes run with identical environment variables

In this lesson we'll cover Telepresence's built-in env-file feature: how it captures the environment from a container in-cluster and writes it to a local file so your locally run process can use the same environment as the intercepted service.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/Sd_r7JaRKfit4jl0/images/Telepresence-for-Kubernetes/Telepresence-For-Kubernetes/Env-file/env-file-blue-teal-slide.jpg?fit=max&auto=format&n=Sd_r7JaRKfit4jl0&q=85&s=1e064869ad90ef40e52113dbff10143f" alt="A blue-to-teal gradient slide with the white text &#x22;Env file&#x22; centered. A small &#x22;© Copyright KodeKloud&#x22; label appears in the bottom-left corner." width="1920" height="1080" data-path="images/Telepresence-for-Kubernetes/Telepresence-For-Kubernetes/Env-file/env-file-blue-teal-slide.jpg" />
</Frame>

What happens when you intercept a Kubernetes service with Telepresence? The `telepresence intercept` command routes the service traffic to a process running on your local machine. For example, intercepting a "products" deployment and forwarding port 8000 on your machine to port 3000 in the cluster looks like this:

```bash theme={null}
telepresence intercept products-depl --port 8000:3000
```

If the original in-cluster container requires environment variables (for example, an `API_URL` or DB connection string defined in the deployment spec), your local process typically needs the same variables to behave identically. To simplify this, Telepresence can export the container’s environment into a local file using the `--env-file` flag.

Example: create a `.env` file that matches the container environment and run the intercept:

```bash theme={null}
telepresence intercept products-depl --port 8000:3000 --env-file .env
```

When you pass `--env-file .env`, Telepresence writes the container's environment to the named file (for example `.env`) using the KEY=VALUE format. You can then source or otherwise load that file to run your local process with the same environment.

<Callout icon="lightbulb" color="#1CB2FE">
  Telepresence will create a local file with the exact name you specify containing the container's environment variables. Source or consume that file so your local process runs with the same environment as the in-cluster container.
</Callout>

Usage patterns: how to consume the generated env file

* Shell (bash/zsh): source the file so environment variables are exported into the current shell session.
  ```bash theme={null}
  source .env
  python app.py
  ```
* Export all variables in one line (useful in some CI/test scripts):
  ```bash theme={null}
  export $(grep -v '^#' .env | xargs)
  node server.js
  ```
* Node.js (dotenv): install and use the `dotenv` package to load `.env` into `process.env`.
  ```bash theme={null}
  npm install dotenv
  node -r dotenv/config server.js
  ```
* Python (python-dotenv):
  ```bash theme={null}
  pip install python-dotenv
  python -m dotenv run -- python app.py
  ```
* Docker CLI (when launching a container locally that needs the same environment):
  ```bash theme={null}
  docker run --env-file .env my-image
  ```

Quick reference table

| Purpose | Example |
| - | - |
| Intercept and write env file | `telepresence intercept products-depl --port 8000:3000 --env-file .env` |
| Source the env file in current shell | `source .env` |
| Export and run a command with variables | `export $(grep -v '^#' .env \| xargs) && ./run-local.sh` |
| Use with Docker CLI | `docker run --env-file .env my-image` |
| Use with Node (dotenv) | `node -r dotenv/config server.js` |

Security note

<Callout icon="warning" color="#FF6B6B">
  The generated env file may contain sensitive values (API keys, DB credentials, tokens). Treat it like a secret: do not commit it to version control, avoid logging it, and delete it when you no longer need it.
</Callout>

Examples and considerations

* If your deployment defines environment variables via `env` or `envFrom` in the pod spec, those values will be captured. For example, a deployment snippet might include:
  ```yaml theme={null}
  env:
    - name: API_URL
      value: "https://api.example.internal"
    - name: DB_HOST
      valueFrom:
        secretKeyRef:
          name: db-credentials
          key: host
  ```
  Telepresence will resolve the container's final environment and write it to the file.
* If values are injected at runtime by an init container or an entrypoint script, ensure those values exist in the running container environment before creating the intercept, otherwise they won’t be captured.
* For local frameworks that use configuration libraries (e.g., Spring Boot, Twelve-Factor apps), a `.env` file can often be wired in directly or via the framework’s environment loader.

Links and references

* Telepresence docs: [https://www.telepresence.io/](https://www.telepresence.io/)
* Kubernetes environment variables: [https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/](https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/telepresence-for-kubernetes/module/0eb5dcb6-2e2a-40d9-9caa-bd3149741aeb/lesson/d6bda8d0-e3f3-4f40-a5ad-3d63a3fe86b2" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.