skip to Main Content

I am doing the terraform tutorial and reach the step to execute terraform apply.

After executing that command I get this error:

WARNING: cgroup v2 is not fully supported yet, proceeding with partial confinement

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 main.tf line 9, in provider "docker":
   9: provider "docker" {

This is what I have in my configuration main.tffile:

terraform {
  required_providers {
    docker = {
      source = "kreuzwerker/docker"
    }
  }
}

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
  }
}

I have tried adding host = "unix:///var/run/docker.sock" in the provider function but still get that error. I have docker and NGINX configured in my pc too.

Does anyone know what is causing it?

4

Answers


  1. When you run docker run hello-world with your user id you will see the same error that you are getting.
    This is happening because your user doesn’t have access to execute the commands of docker. Please do the following steps.

    1. cat /etc/group –> There should be a docker group available if you installed docker correctly.
    2. Add your userid to docker group sudo usermod -aG docker $User_Name
    3. Logout from the session and login again
    4. docker run hello-world –> This should run error free now.

    Now try to apply Terraform again and everything will work.

    Login or Signup to reply.
  2. If docker ps command is successful then it means that the default host that provider of terraform uses is not correct.

    Turning off Docker Desktop and writing docker ps shown me the path that it is looking for.

    Login or Signup to reply.
  3. I had to reboot. In Linux. systemctl restart docker wasn’t enough. Logging out and in again to Gnome wasn’t enough. Just reboot.

    Login or Signup to reply.
  4. You can find your Docker Socket using the following command:

    docker context ls

    And then update your provider block accordingly with the socket address

    provider "docker" {
      host = "unix:///home/rehman/.docker/desktop/docker.sock"
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search