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?
Ensure Docker CLI tools are installed under /usr/local/bin and Set /var/run/docker.sock which some third-party clients such as terrafor may use to communicate with Docker

Reference: https://docs.docker.com/desktop/mac/permission-requirements/
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"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With