My environment:
# docker-compose version
docker-compose version 1.8.1, build 878cff1
docker-py version: 1.10.3
CPython version: 2.7.12
OpenSSL version: OpenSSL 1.0.2h 3 May 2016
#
I'm seeing following _WARNING_ message:
# docker-compose ps
WARNING: The ELK variable is not set. Defaulting to a blank string.
Name Command State Ports
---------------------------------------------------------------------------------------
nginx_nginx_1 nginx -g daemon off; Up 0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
#
Per Environment variables in Compose - Docker:
# grep ELK docker-compose.yaml
gelf-address: "udp://${ELK}:12201"
#
# grep -A1 env_file docker-compose.yaml
env_file:
- docker-compose.env
# grep ELK docker-compose.env
ELK=172.17.0.2
#
Please advise.
You need to escape the variable if you want it to be expanded inside the container, using a double-dollar sign ($${ELK}
).
If however, you want it to be interpreted on your host, the $ELK needs to be defined in your environment or in the .env
file. The env_file
option defines environment variables that will be available inside the container only.
@shin-
I'm trying to implement following:
It's not intuitive to me that the file specified by the "env_file" directive acts differently than the ".env" file...
However @shin- 's comment made it clear to me... the ".env" file's vars are available at docker-compose stage whereas all other env vars set via env_file or env directive are only available within the container... ok
However @shin- 's comment made it clear to me... the ".env" file's vars are available at docker-compose stage whereas all other env vars set via env_file or env directive are only available within the container... ok
That feels a lot like a bug. There shouldn't be "too specific" behavior. It only confuses people.
Found this issue first in the search for a resolution, and ended up understanding then solving it with the info here: http://staxmanade.com/2016/05/how-to-get-environment-variables-passed-through-docker-compose-to-the-containers/
Specifically;
set the variable in ./.env
, which makes it available to substitute in docker-compose.yml
EXTERNAL_PORT=8000
then in docker-compose.yml
, pass it through to the container with:
environment:
- EXTERNAL_PORT=${EXTERNAL_PORT}
edit: added ./
to make it clear we are not talking about <file>.env
@Joshfindit that is EXACTLY like in my comment and yet this isn't working for me, hence new issue..
@a1exus I don't see where you're mentioning ./.env
in your comments.
What is the content of your ./.env
file?
@Joshfindit at the very end of my comment:
# grep ELK docker-compose.env
ELK=172.17.0.2
#
@a1exus ./docker-compose.env
!= ./.env
To reiterate what I wrote earlier in this thread:
environment
and env_file
sections of the Compose file declare variables that will be available inside the container.$VAR
or ${VAR}
syntax inside the Compose file are replaced by the value found on the host machine (i.e. the machine you're executing docker-compose
from) at the time of execution. They're found either in the OS's environment or the statically named .env
file.$$VAR
or $${VAR}
) to escape the sequence.Per @Joshfindit I've tried with .env
and everything works like a charm! Thank you!
@shin- can you explain what I'm missing here?
Variables written using the $VAR or ${VAR} syntax inside the Compose file are replaced by the value found on the host machine (i.e. the machine you're executing docker-compose from) at the time of execution.
services:
nginx:
user: ${UID}
I get this: WARNING: The UID variable is not set. Defaulting to a blank string.
However:
[jacob:.../foo]$ echo $UID
501
@oojacoboo $UID is not an environment variable, it's a shell variable:
$ echo $UID
1000
$ printenv | grep UID # no output
$
See #2380
@shin- was just actually about to update my comment as I noticed the same thing - thanks. And thanks for the issue link - perfect.
I managed to find a solution! I used the env_file
setting itself, and defined the file variables.env
within the same line. And it worked like a charm!
Here is how my docker-compose.yml looks like:
services:
web:
env_file: variables.env
you can also use _.env_ in root of your project it does same...
just for future googlers. my problem was with environment variables not being passed through sudo
(e.g. on Ubuntu). Working solution:
sudo -E docker-compose up -d
Hello, I am problem with set variable in yaml file:
docker-compose build master
WARNING: The CI_ARTEFACTS_HOSTPATH variable is not set. Defaulting to a blank string.
Building master
... (cut log)
I am use:
CI_ARTEFACTS_HOSTPATH: '/ci'
CI_ARTEFACTS_HOSTPATH: "/ci"
- CI_ARTEFACTS_HOSTPATH=/ci
docker-compose --version
docker-compose version 1.24.0-rc1, build 0f3d4dda
docker-compose.yaml:
version: "3.4"
services:
master:
environment:
CI_ARTEFACTS_HOSTPATH: '/ci'
build:
context: master
network: host
image: buildbot-master
command: buildbot start --nodaemon master
ports:
- 8010:8010
- 9989:9989
volumes:
- ${CI_ARTEFACTS_HOSTPATH}:/ci/artefacts
@alexei-developer
Have you solved this issue? I have the same problem
This example may help if you would like to provide default value in docker-compose env:
Use "${ENV_VAR:-defaultValue}"
environment:
HASURA_GRAPHQL_DATABASE_URL: "${HASURA_GRAPHQL_DATABASE_URL:-postgres://postgres:@postgres:5432/postgres}"
Using .env
can override default url.
sudo service redis-server stop
-> sudo systemctl stop docker
->sudo systemctl start docker
Still have this issue, passing variables like this docker-compose --env-file /etc/.env up -d
and receiving Error while attempting to convert service.dev.ports.published to appropriate type
Most helpful comment
You need to escape the variable if you want it to be expanded inside the container, using a double-dollar sign (
$${ELK}
).If however, you want it to be interpreted on your host, the $ELK needs to be defined in your environment or in the
.env
file. Theenv_file
option defines environment variables that will be available inside the container only.