Skip to main content
person working on Windows 11 computer

Fix “Error initializing Docker client: protocol not available” on Terraform for Windows with WSL2

This is a brief post on how I solved a problem using the kreuzwerker/docker Terraform provider on a Windows 11 laptop using WSL2 for Docker.

I’ve been following an excellent tutorial on Udemy, More than Certified in Terraform, but the examples are presented on a Unix based OS (the tutor uses AWS Cloud9). To follow along I setup a local project on my Windows 11 laptop which uses WSL2 support for Docker.

I copied the example text from the provider docs:

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

provider "docker" {
  host = "unix:///var/run/docker.sock"
}

# Pulls the image
resource "docker_image" "ubuntu" {
  name = "ubuntu:latest"
}

# Create a container
resource "docker_container" "foo" {
  image = docker_image.ubuntu.latest
  name  = "foo"
}

Then I used the terraform init command – it worked as expected, downloading the kreuzwerker/docker Terraform provider.

However, when I used the terraform plan command I received the following error:

PS C:\Training\Udemy\Terraform\terraform\terraform-docker> terraform plan

 Error: Error initializing Docker client: protocol not available

   with provider["registry.terraform.io/kreuzwerker/docker"],
   on main.tf line 10, in provider "docker":
   10: provider "docker" {


PS C:\Training\Udemy\Terraform\terraform\terraform-docker>

I tried a number of alternative solutions – no host argument (as in the tutorial examples), or variations of tcp://your-host-ip:2376/ or tcp://localhost:2376/ or tcp://localhost:2375/ and many more…

The Fix

I assumed, correctly it turns out, that the problem was the host item for the docker provider:

provider "docker" {
  # This didn't work for me!
  host = "unix:///var/run/docker.sock"
}

Googling, as you do, I found many alternative variations but the answer for me was in the Install Terraform documentation for Windows:

provider "docker" {
  # This DID work on Windows 11 with WSL2!
  host    = "npipe:////.//pipe//docker_engine"
}

Clearly, different options work for different situations, and you may find something else works for you.

Leave a comment to let me know what worked for you.

Tags: , , ,
Posted in Docker

2 comments on “Fix “Error initializing Docker client: protocol not available” on Terraform for Windows with WSL2”

  1. Prince Nsidibe says:

    This worked for me. Thanks
    1
    2
    3
    4
    provider “docker” {
    # This DID work on Windows 11 with WSL2!
    host = “npipe:////.//pipe//docker_engine”
    }

  2. MK says:

    Thank you – host = “npipe:////.//pipe//docker_engine” worked for me – regards

Leave a Reply

Your email address will not be published. Required fields are marked *

*