This is a personal memo.
Both env_file and environment are docker-compose.yml and ** specify environment variables ** at run time.
--Applies only when running (container creation). -** Not used in build (image creation) ** --If you want to specify environment variables during build, use build-> args.
env_file ** reads the file containing the environment variables **.
For environment, ** describe variables directly in docker-compose.yml **.
env_file example
env_file:
- .env
- .env.prd
- .env.local.aws
- ./common.env
- ./apps/web.env
environment example
environment:
- NODE_ENV=development
- PORT=80
- MIGRATE=true
- USER=${USER}
- AWS_REGION=ap-northeast-1
If the same environment variable is specified for each, ** environment setting takes precedence over env_file **.
If multiple files are specified in env_file or multiple duplicate variables are specified in environment, ** the last one has priority **.
If the host has an environment variable set, that value takes precedence.
·Priority:
(1) Host> (2) environment> (3) env_file> (4) ENV
* in DockerfileSince the environment variable of the host has the highest priority, if the environment variable of the host is applied, it is necessary to change the environment variable name on the docker side.
The file path specified by env_file is based on the directory where docker-compose is executed.
** ▼ How to specify the file **
env_file: .env
In case of multiple
env_file:
- .env
- .env.prd
- .env.local.aws
- ./common.env
- ./apps/web.env
In one case
env_file:
- .env
** ▼ There are multiple ways to name files **
file name | Contents |
---|---|
.env | Default file name |
.env.local.aws | To head.env is attached |
common.env | behind.env is attached |
./apps/web.env | Files in the lower hierarchy |
Recommended Posts