# Cannot connect to docker daemon

**URL:** <https://discuss.hashicorp.com/t/cannot-connect-to-docker-daemon/34122>\
**Category:** Terraform\
**Tags:** hcp-terraform\
**Created:** [January 11, 2022, 7:34pm UTC](https://discuss.hashicorp.com/t/cannot-connect-to-docker-daemon/34122 "2022-01-11T19:34:39Z")\
**Posts on this page:** 1\
**Showing post:** 11

<div class="post-metadata">

**Author:** ![bettkipkemoi](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/bettkipkemoi/32/42433_2.png) [@bettkipkemoi](https://discuss.hashicorp.com/u/bettkipkemoi)\
**Post date:** [July 27, 2024, 12:34am UTC](https://discuss.hashicorp.com/t/cannot-connect-to-docker-daemon/34122/11 "2024-07-27T00:34:23Z")

</div>

I had the same problem but finally managed to fix. Before running `terraform apply`, check where the docker daemon is running using `docker context ls`. The default option may not work, thus the error most people get. Pick the one with \*. Then go to the file \<main.tf\>, edit it to reflect the host as below:

```auto
terraform {
  required_providers {
    docker = {
      source = "kreuzwerker/docker"
      version = "~> 3.0.1"
    }
  }
}

provider "docker" {
  host = "unix:///Users/bett/.docker/run/docker.sock" //specifying where the docker daemon is running
}

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

resource "docker_container" "nginx" {
  image = docker_image.nginx.image_id
  name = "tutorial"

  ports {
    internal = 80
    external = 8000
  }
}

```

Sorry for the poorly formatted explanation, not familiar with code formatting here.

---

_[View the full topic](https://discuss.hashicorp.com/t/cannot-connect-to-docker-daemon/34122)._
