Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Use Environment Variables in a .env file to fill out other environment variable in the same .env file

I am using a base.env as an env_file for several of my docker services.In this base.env I have several parts of the environment variable that repeat throughout the file. For example, port and ip are the same for three different environment variables. I would like to specify these in an environment variable and reuse those variables to fill out the other environment variables.

Here is base.env:

### Kafka
# kafka's port is 9092 by default in the docker-compose file
KAFKA_PORT_NUMBER=9092
KAFKA_TOPIC=some-topic
KAFKA_IP=kafka
KAFKA_CONN: //$KAFKA_IP:$KAFKA_PORT_NUMBER/$KAFKA_TOPIC
# kafka topic that is to be created. Note that ':1:3' should remain the same.
KAFKA_CREATE_TOPICS=$KAFKA_TOPIC:1:3
# the url for connecting to kafka
KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://$KAFKA_IP:$KAFKA_PORT_NUMBER

I have tried writing

KAFKA_CONN: //$${KAFKA_IP}:$${KAFKA_PORT_NUMBER}/$${KAFKA_TOPIC}

in the environment section of the appropriate service in the docker-compose.yml, but this gets interpreted as a literal string in the container.

Is there a way to do what I want in the base.env file? Thank you for your help!

like image 254
Peter Kaufman Avatar asked Jul 06 '18 15:07

Peter Kaufman


2 Answers

You can actually do it like this (at least in vlucas/dotenv package (php), not sure about others, please check it yourself)

MAIL_NAME=${MAIL_FROM}

Read more about it here

like image 79
Saud Qureshi Avatar answered Oct 11 '22 19:10

Saud Qureshi


There is no way to do this in an env_file since it is not run as a bash command. This means that the variable is not created and then concatenated into the next variable it appears in. The values are just read in as they are in the env_file.

like image 26
Peter Kaufman Avatar answered Oct 11 '22 20:10

Peter Kaufman