Difference between docker-compose env_file and .env file

Until now, I was confused in docker-compose

--File to be read by ʻenv_file in docker-compose.yml --File named.env`

Make a note of the difference so that no one else makes the same mistake.

Since the handling of environment variables is slightly different between Linux and Windows, I think that people who are accustomed to Windows or who are using docker for windows are likely to get caught.

It's basically the same as this question and answer, but with a little more information.

docker-compose cannot understand my env_file | stack overflow

docker-compose and environment variables

Official reference Environment variables in Docker documentation | compose I will explain while organizing the knowledge according to.

Host environment variables used in docker-compose.yml

In docker-compose.yml, you can use environment variables of the host side shell.

For example

docker-compose.yml


web:
  image: "webapp:${TAG}"

If the environment variable TAG is set on the host side, that value will be inserted. For example, if TAG is set to v1.5, the image name created by docker-compose build will be webapp: v1.5.

You can check what value is inserted in the set environment variable with the docker-compose config command.

$ echo $TAG
#Echo for Windows%TAG%Confirm with
v1.5

$ docker-compose config

version: '3'
services:
  web:
    image: 'webapp:v1.5'

.env file

However, the environment variables you want to use for the host are not always set. As a workaround in that case, you can set the default value of the environment variable used in docker-compose.yml in the file .env. For example in the .env file

.env


TAG=v1.5

If it is set as, the image name will be webapp: v1.5 as above.

The .env file must be in the directory where you ran the ** docker-compose command (working directory) ** It will not be recognized elsewhere. (See Declare default environment variables in file)

This .env file setting is a workaround if the host doesn't have the environment variables you want to use. Of course, if an environment variable with the same name is set on the host, that will take precedence.

$ export TAG=v2.0
#Set TAG for Windows=v2.Set environment variable at 0
$ docker-compose config

version: '3'
services:
  web:
    image: 'webapp:v2.0'

Environment variables in the launched container

** Apart from the above story, ** By using the item of ʻenvironment in docker-compose.yml, environment variables can be added to the container launched by docker-compose run or docker-compose up`. You can set the.

docker-compose.yml


web:
  environment:
    - DEBUG=1

Then, in the container that started up

$ echo $DEBUG
1

And environment variables are set.

Item of ʻenv_file in docker-compose.yml`

You can also write the environment variables in this container in a separate file. For example, in a file called web-variables.env

web-variables.env


DEBUG=1
FOO=bar

And write

web-variables.env


web:
  env_file:
    - web-variables.env

And, if you specify the file to be read in the item of ʻenv_fileindocker-compose.yml`, In the container

$ echo $DEBUG
1
$ echo $FOO
bar

And environment variables are set.

From the above,

--Files read by ʻenv_file --The.env` file mentioned earlier

You can see that ** works completely different **.

Summary

--$ ... in docker-compose.yml is basically a host environment variable, not an easy-to-use variable. --ʻEnv_fileand.env` files are completely different

How did you get into this problem?

Finally, by searching, as much as possible to help those who are having trouble with this problem solve it. Here's how I got into this problem.

I was trying to use the environment variables in docker-compose.yml like ordinary variables

Depending on the case, use the value to be assigned to $ ... in docker-compose.yml. I suffered from this env_file and .env file when trying to accommodate various cases. Originally, you should split docker-compose.yml by using the -f option.

I wanted to set proxy for docker-compose

When docker-compose under a proxy, in build of docker-compose, You must set environment variables for proxies such as HTTP_PROXY in the launched container.

To solve this, you can set the build> args item and the ʻenvironment:item ofdocker-compose`. I suffered from this env_file and .env file here.

Based on these, I made an article for docker-compose under the proxy.

Combining proxy settings with docker-compose into one file

I tried to use an .env file with VS Code's Remote Container

Advanced Container Configuration | VSCode In the remote debugging function (Remote Container) using VSCode docker, I tried to launch a remote container by specifying docker-compose.yml in the dockerComposeFile section from devcontainer.json. At that time, I tried to use the .env file to set the proxy, The .env file could not be set correctly in Remote Container even in the same folder as docker-compose.yml.

The cause is as mentioned above The .env file must be in the directory when you ran the ** docker-compose command ** It was that.

In Remote Container, docker-compose is executed in the root directory of VS Code, so I put the .env file in the root directory and it worked fine. (See Github Microsoft VSCode issue # 222)

Recommended Posts

Difference between docker-compose env_file and .env file
Difference between process and job
Difference between regression and classification
Difference between np.array and np.arange
Difference between MicroPython and CPython
Difference between ps a and ps -a
Difference between return and print-Python
Difference between Ruby and Python split
Difference between java and python (memo)
Difference between == and is in python
Memorandum (difference between csv.reader and csv.dictreader)
(Note) Difference between gateway and default gateway
Difference between Numpy randint and Random randint
Difference between sort and sorted (memorial)
Difference between python2 series and python3 series dict.keys ()
[Python] Difference between function and method
Difference between SQLAlchemy flush () and commit ()
Python --Difference between exec and eval
[Python] Difference between randrange () and randint ()
[Python] Difference between sorted and sorted (Colaboratory)
File open function in Python3 (difference between open and codecs.open and speed comparison)
[Xg boost] Difference between softmax and softprob
difference between statements (statements) and expressions (expressions) in Python
[Django ORM] Difference between values () and only ()
Difference between PHP and Python finally and exit
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
Difference between linear regression, Ridge regression and Lasso regression
[Python] Difference between class method and static method
[Python Iroha] Difference between List and Tuple
[python] Difference between rand and randn output
speed difference between wsgi, Bottle and Flask
Difference between numpy.ndarray and list (dimension, size)
Difference between ls -l and cat command
Difference and compatibility verification between keras and tf.keras # 1
What is the difference between `pip` and` conda`?
[Samba] File sharing between Linux and Windows machines
Difference between using and import on shield language
[python] Difference between variables and self. Variables in class
About the difference between "==" and "is" in python
Create new file [Comparison between Bash and PowerShell]
About the difference between PostgreSQL su and sudo
What is the difference between Unix and Linux?
Consideration of the difference between ROC curve and PR curve
The rough difference between Unicode and UTF-8 (and their friends)
Can BERT tell the difference between "candy (candy)" and "candy (rain)"?
File write speed comparison experiment between python 2.7.9 and pypy 2.5.0
Difference between Ruby and Python in terms of variables
Difference between Numpy (n,) and (n, 1) notation [Difference between horizontal vector and vertical vector]
Difference between return, return None, and no return description in Python
How to use argparse and the difference between optparse
Center difference and forward difference
Between parametric and nonparametric
Edit the config file and run docker-compose up (Django + MySQL ④)
What is the difference between a symbolic link and a hard link?
Python module num2words Difference in behavior between English and Russian
Python> Difference between inpbt and print (inpbt) output> [1. 2. 3.] / array ([1., 2., 3.], dtype = float32)
Understand the difference between cumulative assignment to variables and cumulative assignment to objects
List concatenation method in python, difference between list.extend () and “+” operator
Difference between SQLAlchemy back_populates and backref and when neither is used