Set extra host in environment variables

I created a file .env, added single line HOST=test.example.com, then did this in docker-compose:

extra_hosts:
- myip:${HOST}

docker-compose config then shows

  extra_hosts:
      myip: test.example.com

To do this I followed the documentation from Docker-compose environment variables the section about .env file

UPDATE

According to the Docker documentation,

Note: If your service specifies a build option, variables defined in environment files will not be automatically visible during the build. Use the args sub-option of build to define build-time environment variables.

It basically means if you place your variables in .env file, you can use them for substitution in docker-compose.yml, but if you use env_file option for the particular container, you can only see the variables inside the Docker container, not during the build. It is also logical, env_file replaces docker run --env-file=FILE ... and nothing else.

So, you can only place your values into .env. Alternatively, as William described, you can use host's environment variables.