How to Use Local Docker Images With Minikube ?
Minikube is a software that helps in the quick setup of a single-node Kubernetes cluster. It supports a Virtual Machine (VM) that runs over a docker container and creates a Kubernetes environment. Now minikube itself acts as an isolated container environment apart from the local docker environment, the local docker images on your computer are not accessible in minikube docker. Now When you are trying to access docker images in minikube it will be possible to access through the docker hub, not through the local docker environment. If the image is available on the docker hub then it is pulled in minikube docker from the docker hub otherwise you may get an Image FallBack Error. So to resolve this issue we will follow through different methods for loading Local Docker images into Minikube.
For the development of Kubernetes, Integrating local Docker images seamlessly with Minikube will be an effective practice. it helps developers develop better locally built docker containers within the Minikube k8s cluster to speed up testing and iterations. We will discuss the different effective methods of using locally created docker images in the Minikube.
Effective methods of using Local Docker Images within Minikube
The following are the different effective methods of pulling local docker images to the minikube:
-
Pushing directly to the in-cluster Docker daemon (docker-env)
-
Push images using the ‘cache’ command
-
Pushing to an in-cluster using Registry addon
-
Building images inside Minikube using SSH
-
Loading directly to in-cluster container runtime
-
Building images to in-cluster container runtime
Pushing Directly To The in-cluster Docker Daemon (docker-env)
Going through this method provides a straightforward approach to push local docker images seamlessly into Minikube’s in-cluster docker daemon. This method helps the developers efficiently test and deploy their local build containers within the environment.
Step 1
Setting up the shell environmental Variables
eval $(minikube docker-env)
This command points our terminal to use the docker daemon inside minikube. So now all the docker commands we will run will be run on docker in the minikube cluster and not on our host machine. So now we can directly build the docker image in our minikube docker.
Step 2
Building the Docker Image locally
docker build -t my_image .
Example: Suppose we have a basic nodejs app that we want to build in our minikube cluster. For the practice the node js application and Dockerfile are provided here , save them with names as Dockerfile and node.js.
# Here Using the official Node.js runtime as a parent image
FROM node:14
# Setting up the working directory in the container
WORKDIR /usr/src/app
# Copy package.json and package-lock.json to the working directory
COPY package*.json ./
# Installing the dependencies of the application
RUN npm install
# Copying the application source code to the working directory
COPY . .
# Expose the port on which the application will run
EXPOSE 3000
# Defining the command to run the application
CMD ["npm", "start"]
and node.js file is here
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello, Docker World!');
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
Step 3
Tag the Image with Minikube’s Docker Daemon IP and Port number
docker tag my_image:latest $(minikube ip):5000/my_image:latest
In the above command provide your can also perform replacing the $(minikube IP) with manual providing their.
Step 4
Push the tagged image directly into the Minikube’s in-cluster Docker deamon.
docker push $(minikube ip):5000/my_image:tag
Successfully the docker image is pushed into minikube’s in-cluster, you can check the image on the in-cluster docker images as a verification.
Step 5
To switch back to the local docker daemon use the following command
eval $(minikube docker-env -u)
Pushing Images Using Cache Command
The cache command helps in adding the locally created docker images to Minikube’s cache by simplifying the image management. This method facilitates a faster pace for testing and interactions in the k8s cluster.
Step 1
Adding the local docker image to minikube’s cache
minikube cache add my_image:latest
Step 2
Deploy the pod referencing the locally cached image
kubectl apply -f my_pod.yaml
Pushing To An In-Cluster Using Registry Addon
This method makes it easier to push and pull the docker images by enabling the Minikube ‘registry’ addon inside the cluster. It takes the responsibility of image management ensuring with central repository.
Step 1
Firstly enabling the ‘registry’ addon in Minikube
minikube addons enable registry
Step 2
Building the docker image locally
docker build -t my_image:latest Dockerfile
Here provide your image name, image version and your docker file name.
Step 3
Tag the docker image with the minikube registry’s address and port
docker tag my_image:latest localhost:5000/my_image:latest
Step 4
Push the tagged image to Minikube’s in-cluster registry
docker push localhost:5000/my_image:latest
Building Images Inside Minikube Using SSH
This method accesses the minikube VM directly through SSH, allowing the developers to build, tag, and push the docker images within the in-cluster environment providing control for effective image management.
Step 1
Firstly access the Minikube VM through ssh
minikube ssh
Step 2
Build the docker image within the Minikube VM
docker build -t my_image:latest Dockerfile
Step 3
Now tag the image with Minikube’s docker daemon IP and Port
docker tag my_image:latest localhost:5000/my_image:latest
Step 4
Push the tagged image directly into Minikube’s in-cluster
docker push localhost:5000/my_image:latest
Step 5
Exit from the Minikube VM as a final step
exit
Loading Directly to in-Cluster Container Runtime (Using Image Load Command)
This method provides convenient image management by saving and loading docker images as tarballs allowing developers to have effective integration with locally generated images into Minikube’s cluster container runtime.
minikube image load my_image
This command can be used when we are having the image built in the docker of our host machine but want to load it into the minikube cluster. First, check if my_image exists in the host machine docker images:
Since the image my-image already exists in our host machine docker we can load it directly into our minikube cluster by image load command.
Now check in minikube docker for the image
Here we have loaded the image successfully into Minikube.
Building images to in-cluster container runtime
This method directly imports the docker image tarballs into the minikube’s selected container runtime such as containerd. It allows the developer to tune the management of images to specific runtime preferences.
Step 1
Access the minikube VM through SSH
minikube ssh
Step 2
Import Docker images tarball into the chosen in-cluster container runtime ( Eg: containerd)
sudo ctr image import my_image.tar
Step 3
As a final step exit from the VM
exit
To know the difference between docker and containerd refer this article – Docker vs Containerd
Conclusion
Learning these methods for integrating local docker images into Minikube will be essential for the developers to fasten the Kubernetes development. These flexible methods facilitate in direct pushing of the local images to the in-cluster docker daemon, accelerating the Iterations and generating images directly within the Minikube environment providing streamlined workflows.