Short answer
A .env file is a plain text list of KEY=value settings that stays outside the source code, and the running process reads those values from its environment. The Twelve-Factor App says config is whatever changes between deploys, including database handles and credentials, and that those values belong in environment variables. The dotenv README says not to commit the file, unless you encrypt it. Docker can load the same style of file into a container.
Last verified against the sources listed below.
Key takeaways
- Each line is a key, then an equals sign or, in Compose, a colon, then a value. Lines starting with # are comments.
- Twelve-Factor says env vars should hold what changes per deploy, and code should hold what does not.
- The dotenv README answers "Should I commit my .env file?" with no, unless the file is encrypted with dotenvx.
- Node's process.env is the user environment. loadEnvFile reads ./.env and does not replace variables already set.
- Docker's container docs say not to pass passwords in environment variables. Use secrets for those values.
People who ask what is a env file want the text file that holds settings outside the program. The usual name is .env. Each line is a key, an equals sign, and a value. The Twelve-Factor App says that kind of config belongs in the environment, because it changes between deploys and the code does not.
Back end development is where those values are read. The file is a convenient way to set the environment. The process environment is what the running app actually sees.
What does one line look like?
One line names a variable and gives it a value.
The dotenv README shows a file like this, with a comment and quoted values:
HELLO="Dotenv"
OPENAI_API_KEY="your-api-key-goes-here"
A line that starts with a hash is a comment. From dotenv 15.0.0 on, a hash later in the line also starts a comment unless the value is quoted. Multiline values are supported from that same version.
Docker Compose documents a close syntax for its own env files. Blank lines are ignored. The separator can be an equals sign or a colon.
Spaces around the value are ignored. Double quotes allow interpolation. Single quotes are taken literally.
A kebab-case converter helps when a label still has spaces and you need a file or path name. Variable keys themselves are usually uppercase words separated by underscores, which is the shape in the Docker and dotenv examples.
Why keep the file out of git?
Commit the code. Keep credentials out of the repository.
Twelve-Factor’s test is whether the codebase could be published at any moment without compromising a credential. Constants in source fail that test. A config file that is not checked in is better, and the page still calls it risky, because it is easy to commit by mistake and because those files tend to scatter in framework-specific formats.
The dotenv FAQ answers “Should I commit my .env file?” with no. The exception it allows is a file you encrypt with dotenvx. It also says that if you already committed the file, you should remove it and remove it from git history. Website security includes that cleanup. A secret in an old commit is still a secret.
Twelve-Factor also warns against grouping every setting into named blocks such as development, test, and production. It wants each variable managed on its own for each deploy. The dotenv README still recommends one file per environment, such as .env and .env.production, and says to duplicate values rather than inherit one file from another.
What does the app actually read?
The app reads the process environment. The file is only one way to fill that environment.
In Node, process.env is an object of the user environment. Changing it inside the process does not change the shell outside the process, and it does not change other worker threads unless you set that up. process.loadEnvFile() reads .env by default. Existing variables win, and NODE_OPTIONS inside the file does not affect Node.
dotenv’s job is the same fill step. The README shows require(‘dotenv’).config() and import ‘dotenv/config’. After that, process.env contains the keys from the file.
With more than one file, the first value wins unless you pass an override flag. Put the import before other modules that read the variables. You can also start the process with dotenv run so the values exist before Node starts.
Docker makes the same split. An env file next to compose.yaml supplies values for interpolation when you run docker compose up. That file is not automatically the environment inside the container.
The env_file attribute copies a file into the container. The environment attribute can override those keys. The container page says not to pass passwords that way, and to use secrets instead.
What belongs in the file, and what belongs in code?
Put values that change per deploy in the environment. Put the structure of the program in code.
| Put it in the environment | Put it in code |
|---|---|
| Database and cache connection details | Routing tables, such as the Rails routes file Twelve-Factor cites |
| Credentials for services outside the app | How modules are wired together |
| The canonical hostname of this deploy | Behavior that is the same in every deploy |
| A flag that differs on a laptop and in production | The schema and the queries |
Twelve-Factor’s examples of config are resource handles, credentials, and the hostname. Its examples of non-config are config/routes.rb and Spring’s module wiring. A port number that changes between your laptop and production fits the file. The SQL that reads a table fits the code.
How do Docker and Node load it?
Node loads the file into process.env. Compose uses it for interpolation, and env_file injects it into the container.
For Compose interpolation, the shell environment wins, then a file you pass with --env-file, then .env in the project directory. The project directory is --project-directory if you set it, otherwise the directory of the first Compose file, otherwise the current directory. docker compose config prints the resolved file.
Inside the container, the precedence page ranks docker compose run -e first. An interpolated environment or env_file value comes next. A plain environment entry follows.
Then env_file alone. The image ENV instruction is last.
From Compose 2.24.0, an env_file entry can set required to false so a missing file is ignored. From 2.30.0, a raw format passes values through without interpolation.
If the variable is missing, Compose interpolation substitutes an empty string. The variable page shows that an unset tag can leave an image name ending in a colon, which is not a valid reference. Set the variable, or give the Compose file a default with the ${VAR:-default} form.
What mistakes leak a key?
The usual leaks are a committed file, a printed environment, and a password stuffed into a container variable.
| Mistake | Why it spreads | What to do |
|---|---|---|
.env committed with a live key |
The key is in git history, not only in the latest tree | Remove the file and the history, then rotate the key |
Quotes omitted around a value that contains # |
dotenv treats the hash as a comment | Quote the value |
Assuming .env is inside the container |
Compose interpolation and env_file are different steps |
Set env_file or environment on the service |
Passing a database password with environment |
Docker says not to pass passwords that way | Use a secret |
| Expecting the file to override the shell | Existing Node variables and the Compose shell win | Unset the shell variable, or pass an override flag where the tool allows it |
Name the keys for humans, keep one file per deploy, and read them from the environment in the process. The filename .env is a convention the tools above agree on. The contract that matters is the environment the process receives.
Frequently asked questions
Does Node overwrite variables that are already set?
Node's process.loadEnvFile loads a .env file into process.env. The default path is ./.env. If a variable is already set, the existing value stays and the file does not replace it. NODE_OPTIONS in that file has no effect on Node. The API is stable as of Node.js 24.10.0 and 22.21.0. Assigning process.env inside the process does not change the shell that started Node. A one-line assignment will not appear in a later echo of that variable.
Which file wins when Docker Compose sees two sources?
For interpolation, the shell wins, then a file passed with --env-file, then .env in the project directory. Inside the container, docker compose run -e is highest, then an interpolated environment or env_file value, then an environment entry, then env_file, then the image ENV instruction. If --env-file is omitted and the project .env sets COMPOSE_FILE to another directory, Compose also loads a second .env from that directory at lower precedence. A later file in an env_file list overrides an earlier file for the same key.
Can a value contain a hash mark?
The dotenv README says a comment starts where a hash appears, including inline, so quote a value that contains one. That rule is a breaking change from version 15.0.0. Docker's env file syntax is similar. An unquoted inline comment needs a space before the hash, and a hash glued to the value is part of the value. A comment on a quoted value has to follow the closing quote. Single quotes in Compose are literal, so a dollar sign inside them is not expanded.
What does Twelve-Factor refuse to call config?
The config chapter excludes internal application config, such as a Rails routes file or how modules connect in Spring. That material does not change between deploys, so it stays in code. The litmus test is whether you could publish the codebase without exposing a credential. Resource handles, external credentials, and the canonical hostname of a deploy are the examples it puts in the environment. Grouping variables into named blocks such as development and production is the pattern it says does not scale.
Should every secret go through an environment variable?
Twelve-Factor wants credentials in the environment rather than in source. Docker's container page adds a limit: do not pass passwords into containers with environment variables. Use secrets instead. The dotenv README says not to commit a .env file, and to remove it from git history if you already did. Encryption with dotenvx is the exception it allows before a commit. A value the process can read is still a value a debug print can reveal.
Sources
- The Twelve-Factor App: Config · Twelve-Factor, 2017
- Set environment variables within your container's environment · Docker, 2026
- Variable interpolation · Docker, 2026
- Environment variables precedence · Docker, 2026
- dotenv README · dotenv, 2026
- Node.js process · Node.js, 2026