Keywords: Docker | Environment Variables | Container Configuration
Abstract: This article provides an in-depth exploration of three primary methods for passing environment variables to Docker containers: embedding in Dockerfile, using -e/--env command-line parameters, and leveraging --env-file configuration files. It analyzes the applicable scenarios, security considerations, and best practices for each approach, covering the complete workflow from basic configuration to production deployment to help developers achieve efficient configuration management in containerized applications.
Overview of Docker Environment Variable Passing Methods
In containerized application deployment, proper environment variable passing is crucial for ensuring application portability across different environments. Through appropriate environment variable configuration, developers can achieve separation of application configuration from code, enhancing deployment flexibility and security.
Command-Line Parameter Approach
Using the -e or --env flag is the most direct method for passing environment variables. This approach is suitable for temporary testing or scenarios with a small number of variables. The basic syntax is:
docker run -e VARIABLE_NAME=value image_name
In actual deployment scenarios, multiple environment variables are typically passed simultaneously:
sudo docker run -d -t -i -e REDIS_NAMESPACE='staging' \
-e POSTGRES_ENV_POSTGRES_PASSWORD='foo' \
-e POSTGRES_ENV_POSTGRES_USER='bar' \
-e POSTGRES_ENV_DB_NAME='mysite_staging' \
-e POSTGRES_PORT_5432_TCP_ADDR='docker-db-1.hidden.us-east-1.rds.amazonaws.com' \
-e SITE_URL='staging.mysite.com' \
-p 80:80 \
--link redis:redis \
--name container_name dockerhub_id/image_name
For sensitive information, it's recommended to inherit variable values from the current shell environment to avoid plaintext display in command lines:
sudo PASSWORD='foo' docker run [...] -e PASSWORD [...]
Environment File for Batch Management
When managing numerous environment variables or dealing with sensitive configurations, using environment files is the superior choice. Variables can be loaded in bulk through the --env-file parameter:
docker run --env-file ./env.list ubuntu bash
Environment files should follow standard format, with each line defining one variable:
LOG_LEVEL=DEBUG
MONGODB_URL=mongodb://localhost:27017
API_KEY=secret_value_here
Comment lines start with #, supporting complex configuration management requirements. This method is particularly suitable for team collaboration and CI/CD pipeline integration.
Dockerfile Embedded Environment Variables
Defining environment variables during the build phase is another common practice. By using the ENV instruction in Dockerfile, configurations can be solidified into the image:
FROM node:20-alpine
RUN addgroup app && adduser -S -G app app
USER app
WORKDIR /app
COPY package*.json ./
USER root
RUN chown -R app:app .
ENV LOG_LEVEL=debug
USER app
RUN npm install
COPY . .
EXPOSE 5173
CMD npm run dev
This approach is suitable for configuration items that don't change with the environment, but lacks runtime flexibility.
Security Best Practices
Environment variable management requires special attention to security considerations:
- Avoid hardcoding sensitive information in Dockerfile, such as database passwords, API keys, etc.
- For production environments, recommend using Docker Secrets or external key management services
- Environment files should be included in version control ignore lists to prevent accidental commits
- Regularly rotate sensitive environment variables and establish comprehensive access control mechanisms
Docker Compose Integration Solution
In complex application deployments, Docker Compose provides more powerful environment variable management capabilities. Through the environment attribute or env_file configuration, multi-service coordination can be achieved:
services:
webapp:
environment:
DEBUG: "true"
DATABASE_URL: postgresql://user:pass@db:5432/app
env_file:
- webapp.env
Compose also supports environment variable interpolation and conditional loading, significantly enhancing configuration management flexibility.
Verification and Debugging Techniques
Confirming correct environment variable passing is an important step in the deployment process. The following commands can be used to verify environment variables within containers:
docker inspect --format '{{.Config.Env}}' container_id
Or directly view in interactive containers:
docker container run -it --env MY_ENV=development nginx /bin/sh
export
Conclusion and Recommendations
Environment variable passing strategies should be chosen based on specific scenarios: command-line parameters for development debugging, environment files for team collaboration, and Dockerfile embedding for fixed configurations. Regardless of the approach adopted, the security-first principle should be followed, establishing standardized configuration management processes.