Using Local Docker Images with Minikube in Kubernetes
To use local Docker images with Minikube in Kubernetes, we can follow some easy methods. One good way is to build our Docker images right in the Minikube environment. This makes sure they work well and are easy to deploy. We can also load our existing local images into Minikube. We can set up Minikube to use the local Docker daemon. Another option is to push images to the Minikube registry for easier access.
In this article, we will talk about different ways to use local Docker images in Minikube. This will help us have a smooth time developing in Kubernetes. We will cover these topics:
- Using Local Docker Images with Minikube in Kubernetes
- Building Local Docker Images in Minikube Environment
- Loading Local Docker Images into Minikube
- Using Minikube Docker Daemon for Local Images
- Pushing Local Images to Minikube Registry
- Configuring Minikube to Use Local Docker Images
- Frequently Asked Questions
This guide will help us add local Docker images into our Kubernetes workflow with Minikube easily.
Building Local Docker Images in Minikube Environment
To build local Docker images in Minikube, we can use Minikube’s Docker daemon. This helps us create Docker images right inside Minikube. We do not need to push images to an outside registry. Let us follow these steps.
Start Minikube: First, we need to start Minikube. If we have not done this yet, we can use this command:
minikube startSet Docker Environment: Next, we need to set our shell to use the Docker daemon inside Minikube:
eval $(minikube docker-env)Build Your Docker Image: Now we go to the directory with our Dockerfile. We will build the image using the
docker buildcommand. For example:docker build -t my-local-image:latest .Verify the Image: To check if our image built well in Minikube’s Docker environment, we run:
docker imagesUse the Image in a Pod: We can now use our new image in a Kubernetes deployment or pod. Here is a sample deployment YAML:
apiVersion: apps/v1 kind: Deployment metadata: name: my-app spec: replicas: 1 selector: matchLabels: app: my-app template: metadata: labels: app: my-app spec: containers: - name: my-app image: my-local-image:latest ports: - containerPort: 8080Deploy the Application: We apply the deployment with:
kubectl apply -f deployment.yamlCheck Deployment Status: We need to make sure our deployment is running well:
kubectl get pods
By following these steps, we can build local Docker images in our Minikube environment. Then we can deploy them as part of our Kubernetes workloads.
Loading Local Docker Images into Minikube
We need to load local Docker images into Minikube to use them. This step helps our Kubernetes pods to access and use the images we built on our machines. Let us follow these steps to load local Docker images into Minikube.
Step 1: Build Your Docker Image
First, we build our Docker image using Docker CLI. For example:
docker build -t my-local-image:latest .Step 2: Load the Docker Image into Minikube
Next, we load our Docker image directly into the Minikube environment. We can use this command:
minikube image load my-local-image:latestStep 3: Verify the Image is Available
To check if the image loaded into Minikube, we run:
minikube ssh -- docker imagesWe should see my-local-image:latest listed with other
images.
Step 4: Deploy Your Application
Now that we loaded our image, we can create a Kubernetes deployment
with it. Here is an example of deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 2
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app-container
image: my-local-image:latest
ports:
- containerPort: 8080We deploy it using:
kubectl apply -f deployment.yamlNow our local Docker image is loaded into Minikube and used in our Kubernetes deployment. For more details on setting up Minikube, we can look at this installation guide.
Using Minikube Docker Daemon for Local Images
We can use local Docker images with Minikube easily by using the Minikube Docker daemon. This way, we can build and run Docker images directly in Minikube. We do not need to push them to a remote registry. Here are the steps to use the Minikube Docker daemon:
Start Minikube: First, we need to make sure Minikube is running. If it is not started, we can use this command:
minikube startSet Docker Environment: Next, we need to set our shell to use the Docker daemon inside Minikube. We run this command:
eval $(minikube docker-env)This command sets the right environment variables for the Docker daemon of Minikube.
Build Local Docker Images: Now that our shell is set, we can build Docker images like we normally do. For example:
docker build -t my-local-image:latest .Verify the Image: To check if our image is available in Minikube, we can run:
docker imagesDeploying the Image: We can deploy our local image in a Kubernetes deployment. Use a YAML file like this:
apiVersion: apps/v1 kind: Deployment metadata: name: my-deployment spec: replicas: 1 selector: matchLabels: app: my-app template: metadata: labels: app: my-app spec: containers: - name: my-container image: my-local-image:latest ports: - containerPort: 80We apply the deployment with:
kubectl apply -f deployment.yamlAccessing the Application: We need to expose our deployment using a service to access it from outside:
kubectl expose deployment my-deployment --type=NodePort --port=80Get the URL: Finally, we can get the URL to access our service:
minikube service my-deployment --url
This way, we can work with local Docker images in our Minikube Kubernetes environment. It makes our development work easier. For more details on setting up Kubernetes with Minikube, we can check how do I install Minikube for local Kubernetes development.
Pushing Local Images to Minikube Registry
We can push local Docker images to the Minikube registry by following these steps.
Start Minikube with the Registry Add-on: First, we need to make sure the Minikube registry add-on is on. We can do this by running:
minikube addons enable registryBuild Your Local Docker Image: Next, we build our Docker image using the Docker CLI. For example:
docker build -t my-image:latest .Tag the Image for the Minikube Registry: Now we tag our image so it matches the registry URL. The default Minikube registry URL is
localhost:5000. We use this command:docker tag my-image:latest localhost:5000/my-image:latestPush the Image to Minikube’s Registry: After tagging, we push the image to the Minikube registry:
docker push localhost:5000/my-image:latestDeploy Your Application Using the Pushed Image: In our Kubernetes deployment YAML file, we need to say which image to use from the Minikube registry:
apiVersion: apps/v1 kind: Deployment metadata: name: my-app spec: replicas: 2 selector: matchLabels: app: my-app template: metadata: labels: app: my-app spec: containers: - name: my-app image: localhost:5000/my-image:latestApply Your Deployment: Finally, we can deploy our application using
kubectl:kubectl apply -f deployment.yaml
By following these steps, we can push local Docker images to the Minikube registry and deploy our applications with those images. For more info on setting up Minikube for local Kubernetes development, check out this guide.
Configuring Minikube to Use Local Docker Images
To configure Minikube to use local Docker images, we can follow these easy steps:
Start Minikube with Docker Driver: First, we need to start Minikube with the Docker driver. This helps us build and use Docker images directly in Minikube.
minikube start --driver=dockerBuild Your Docker Image: Next, we create our Docker image as usual. Here is a simple Dockerfile for a web application:
FROM nginx:alpine COPY ./html /usr/share/nginx/htmlThen, we build the image with Docker:
docker build -t my-local-image:latest .Check Minikube’s Docker Environment: Now, we need to check if we are using the Docker daemon from Minikube. We run this command:
eval $(minikube docker-env)This command sets up our shell to use Minikube’s Docker daemon. Now we can build images in Minikube.
Build the Docker Image in Minikube: After setting up, we build the image again in Minikube’s Docker context:
docker build -t my-local-image:latest .Deploy the Application: Now we create a Kubernetes deployment using our local image:
apiVersion: apps/v1 kind: Deployment metadata: name: my-deployment spec: replicas: 2 selector: matchLabels: app: my-app template: metadata: labels: app: my-app spec: containers: - name: my-container image: my-local-image:latest ports: - containerPort: 80Then we apply the deployment:
kubectl apply -f deployment.yamlVerify the Deployment: Finally, we check if the deployment is running well with:
kubectl get pods
This setup helps us easily use local Docker images in Minikube. This makes our development work better. For more details on setting up Minikube, we can look at the article on how do I install Minikube for local Kubernetes development.
Frequently Asked Questions
1. How do we use local Docker images in Minikube?
To use local Docker images in Minikube, we must set up Minikube to connect with the local Docker daemon. This way, we can build Docker images right inside the Minikube space. Then, these images will be ready for our Kubernetes deployments. You can check our guide on how to install Minikube for local Kubernetes development for more steps.
2. Can we load Docker images from our local machine into Minikube?
Yes, we can load Docker images from our local machine into Minikube
easily. We just use the minikube image load command and
then write our image name. This command brings our local Docker images
into the Minikube space. After that, we can use these images in our
Kubernetes apps without problems.
3. How do we build local Docker images in Minikube?
Building local Docker images in Minikube is not hard. First, we need
to set our terminal to use the Minikube Docker daemon. We do this by
running eval $(minikube docker-env). After that, we can
build our Docker images like normal with the docker build
command. These images will be available to our Kubernetes pods that run
in Minikube.
4. What is the Minikube Docker daemon, and why is it important?
The Minikube Docker daemon is a small Docker space that runs inside a virtual machine made by Minikube. It helps us build and run Docker containers just for our Kubernetes apps in a local way. By using the Minikube Docker daemon, we make sure our local images are ready for our Kubernetes deployments. We do not need to send them to a remote registry.
5. How can we push local images to the Minikube registry?
To push local images to the Minikube registry, we can use the
minikube image load command. We can also build our images
directly in the Minikube Docker space. This way, we do not need a remote
Docker registry. It makes our development process easier. For more
details about managing images, you can read our article on Kubernetes
components.