Over a million developers have joined DZone.

Running Local Docker Images in Kubernetes

DZone 's Guide to

Running Local Docker Images in Kubernetes

This short tutorial gives you step-by-step instructions on how to run your local Docker images on Kubernetes using minikube.

· Cloud Zone ·
Free Resource

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.

kubernetes ,cloud ,docker ,local docker ,minikube

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}