Introduction to Kubernetes use local docker image
Kubernetes use local docker image is nothing but create Kubernetes image locally and deploy the same on Kubernetes cluster locally; the first step is to deploy our application on Kubernetes to build the docker image. Next, we need to use minikube to run the Kubernetes in our local environment. Minikube is nothing but the lightweight implementation of Kubernetes which was used to create a virtual machine locally on our system and deploy our application on a cluster that contains a single node. As we can say that by default, minikube will pull the docker image from the repository of docker.
What is a Kubernetes local docker image?
- We can test the locally created docker image by using minikube. To test a locally created docker image with minikube, we need to tell minikube to refer it from our system.
- By default, minikube will fetch docker images from the registry. So to run a locally created docker image on Kubernetes, we need to install kubectl and minikube in our system. Kubectl is the command-line tool that was used for controlling the cluster of Kubernetes.
- The docker image represents the binary data that was encapsulated by the application and by using software dependencies.
- Docker image is executing the software bundles which were running server and making it well-defined assumption for an environment of runtime.
- We can also create a docker image for our application which can push into the registry before referring to the same in a pod. So as we know, the first step is to deploy our application on the Kubernetes cluster is to build the docker image.
- To deploy the Docker image on the Kubernetes cluster, we are using kubectl. We can also edit the existing deployment by using the kubectl edit command.
- For editing the docker image, we simply need to update the attribute for containers to save the deployment.
- We do not need to create a new pod; a new deployment will automatically create the new image with the new pod which was we have specified. Likewise, it will automatically terminate old pods which were we have used for the old image.
- Our Kubernetes cluster has access on the repository. Therefore, if we are running the Kubernetes cluster on the AWS environment, we can use the AWS IAM role to access the nodes and read the same from the repository. However, AWS is not providing an easy way to access the cluster from the outside environment.
How to use local docker image?
- There are various ways to use local docker images and deploy the same on the Kubernetes cluster.
- Create a local repository for the local docker image.
- Copy docker image for minikube environment of docker.
- Build the docker image in minikube itself.
- We need to install the minikube and virtual box in our local environment to use the local docker image.
- Docker image which was running on our machine which is different from docker daemon from minikube.
- Also, we need to use minikube to pull the docker image from the repository. There are different types of configured repositories available.
- To use the local docker image first, we need to pull the image into the repository. Without pulling images into the repository, we cannot use the same in Kubernetes.
How to run Kubernetes use local docker image?
The below steps show how to run a local docker image in Kubernetes are as follows.
- Start the minikube and virtual box –
To use the docker image in the local environment, we need to start the minikube and virtual box.
We can start the minikube and virtual box using the below command.
> minikube start --vm-driver = virtualbox
- Start the docker environment using minikube –
Once starting virtualbox and minikube next step is to create the docker environment using minikube.
As we see in the below snap minikube, the docker-env command creates global variables, which was required to start the daemon of the docker.
- Create deployment using kubectl –
After installing the virtual box and minikube next step is to build using docker.build command. After building the image next step is to create deployment using the kubectl command.
Kubectl create deployment kubernetes-dep –image = kubernetesimage
In the above example, we have provided the image name as Kubernetes image and provided the name of deployment as Kubernetes-dep.
- Start the dashboard –
Once the deployment is ready, start the dashboard using the following command are as follows.
After executing the below command, we can see that the dashboard is created and opened in the browser. After opening the dashboard in the browser, we can see that deployment is created, but it shows an error.
The error will suggest that minikube will be trying to access the image from the local repository. To remove this error, we need to edit the flag name as image-pull-policy as IfNotPresent or Never.
- Edit deployment –
In the below step, we have edited the deployment. We are changing the flag name as image-pull-policy from Always to Never.
kubectl edit deployment kubernetes-dep
- Check the pod status –
After deploying the changes on the Kubernetes cluster, we need to check all the pods are running without any error.
Where to store Docker images
The container of docker consists of volumes, images, and network settings. The storage location of the docker image depends on the operating system which was we have using.
Below is the default location of the docker image, which was used to store the image.
- Windows –
On Windows operating system, the docker image is stored in the default location as C:\ProgramData\DockerDesktop.
- MacOS –
On MacOS operating system docker image is stored in default location as ~/Library/Containers/com.docker.docker/Data/vms/0/
- Fedora –
On the Fedora operating system, the docker image is stored in the default location as /var/lib/docker/
- Ubuntu –
On the ubuntu operating system, the docker image is stored in the default location as /var/lib/docker/
- Debian –
On Debian operating system, the docker image is stored in the default location as /var/lib/docker/
The docker image represents the binary data that was encapsulated by the application and by using software dependencies. We can test the locally created docker image by using minikube. To test locally created docker image with minikube, we need to tell minikube to refer it from our system.
This is a guide to Kubernetes use local docker image. Here we discuss the various ways to use local docker images and deploy the same on the Kubernetes cluster. You may also have a look at the following articles to learn more –