


How do you specify environment variables in a Docker container?
Jun 28, 2025 am 12:22 AMThere are three common ways to set environment variables in a Docker container: use the -e flag, define ENV instructions in a Dockerfile, or manage them through Docker Compose. 1. Adding the -e flag when using docker run can directly pass in variables, which is suitable for temporary testing or CI/CD integration; 2. Set default values ??in Dockerfile using ENV, which is suitable for fixed variables that are not often changed, but is not suitable for distinguishing different environment configurations; 3. Docker Compose can define variables through environment blocks or .env files, which is more conducive to development collaboration and configuration separation, and supports variable replacement. Choose the right method or use multiple methods in combination according to project needs.
You can specify environment variables in a Docker container in several practical ways, depending on your setup and needs. The most common methods include using the -e
flag when running a container, defining them in a Dockerfile with ENV
, or managing them via a .env
file when using Docker Compose.
Using the -e
flag with docker run
When you start a container manually using docker run
, you can pass environment variables directly on the command line with the -e
flag.
For example:
docker run -d -e MY_VAR="hello" my-app
This sets the variable MY_VAR
to "hello"
inside the container.
- You can set multiple variables by repeating the
-e
flag. - This is useful for one-off containers or testing different configurations.
- It's also helpful when integrating with CI/CD systems where secrets or settings are injected dynamically.
If you're setting sensitive data like API keys or passwords, be cautious about exposing them in shell history or logs.
Defining environment variables in a Dockerfile
You can also define default environment variables directly in your Dockerfile using the ENV
instruction.
Example:
ENV MY_VAR hello ENV LOG_LEVEL debug
These values ??will be baked into the image and available in every container started from it unless overridden at runtime.
- This method is good for setting defaults that rarely change.
- It's not ideal if you need different values ??across environments (like dev vs prod).
- Keep in mind these values ??are visible in the image metadata, so avoid putting sensitive info here.
Managing environment variables with Docker Compose
When working with Docker Compose, you can define environment variables in two main ways:
Directly in the
environment
block of yourdocker-compose.yml
:services: app: image: my-app environment: MY_VAR: "hello"
Using an external
.env
file with theenv_file
option:services: app: image: my-app env_file: - .env
The second approach helps keep your configuration clean and portable. The .env
file looks like this:
MY_VAR=hello LOG_LEVEL=debug
- This is great for local development and team settings.
- Make sure to add
.env
files to.gitignore
if they contain sensitive data. - Docker Compose supports variable substitution too, so you can reference existing shell variables.
That's how you handle environment variables in Docker — through CLI flags, Dockerfiles, or compose files. Each method has its use case, and often you'll combine them depending on your project's complexity.
The above is the detailed content of How do you specify environment variables in a Docker container?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

To develop a complete Python Web application, follow these steps: 1. Choose the appropriate framework, such as Django or Flask. 2. Integrate databases and use ORMs such as SQLAlchemy. 3. Design the front-end and use Vue or React. 4. Perform the test, use pytest or unittest. 5. Deploy applications, use Docker and platforms such as Heroku or AWS. Through these steps, powerful and efficient web applications can be built.

There are three ways to view the process information inside the Docker container: 1. Use the dockertop command to list all processes in the container and display PID, user, command and other information; 2. Use dockerexec to enter the container, and then use the ps or top command to view detailed process information; 3. Use the dockerstats command to display the usage of container resources in real time, and combine dockertop to fully understand the performance of the container.

Deploying a PyTorch application on Ubuntu can be done by following the steps: 1. Install Python and pip First, make sure that Python and pip are already installed on your system. You can install them using the following command: sudoaptupdatesudoaptinstallpython3python3-pip2. Create a virtual environment (optional) To isolate your project environment, it is recommended to create a virtual environment: python3-mvenvmyenvsourcemyenv/bin/activatet

Deploying and tuning Jenkins on Debian is a process involving multiple steps, including installation, configuration, plug-in management, and performance optimization. Here is a detailed guide to help you achieve efficient Jenkins deployment. Installing Jenkins First, make sure your system has a Java environment installed. Jenkins requires a Java runtime environment (JRE) to run properly. sudoaptupdatesudoaptininstallopenjdk-11-jdk Verify that Java installation is successful: java-version Next, add J

An efficient way to batch stop a Docker container includes using basic commands and tools. 1. Use the dockerstop$(dockerps-q) command and adjust the timeout time, such as dockerstop-t30$(dockerps-q). 2. Use dockerps filtering options, such as dockerstop$(dockerps-q--filter"label=app=web"). 3. Use the DockerCompose command docker-composedown. 4. Write scripts to stop containers in order, such as stopping db, app and web containers.

There are two ways to compare the differences in different Docker image versions: 1. Use the dockerdiff command to view changes in the container file system; 2. Use the dockerhistory command to view the hierarchy difference in the image building. These methods help to understand and optimize image versioning.

Implementing Docker's automated deployment on Debian system can be done in a variety of ways. Here are the detailed steps guide: 1. Install Docker First, make sure your Debian system remains up to date: sudoaptupdatesudoaptupgrade-y Next, install the necessary software packages to support APT access to the repository via HTTPS: sudoaptinstallapt-transport-httpsca-certificatecurlsoftware-properties-common-y Import the official GPG key of Docker: curl-

Through Docker containerization technology, PHP developers can use PhpStorm to improve development efficiency and environmental consistency. The specific steps include: 1. Create a Dockerfile to define the PHP environment; 2. Configure the Docker connection in PhpStorm; 3. Create a DockerCompose file to define the service; 4. Configure the remote PHP interpreter. The advantages are strong environmental consistency, and the disadvantages include long startup time and complex debugging.
