Home > Enterprise >  Docker: Trying to execute a bash command in .env for docker-compose but gives Invalid Template
Docker: Trying to execute a bash command in .env for docker-compose but gives Invalid Template

Time:10-12

I am trying to pass via the command parameter the value of an environment variable, this does work as I have tested it.

The problem is that the environment variable I really need is something like this

HOST_IP=$(ip route|awk '/default/ { print $3 }')

If I now "docker compose up", I get an error saying " Invalid Template" in the .env file.

I know about how you can map a DNS name to the IP of the host but this doesn't work for me, I need the real IP address so I am passing it in

command: []

As I say, I have done a test via passing the env variable and works great BUT I can't have the variable be the result of running a $() command - this does work in the bash shell directly.

The problem is I need the HOST IP but this is NOT guaranteed to be a specific address.

Can anyone help ?

Maybe the .env needs a different format ?

Thanks in advance

CodePudding user response:

It's not possible.

Maybe the .env needs a different format ?

No.

Use a script to export the variable and wrap docker-compose in it.

#!/bin/sh
# run-docker-compose.sh
export HOST_IP=$(ip route | awk '/default/ { print $3 }')
docker-compose "$@"
  • Related