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
Official reference Environment variables in Docker documentation | compose I will explain while organizing the knowledge according to.
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
fileHowever, 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'
** 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.
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_filein
docker-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 **.
--$ ...
in docker-compose.yml
is basically a host environment variable, not an easy-to-use variable.
--ʻEnv_fileand
.env` files are completely different
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.
docker-compose.yml
like ordinary variablesDepending 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.
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 of
docker-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
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