lizminim

忘れていても思い出せるように。

terraform: Error: Error pinging Docker server: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? の対処例。

terraform tutorialでエラー発生。環境はwin10+ubuntu18.04(wsl2)

 learn-terraform-docker-container % terraform apply
╷
│ Error: Error pinging Docker server: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
│
│   with provider["registry.terraform.io/kreuzwerker/docker"],
│   on main.tf line 10, in provider "docker":
│   10: provider "docker" {}
│
╵

docker desktopが起動するためdocker自体は起動しているはずでは?と思っていたが、dockerコマンドを叩くと、

[05:35:18]kilin@DESKTOP-8H0PBKG ~/learn-terraform-docker-container $ docker context ls

The command 'docker' could not be found in this WSL 2 distro.
We recommend to activate the WSL integration in Docker Desktop settings.

For details about using Docker Desktop with WSL 2, visit:

https://docs.docker.com/go/wsl2/

[05:35:29]kilin@DESKTOP-8H0PBKG ~/learn-terraform-docker-container $

と返される。どうやらWSL2でdockerが設定されていない様子。 以下の記事で設定を確認。

docs.docker.com

私の環境の場合、以下の画像箇所でintegrationが有効になっていなかった。

こちらをonにして確認。

[05:35:29]kilin@DESKTOP-8H0PBKG ~/learn-terraform-docker-container $ docker context ls
NAME                TYPE                DESCRIPTION                               DOCKER ENDPOINT                             KUBERNETES ENDPOINT   ORCHESTRATOR
default *           moby                Current DOCKER_HOST based configuration   unix:///var/run/docker.sock
desktop-linux       moby                                                          npipe:////./pipe/dockerDesktopLinuxEngine

OK.

terraform tutorialの続き。

developer.hashicorp.com

terraform init terraform apply => yes

OK.

-- 余談: dockerをintegrationしたのが原因なのか、もともと持っていたコンテナたちが一斉に起動してしまった。不思議だ。