# Use environment variables at build time

Your variables are runtime env in the container. When a build step needs them, mount them as a secret for that one step.

## Set your variables

```
npx ezhst env set NEXT_PUBLIC_SITE_URL=https://my-app.easyhost.fun
npx ezhst env import .env
```

`.env` files are never uploaded by `deploy`, so set values with the CLI or the dashboard.

## Mount the appenv secret

During the build, your variables and `PORT` are available as a file at `/run/secrets/appenv`, but only in a `RUN` step that mounts it. Load the file, then run your command:

```Dockerfile
RUN --mount=type=secret,id=appenv,target=/run/secrets/appenv sh -c 'set -a; . /run/secrets/appenv; set +a; npm run build'
```

`set -a` exports every variable the file defines, so tools such as `next build` or `vite build` see them. The values exist only while that step runs and are not stored in the image.

## When you need this

- Frameworks that inline public values at build time, such as `NEXT_PUBLIC_` or `VITE_` variables
- Build steps that call an API or download private packages
- Code generation that reads a database URL, such as `prisma generate`

A step that does not need your variables can be a plain `RUN` line.

## Never use ARG or ENV for secrets

`ARG` values are recorded in the image history, and `ENV` values are baked into every layer after them. Anyone with the image can read both. Use `ENV` only for non-secret settings such as `NODE_ENV=production`.

## Apply changes

```
npx ezhst env set NEXT_PUBLIC_API_URL=https://api.example.com
npx ezhst redeploy
```

Build-time values are fixed into the build output, so changing one needs a rebuild. Runtime values apply on the next `deploy` or `redeploy`. See [write a Dockerfile](/guides/write-a-dockerfile) for the other rules.

## Frequently asked questions

### Why is my variable undefined during the build?

The build step does not mount the appenv secret. Wrap the command with the secret mount shown above, upload the Dockerfile and redeploy.
