# Required Variables — Docker Compose

Source: https://www.skillbyai.com/en/docker-compose/c-required

> Fail fast with a clear message.

## ${VAR:?message}

`${VAR:?message}` makes a variable **required**: if it is unset or empty, Compose refuses to load the file and prints your message. This is much better than starting containers with an empty password or URL and debugging the failure later. Use it for secrets and anything without a safe default.

## Loading without the .env file, run

I ran this with Docker Compose v2.38.1 and jq in the demo "shop" project. docker compose config parses, interpolates, merges and validates the files without starting containers; the Docker daemon was not running, so nothing was started. Pointing --env-file at an empty file leaves DB_PASSWORD unset, so Compose stops with the custom message "set DB_PASSWORD in .env" and exit code 1, before anything starts.

```bash
docker compose --env-file /dev/null config
```

Output:

```
error while interpolating services.api.environment.DATABASE_URL: required variable DB_PASSWORD is missing a value: set DB_PASSWORD in .env
(exit code 1)
```

## Write helpful messages

Say where the value should come from ("set DB_PASSWORD in .env") so the next person fixes it in seconds.

**Quiz:** What does ${DB_PASSWORD:?msg} do when DB_PASSWORD is empty?

- [ ] Uses the word msg as the password
- [ ] Uses an empty password
- [x] Stops loading with the error message
- [ ] Prompts interactively

*Answer:* Stops loading with the error message. Fail fast on missing configuration.
