An .env file stores configuration as environment-variable assignments, keeping values such as credentials and service URLs out of application code. The filename is flexible: examples include .env, .env.local, and .sql_server.conf. Keep sensitive files out of source control, and document the required settings so others can configure new instances. section 3.7 section 6.5.3
Use the file to change configuration without editing hardcoded values:
- For Docker, specify the environment file with the service’s env_file directive. section 3.7
- For a Next.js API call, read the configured URL from process.env instead of embedding localhost in the component. section 6.5.3
POSTGRES_USER=postgres POSTGRES_PASSWORD=secret
Example assignments for PostgreSQL credentials. section 3.7
postgres:
build:
context: ./db
dockerfile: Dockerfile
env_file:
- ./.sql_server.conf
Docker service configuration that loads an environment file. section 3.7
Listing 6.27 Our .env.local file
NEXT_PUBLIC_API_URL=http://localhost:8081/api/v1
A Next.js local environment file defining the API base URL. section 6.5.3
Listing 6.28 Updated API call
useEffect(() => {
const apiUrl = process.env.NEXT_PUBLIC_API_URL ?? ''; #1
axios.get(`${apiUrl}/files`) #2
Updated API call that reads its base URL from process.env. section 6.5.3




