Add hostname env to docker .yml file

Viewed 30

I'm trying to use my computer name as part of an address inside a docker file. I have an .env file with where it is supposed to call hostname. If I do an echo to the variable I can get the computer name but I can pass it to "traefik.http.routers.whoami.rule=Host(${HOST_HOSTNAME}.dd.dd.com)"

whoami:
image: containous/whoami
container_name: whoami
restart: ${RESTART}
labels:
  - "traefik.enable=true"
  # default route over https
  - "traefik.http.routers.whoami.rule=Host(`${HOST_HOSTNAME}.dd.dd.com`)"
  - "traefik.http.routers.whoami.entrypoints=https"
  - "traefik.http.routers.whoami.tls.certresolver=${PROVIDER}"
  # HTTP to HTTPS
  - "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https"
  - "traefik.http.routers.whoami-redirs.rule=hostregexp(`{host:.+}`)"
  - "traefik.http.routers.whoami-redirs.entrypoints=http"
  - "traefik.http.routers.whoami-redirs.middlewares=redirect-to-https"

Its any other way that I can invoke the computer name and use it to complete the address on the 8 code line?

This is what I've tried inside the .env file.

HOST_HOSTNAME=hostname
HOST_HOSTNAME='hostname'
set host=%COMPUTERNAME%
HOST_HOSTNAME=host
1 Answers

The environment variables that you are trying to access should be setted in your local environment. Try to run first an export to the variables, you can do something similar to this:

docker-compose.yml

version: '3.1'

services:
  whoami:
    image: containous/whoami
    labels:
      - "label_test=${VAR}"

Export the variable VAR

export VAR=foo

If you run and inspect the container you should see the label with the value

docker inspect root_whoami_1_6f9004197d63 --format '{{ index .Config.Labels "label_test"}}'
foo

You can view more information in the compose environment variable documentation https://docs.docker.com/compose/environment-variables/#substitute-environment-variables-in-compose-files

Related