Reputation: 153
I'm setting up a new docker-based server and I'm writing a script to run
$ docker-compose
for all my docker-compose.yml
files at once.
I know about existence of .env
files and I know about env_file
option in docker-compose.yml
, however the .env
files are ignored when I launch
$ docker-compose up
from other directory and not even full path in env_file
helps.
my_script.sh:
#!/bin/bash
docker-compose up -f /server1/docker-compose.yml -f /server2/docker-compose.yml -f /server3/docker-compose.yml
Docker-compose.yml:
version: '3'
services:
service_name:
build: /server1/apache
env_file: /server1/.env
volumes:
- /web/${ROOT_DOMAIN}/web/:/var/www/:rw
.env
ROOT_DOMAIN=server1.domain.tld
I want to replace the ${ROOT_DOMAIN}
variable in volumes for the variable defined in .env
file. This works like a charm, when I run
$ docker-compose up
from the /server1/
folder. However when I run my script, which is in other directory, the enviroment variables are ignored and default to null.
How to fix this?
Upvotes: 5
Views: 9985
Reputation: 3806
Using flag didn't work for me, but since I'm using node I created a script which copies the .env file across before running it.
If you aren't using node you could run a bash script which essentially does the same thing.
"scripts":
{
"build_postgres":"cp .env node-app/pg/.env && cd node-app/pg && docker-compose up",
}
Warning
Just be sure to exclude the .env file from your .gitignore in case of sensitive information ending up in git repo.
.gitignore
node-app/pg/.env
note:
Be sure your .env file variables are uppercase and values
PG_USER=myuser
Upvotes: 0
Reputation: 370
You can use the flag --env-file to set the path where the .env file is located, example:
docker-compose --env-file ./config/.env.dev up
Official Docker documentation: https://docs.docker.com/compose/environment-variables/
Upvotes: 3
Reputation: 30684
You can roll your own:
function execute() {
(set -a; source .env; cat $1 | envsubst | kubectl apply -f -)
}
execute foo.yaml
And
$ cat .env
FOO=123
BAR=abc
$ cat foo.yaml
{
"myfoo": $FOO,
"mybar": $BAR
}
Upvotes: 0
Reputation: 5750
Sorry to say, but this cannot be fixed. The provided .env
file must be in the same directory.
This is a requirement of docker compose and is described in the docs (first paragraph): https://docs.docker.com/compose/env-file/
Upvotes: 8