[Docker-compose] Difference between env_file and environment. Priority of environment variable application

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.


## Difference between env_file and environment

-The method of reading and setting environment variables is different.

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

### ・ Different priorities

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:

Since 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.


env_file

The file path specified by env_file is based on the directory where docker-compose is executed.

** ▼ How to specify the file **

  1. When there is only one file env_file: .env
  2. When there are multiple files (even one)

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

[Docker-compose] Difference between env_file and environment. Priority of environment variable application
Difference between instance variable and class variable
[Java] Difference between assignment of basic type variable and assignment of reference type variable
Difference between Ruby instance variable and local variable
Difference between isEmpty and isBlank of StringUtils
Difference between addPanel and presentModally of FloatingPanel
Difference between member and collection of rails routes.rb
[Java] Difference between "final variable" and "immutable object"
Difference between vh and%
Difference between i ++ and ++ i
Difference between product and variant
Difference between redirect_to and render
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between puts and print
Difference between redirect_to and render
Difference between CUI and GUI
I will explain the difference between Android application development and iOS application development from the perspective of iOS engineers
Difference between variables and instance variables
Difference between mockito-core and mockito-all
[Ruby] About the difference between 2 dots and 3 dots of range object.
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
[Java] Difference between Stack Overflow Error and Out Of Memory Error
What is the difference between a web server and an application server?
Difference between byCharWrapping and byWordWrapping of UI Label in Japanese display
Difference between instance method and class method
Difference between render method and redirect_to
Difference between interface and abstract class
Difference between == operator and equals method
[Java] Difference between Hashmap and HashTable
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
[Ruby] Difference between is_a? And instance_of?
Difference between == operator and eqals method
Rough difference between RSpec and minitest
[Rails] Difference between find and find_by
Understand the difference between each_with_index and each.with_index
[JAVA] Difference between abstract and interface
Difference between Thymeleaf @RestController and @Controller
Difference between Stream map and flatMap
[Java] Difference between array and ArrayList
Difference between primitive type and reference type
Application of downcase and slice methods
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
[rails] Difference between redirect_to and render
[Android] Difference between finish (); and return;
How to use args :, environment :, env_file: and .env files with docker-compose command
Note: Difference between Ruby "p" and "puts"
Difference between final and Immutable in Java
[Memo] Difference between bundle install and update