How use a "calculated" variable in a docker-compose.yml or .env file

Question :

Can I use a "result of a bash command" as env variables in docker-compose.yml file ?

Can I "execute a command" and put the result in env variables during the "docker-compose up" ?

The use Case :

I work with docker & VScode in windows WSL2, and I want send "WSL2 internal IP" to my container to use Xdebug (which need a "remote_host").

The problem are the "WSL2 internal IP" can change on each reboot of the PC.

The commande used :

ip addr show eth0 | grep -oP '(?<=inets)d+(.d+){3}'

Where I am

I have tested to put the command in .env file

MY_WSL_IP=$(ip addr show eth0 | grep -oP '(?<=inets)d+(.d+){3}')

and the docker-compose.yml file

  version: "3"
        container_name: lamp_test
        image: eurelis/aws-ami-php7:php73
        env_file: ./drupal.env
          - WSL_HOST_IP=${MY_WSL_IP}

In this case, I get the whole command in the container.


When in the terminal il put play the two command in the terminal, that’s work

export MY_WSL_IP=$(ip addr show eth0 | grep -oP '(?<=inets)d+(.d+){3}')
docker-compose -f etc/docker/docker-compose.yml up -d

But there is not the "use case" because variable are created "out side" the docker-compose configuration.
When using the VScode’s docker plugin, I can’t inject this "result"

I don’t have found a proper way to inject the value in the container, nor a explicit "that’s not possible".

If someone has a tips

Source: Docker Questions