0

I have built my docker image in Docker desktop but I do not know how to config so that terraform kubernetes can refer to local image? (it stuck while creating the pod)

Here is my tf file look like

....
provider "kubernetes" {
  config_path = "~/.kube/config"
}

resource "kubernetes_pod" "test" {
  metadata {
    name = "backend-api"
    labels = {
      app = "MyNodeJsApp"
    }
  }
  spec {
    container {
      image = "backendnodejs:0.0.1"
      name = "backendnodejs-container"
      # I think it keep pulling from Docker Hub 
      port {
        container_port = 5000
      }
    }
  }
}

resource "kubernetes_service" "test" {
  metadata {
    name = "backendnodejs-service"
  }

  spec {
    selector = {
      app = kubernetes_pod.test.metadata.0.labels.app
    }

    port {
      port = 5000
      target_port = 5000
    }

    type = "LoadBalancer"
  }
}
  • Where is your Kubernetes cluster running? Usually you'll need to push the image to a container registry (like Docker Hub) so that the Kubernetes node can pull the image. – derkoe Aug 06 '22 at 14:12
  • I downloaded minikube to my windows machine and use it locally. Is there a way to avoid container registry? – Babyface_Developer Aug 06 '22 at 14:14
  • 1
    You can push the image directly to the minikube Docker registry - see https://shashanksrivastava.medium.com/how-to-set-up-minikube-to-use-your-local-docker-registry-10a5b564883 – derkoe Aug 06 '22 at 14:16

1 Answers1

1

So after hours researching how to deploy to minikube (installed on minikube website not docker desktop kubernetes). I found out that minikube ran as container itself in docker desktop which is why you can not use images from docker desktop to deploy into minikube.

Here are the link solved about this:

So things you will need before using terraform deploy to minikube:

  1. Pushing directly to the in-cluster Docker daemon (docker-env)

    • Windows
      • Powershell
        PS> & minikube -p minikube docker-env --shell powershell | Invoke-Expression
        
      • CMD
        CMD> @FOR /f "tokens=*" %i IN ('minikube -p minikube docker-env --shell cmd') DO @%i
        
    • Linux/MacOS
      > eval $(minikube docker-env)
      
  2. Build docker image again (same terminal that been entered command above)

    docker build -t your_image_tag your_docker_file
    
  3. Run normal terraform file (same terminal)

This link also explained same as above