skip to Main Content

main.tf

terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
    }
    google = {
      source = "hashicorp/google"
    }
    random = {
      source = "hashicorp/random"
    }
  }
}

provider "docker" {}

resource "docker_image" "nginx" {
  name         = "nginx:latest"
  keep_locally = false
}

resource "docker_container" "nginx" {
  image = docker_image.nginx.latest
  name  = "tutorial"
  ports {
    internal = 80
    external = 8000
  }
}

Upon executing terraform init I face issue with downloading only docker resource:

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/google...
- Finding latest version of hashicorp/random...
- Finding latest version of kreuzwerker/docker...
- Installing hashicorp/google v4.11.0...
- Installed hashicorp/google v4.11.0 (signed by HashiCorp)
- Installing hashicorp/random v3.1.0...
- Installed hashicorp/random v3.1.0 (signed by HashiCorp)
╷
│ Error: Failed to install provider
│ 
│ Error while installing kreuzwerker/docker v2.16.0: could not query provider registry for registry.terraform.io/kreuzwerker/docker: failed to retrieve authentication checksums for
│ provider: the request failed after 2 attempts, please try again later: Get
│ "https://github.com/kreuzwerker/terraform-provider-docker/releases/download/v2.16.0/terraform-provider-docker_2.16.0_SHA256SUMS": context deadline exceeded

After following this post I downloaded the file in local and I get to perform terraform init successfully but failed to run terraform apply with below error:

│ Error: Could not load plugin
│ 
│ 
│ Plugin reinitialization required. Please run "terraform init".
│ 
│ Plugins are external binaries that Terraform uses to access and manipulate
│ resources. The configuration provided requires plugins which can't be located,
│ don't satisfy the version constraints, or are otherwise incompatible.
│ 
│ Terraform automatically discovers provider requirements from your
│ configuration, including providers used in child modules. To see the
│ requirements and constraints, run "terraform providers".
│ 
│ failed to instantiate provider "registry.terraform.io/kreuzwerker/docker" to obtain schema: fork/exec
│ .terraform/providers/registry.terraform.io/kreuzwerker/docker/2.16.0/linux_amd64/terraform-provider-docker_2.16.0_linux_amd64.zip: permission denied

System Details:

OS: Ubuntu 21.10

Terraform versions tried:

  • 1.0.6 [same version used in tutorial cli at https://learn.hashicorp.com/]

  • 1.1.16 [using apt-get]

I have also launched another docker container and reproduce the issue and I was able to reproduce the same issue with terraform init



Update:

gahan@jarvis:~/devOps/test$ terraform init

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/random...
- Finding latest version of kreuzwerker/docker...
- Finding latest version of hashicorp/google...
- Installing hashicorp/google v4.11.0...
- Installed hashicorp/google v4.11.0 (signed by HashiCorp)
- Installing hashicorp/random v3.1.0...
- Installed hashicorp/random v3.1.0 (signed by HashiCorp)
╷
│ Error: Failed to install provider
│ 
│ Error while installing kreuzwerker/docker v2.16.0: could not query provider registry for registry.terraform.io/kreuzwerker/docker: failed to retrieve authentication checksums for provider: the request
│ failed after 2 attempts, please try again later: Get "https://github.com/kreuzwerker/terraform-provider-docker/releases/download/v2.16.0/terraform-provider-docker_2.16.0_SHA256SUMS": net/http: request
│ canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
╵

gahan@jarvis:~/devOps/test$ cat /etc/group | grep docker
docker:x:998:gahan

gahan@jarvis:~/devOps/test$ docker pull python:alpine3.13
alpine3.13: Pulling from library/python
5758d4e389a3: Pull complete 
9292b3ab1647: Pull complete 
35d95eb0acaf: Pull complete 
cfda6539f3f2: Pull complete 
f4471b8ea909: Pull complete 
Digest: sha256:93eb0ba98b15791a071ec8bce2483e670e52c83af51962d3255b4f8f93b52d24
Status: Downloaded newer image for python:alpine3.13
docker.io/library/python:alpine3.13

2

Answers


  1. Chosen as BEST ANSWER

    Thanks a lot for help here @Tapan and other community members.

    Since I followed all the steps of post installation steps, re-iterated permissions as well..

    In the end I also created a docker container to recreate issue and on further debugging it turns out that even though with wget command reported checksum can be downloaded, while working on terraform it might be using some other url/protocol which I am not yet aware of but it somehow conflicting with my ISP [Airtel fiber connection] .

    As result I used warp desktop client and tried to use the terraform command which worked for me without any issue.


  2. @Gahan, I guess issue is with permissions, if i am not wrong, docker needs sudo access to run, where terraform works with normal user access. If you look at below line in error

    .terraform/providers/registry.terraform.io/kreuzwerker/docker/2.16.0/linux_amd64/terraform-provider-docker_2.16.0_linux_amd64.zip: permission denied
    

    It is showing "Permission denied".

    I guess you can try changing user+group from sudo to your local user for .terraform directory and give a try.

    chown <user>:<group> .terraform
    chmod 750 .terraform/providers/registry.terraform.io/kreuzwerker/docker/2.16.0/linux_amd64/terraform-provider-docker_2.16.0_linux_amd64.zip
    

    Update :

    This setup is working fine with MacOS, I just tried to create ngnix container in docker using above main.tf contents and I am able to initialize and apply terraform successfully. I am using Mac OS. Terraform version – 1.1.1, Docker version – 4.4.2

    Next, I tried to replicate same on ubuntu machine (20.04 LTS) and encountered similar issue as above when I tried to run "terraform apply"

    Error:

     Error: Error pinging Docker server: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/_ping": dial unix /var/run/docker.sock: connect: permission denied
    

    On further digging I was able to resolve it.

    The main cause of issue is our custom user doesn’t have access to execute the commands of docker. So when terraform tries to run docker related commands, this fails. We can execute below steps to tackle this

    1. There should be a docker group available if you installed docker correctly. This you can check in /etc/group path
    $ cat /etc/group
    
    1. Add your userid to docker group. In my case username is ‘tapan1991’
    $ sudo usermod -aG docker $User_Name
    
    Eg : sudo usermod -aG docker tapan1991
    
    1. Logout from the session and login again

    2. Execute terraform plan/apply command

    $ terraform apply
    

    Hope this helps!!

    Reference : Error pinging docker server on "terraform apply"

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search