Reputation: 12906
Though having read the Docker documentation about environment variables I have some trouble understanding the variable substitution.
This is my current docker-compose.yml
:
version: '3'
services:
web:
image: myimage:latest
environment:
FRONTEND_URL: http://mydomain
CALLBACK_URL: ${FRONTEND_URL}/callback
My understanding so far is that I can use something like ${FRONTEND_URL}
so that CALLBACK_URL
will be interpolated to http://mydomain/callback
, but after a docker-compose up
this service has the following environment values:
FRONTEND_URL: http://mydomain
CALLBACK_URL: /callback
So it looks as if ${FRONTEND_URL}
is not substituted. What am I missing here?
Upvotes: 3
Views: 4180
Reputation: 43594
You can use a .env
file on the same folder a docker-compose.yml
.
.env
FRONTEND_URL=http://mydomain
docker-compose.yml
version: '3'
services:
web:
image: myimage:latest
environment:
FRONTEND_URL: ${FRONTEND_URL}
CALLBACK_URL: ${FRONTEND_URL}/callback
Instead of using the .env
file you can use export
. Run this command before docker-compose up
:
export FRONTEND_URL=http://mydomain
Now you can use this docker-compose.yml
:
version: '3'
services:
web:
image: myimage:latest
environment:
FRONTEND_URL: ${FRONTEND_URL}
CALLBACK_URL: ${FRONTEND_URL}/callback
If an environment variable is not set, Compose substitutes with an empty string. In the example above, if
POSTGRES_VERSION
is not set, the value for the image option ispostgres:
.You can set default values for environment variables using a
.env
file, which Compose automatically looks for. Values set in the shell environment override those set in the.env
file.source: https://docs.docker.com/compose/compose-file/#variable-substitution
Upvotes: 3