terraformterraform-provider-docker

How to reference Terraform Environment variable in providers?


I have a gitlab pipeline that runs some terraform, so say I have a pipeline that declares

Plan (Example):
  variables:
    TF_VAR_myenvvariable: /home/$example

So now inside my Terraform Project under providers.tf

I have

provider "docker" {
...

    registry_auth {
        address     = "example.com"
        config_file = "${TF_VAR_myenvvariable}/docker.json"
    }
}

How do I get this to work? It's not working. I can't use the var.myenvvariable because you can't use variables in the providers block.


Solution

  • There are at least 2 docker providers in the Terraform registry that seem compatible to your provider configuration:

    You haven't specified which one you're using. Given these are very similar, they probably work exactly the same way.

    Many Terraform providers allow you to omit properties and use environment variables instead. For example, when using calxus/docker provider you can set the DOCKER_HOST instead of setting the config_file property.

    So you can configure your gitlab pipeline as follows:

    Plan (Example):
      variables:
        DOCKER_HOST: /home/$example
    

    Provider configuration:

    provider "docker" {
      # ...
    
      registry_auth {
        address = "example.com"
      }
    }
    

    Or, as an alternative, use username and password:

    Plan (Example):
      variables:
        DOCKER_REGISTRY_USER: myuser
        DOCKER_REGISTRY_PASS: mypassword
    

    Provider configuration:

    provider "docker" {
      # ...
    
      registry_auth {
        address = "example.com"
      }
    }