Skip to main content

How to run locally build docker images with Kubernetes

To run Kubernetes in your local environment, Minikube is your choice. Minikube is a lightweight Kubernetes implementation that creates a Virtual Machine on your local machine and deploys a simple cluster containing only one node. By default, Minikube will always pull the docker images from the docker repository. To test locally build docker images with Minikube, you got to tell Minikube to refer them from your local system, instead of fetching from the docker registry. There are various ways to tell Minikube to look for local docker images. In this post, we’ll see how to run locally build docker images with Kubernetes.

How to run locally build docker images with Kubernetes

Recently, for one of my .NET Core based project, the deployment model was to containerize the app and deploy it on Kubernetes. Therefore, to test the locally created docker image on Kubernetes environment, I installed Minikube and Kubectl (command-line tool for controlling Kubernetes clusters) on my Windows development machine. After doing some research I learnt that there are various ways to run locally build docker images with Kubernetes. Like,

  1. Creating a local repository for docker images
  2. Copying the docker image to Minikube docker environment
  3. Building the docker image into Minikube itself.

To start, I found the 3rd method to be simple and quick to begin with. But it wasn’t a smooth road. Please note, I am running Minikube on a Windows machine.

First, to start the Minikube and the virtual box, run the following command.

minikube.exe start --vm-driver=virtualbox

Once it is completed, start the docker environment on the Minikube. To do that, run the following command.

minikube docker-env

This would print some global variables required to start the docker daemon. If you are on Linux or Mac then you can run eval $(minikube docker-env) command to make those settings. But eval command doesn’t work on windows. On windows, the equivalent command is,

@FOR /f "tokens=*" %i IN ('minikube -p minikube docker-env') DO @%i

Once it is completed, the docker environment on the virtual box is ready to use. You can now build your image using docker build command. Once your image is built, let’s create the deployment using kubectl. The following command will deploy your docker image on Kubernetes.

kubectl create deployment kubedemo --image=dummyimage

How to run locally build docker images with Kubernetes

For demonstration, I have provided some dummy names for deployment and docker image. Here, kubedemo is the name of the deployment and dummyimage is the name of the docker image. In an actual environment, you should provide a more meaningful name. Once the deployment is done, let’s go to the Kubernetes dashboard to verify the deployment. To start the dashboard, run minikube dashboard command. This command once completed, launch the browser with Kubernetes dashboard loaded.

On the dashboard, you can see that deployment is created, but it’s not working as expected. If you go to Pods menu, then you can see the Pods are not up and you will see the following errors in the Pods event. The error says, “Failed to pull image “dummyimage”: rpc error: code = Unknown desc = Error response from daemon: Error: ErrImagePull.”.

How to run locally build docker images with Kubernetes

This suggests that Minikube is trying to access the image from docker registry, where the image is present in the local docker daemon. If you google about this error, then the answer would be to add a flag called image-pull-policy and set its value to either “Never” or “IfNotPresent” while creating the deployment. This tells Minikube to refer to the locally build docker image. When I tried the same, I got an error “unknown flag: –image-pull-policy”. Probably this is working on Linux or Mac machine, but definitely not working on Windows machine.

How to run locally build docker images with Kubernetes

Even if you go through the help section of this command as suggested, you won’t find this flag as an option.

Here, the fix seems correct, but how do I update the value of this flag? After spending some time, I got my answer. You can set this flag by editing the already created deployment. You can edit the deployment either via the command line or via the Dashboard.

kubectl edit deployment kubedemo

Or,
How to run locally build docker images with Kubernetes
When you edit the deployment, it will open the deployment configuration .yaml file. In this file, look for the flag image-pull-policy. The current value of this flag is “Always”. That tells Minikube to get the docker images from the docker registry. We need to set its value to either “Never” or “IfNotPresent“.

(When command line tools is used)
How to run locally build docker images with Kubernetes

(When Edit is done from the dashboard)
How to run locally build docker images with Kubernetes

After updating the value, save the changes and close it. After a few seconds, you will see that deployment is successfully completed and pods are running without any errors.

How to run locally build docker images with Kubernetes

Summary

Minikube is your choice for creating Kubernetes environment on your local development system. By default, it will try to fetch the docker image from the docker registry. To test your locally build docker image with Minikube, you need to set the flag image-pull-policy to “Never” or “IfNotPresent”. You won’t find this flag while creating the deployment (at least on Windows machines). You need to edit the deployment and update the YAML configuration file and update this flag value. Once updated, your deployment will run successfully.

Thank you for reading. Keep visiting this blog and share this in your network. Please put your thoughts and feedback in the comments section.

PS: If you found this content valuable and want to return the favour, then Buy Me A Coffee

2 thoughts to “How to run locally build docker images with Kubernetes”

Leave a Reply

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