Running Local Docker Images in Kubernetes

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 understand that minikube is a virtual machine with the Docker engine installed. Docker engine running on your local machine has a different Docker daemon than the one installed in minikube.

Minikube’s strategy is to pull the Docker images from the repository by default. There are predefined default repositories configured, but we will not go into those details. When you run the following command for creating the pod, it will fail with an error because it will not be 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 for you 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.

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 into your local Docker environment. Verify this with the following command:

minikube ssh

>docker images

If you execute the following command now:

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

It will still fail with the same problem because Kubernetes, by default, works on the pull strategy and we need to disable it 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.

from DZone Cloud Zone

Sharing is caring!

Comments are closed.