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.tf
file:
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
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.
cat /etc/group
–> There should be a docker group available if you installed docker correctly.sudo usermod -aG docker $User_Name
docker run hello-world
–> This should run error free now.Now try to apply Terraform again and everything will work.
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.
I had to reboot. In Linux.
systemctl restart docker
wasn’t enough. Logging out and in again to Gnome wasn’t enough. Just reboot.You can find your Docker Socket using the following command:
And then update your provider block accordingly with the socket address