Running Local Docker Images in Kubernetes

Definitely one of the easiest deployment of Kubernetes in the local environment is using the minikube.

One should first and furthermost understand that minikube is a virtual machine with docker engine installed. So docker engine running on your local machine(Linux/OSx) is having different docker daemon than that one installed in minikube.

Minikube strategy is to pull the docker images from the repository by default.  There are predefined default repositories configured, we will not go into that details. So when you run the following command for creating the pod it will fail with an error not able to pull the image from the repository.

kubectl run discovery –image=myproject/myimage –port=8761

This is because it is not able to find the repo. If you do ssh into minikube

minikube ssh

>docker images

Then you will notice that there is no image which you are trying to run.

There are lots of solution to this problem like creating the repo and pushing the image to that, copying the image from local to minikube etc. But the easiest solution is to build the image into minikube itself.

Follow the steps to do that:

First, note that when you execute the command docker images from your local prompt it will display the images present in your local docker environment.

Now we point local docker environment to minikube using the following command

eval $(minikube docker-env)

Now if you execute the following command from your terminal

docker images

It will show the images in minikube and not from your local docker environment.

Finally, build the image from your terminal

docker build -t myproject/myimage .

The image will get build into minikube and not in your local docker environment. Verify from the following command

minikube ssh

>docker images

If you execute the following command now

kubectl run discovery –image=myproject/myimage –port=8761

Still, it will fail with the same problem because kubernetes by default work on the pull strategy and we need to disable the same by executing the following command:

kubectl run discovery –image=myproject/myimage –image-pull-policy=Never –port=8761

Now if you inspect the pods

kubectl get pods

It will show that it is successfully running.


2 thoughts on “Running Local Docker Images in Kubernetes

  1. Thank you very much for this. It´s been the most useful resource to understand why I could not deploy the image I had just snapshot on Docker.

    Like

  2. Thank you very much for the perfect explanation. Eventually, after much search on Internet, someone is explaining the problem well. Thank you very much! Much appreciated

    Like

Leave a comment