0
0
Dockerdevops~5 mins

Environment files (.env) in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Environment files store settings like passwords or ports outside your main code. This keeps your app flexible and safe by changing settings without editing code.
When you want to keep database passwords separate from your app code to avoid exposing them.
When you need to run the same app on different servers with different settings like ports or API keys.
When you want to share your app code but keep your personal or secret settings private.
When you want to quickly change environment variables without rebuilding your Docker image.
When you want to organize many environment variables in one simple file for easier management.
Config File - .env
.env
APP_NAME=my-app
APP_ENV=production
APP_PORT=8080
DB_HOST=database
DB_USER=admin
DB_PASS=secret123

This file sets environment variables for your app.

APP_NAME names your app.

APP_ENV sets the environment type like production.

APP_PORT defines which port the app listens on.

DB_HOST, DB_USER, and DB_PASS configure database connection details.

Commands
This command runs an nginx container using the environment variables from the .env file and maps port 8080 on your computer to port 8080 in the container.
Terminal
docker run --env-file .env -p 8080:8080 nginx
Expected OutputExpected
Unable to find image 'nginx:latest' locally latest: Pulling from library/nginx Digest: sha256:... Status: Downloaded newer image for nginx:latest f1a2b3c4d5e6 nginx
--env-file - Loads environment variables from the specified file
-p - Maps a port from the host to the container
This command checks inside the running container to see if the APP_NAME environment variable is set correctly.
Terminal
docker exec f1a2b3c4d5e6 printenv APP_NAME
Expected OutputExpected
my-app
Key Concept

If you remember nothing else from this pattern, remember: environment files let you safely and easily change app settings without touching your code.

Common Mistakes
Not using the --env-file flag and trying to pass environment variables directly without quotes or proper syntax.
Docker won't load the variables correctly, causing your app to miss important settings.
Use --env-file with a properly formatted .env file or pass variables with -e VAR=value syntax.
Including spaces around the equal sign in the .env file like 'APP_NAME = my-app'.
Docker treats spaces as part of the variable name or value, causing errors or unexpected behavior.
Write variables without spaces: 'APP_NAME=my-app'.
Committing .env files with secrets to public repositories.
This exposes sensitive information like passwords to anyone who can see the code.
Add .env to .gitignore and share secrets securely.
Summary
Create a .env file to store environment variables like app name and database credentials.
Use docker run with --env-file to load these variables into your container.
Verify variables inside the container with docker exec and printenv.