🐶
Kubernetes

Use Local Docker Images with Minikube

By Jan on 01/14/2025

Learn how to leverage your locally built Docker images for development and testing within a Minikube environment.

Use Local Docker Images with Minikube

Table of Contents

Introduction

This document provides a step-by-step guide on how to deploy a local Docker image to Minikube.

Step-by-Step Guide

  1. Build your Docker image locally. Make sure to tag it appropriately:

    docker build -t my-image:latest .
  2. Start Minikube and configure your shell to use its Docker daemon:

    minikube start
    eval $(minikube docker-env)
  3. Now you can use the my-image:latest image in your Kubernetes deployments just like any other image. Kubernetes will pull it from your local Docker daemon, which is now managed by Minikube.

    apiVersion: apps/v1
    kind: Deployment
    # ... rest of your deployment spec
        image: my-image:latest 
  4. Apply your deployment:

    kubectl apply -f deployment.yaml

Minikube will now use your local Docker image without needing to push it to a registry.

Code Example

The project demonstrates deploying a simple web application to a Minikube cluster. It includes a Dockerfile that builds an image based on Nginx and copies the application code. A deployment.yaml file defines a Kubernetes Deployment to run three replicas of the application. The instructions guide through building the Docker image, starting Minikube, and deploying the application using kubectl. This setup is suitable for local development and testing, allowing you to run your application in a Kubernetes environment without pushing the image to a registry.

my-project/
├── Dockerfile
└── deployment.yaml

1. Dockerfile:

FROM nginx:latest

# Add your application code here
COPY . /usr/share/nginx/html

2. deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: my-image:latest
        ports:
        - containerPort: 80

3. Build and deploy:

  1. Open your terminal and navigate to the my-project directory.

  2. Build the Docker image:

    docker build -t my-image:latest .
  3. Start Minikube and configure your shell:

    minikube start
    eval $(minikube docker-env)
  4. Apply the deployment:

    kubectl apply -f deployment.yaml
  5. Check the deployment status:

    kubectl get deployments

Now, Minikube is running your application using the Docker image built locally without pushing it to any registry. You can access your application through the Minikube IP address and the service port.

Note: This approach is suitable for development and testing purposes. For production environments, it's recommended to push your images to a registry for better management and scalability.

Additional Notes

  • Understanding the process: This method works because Minikube runs its own Docker daemon (a background process managing Docker images and containers). By configuring your shell to use Minikube's Docker daemon, you're essentially telling Kubernetes to look for the image in Minikube's local repository instead of a remote registry.
  • Minikube status: Ensure Minikube is running before building and deploying. Use minikube status to check. If it's not running, use minikube start to start it.
  • Image naming: While latest is used in the example, it's good practice to use specific tags for your images (e.g., my-image:v1.0) to avoid confusion and ensure you're deploying the correct version.
  • Cleaning up:
    • To stop Minikube, use minikube stop.
    • To delete Minikube (including images and deployments), use minikube delete.
  • Alternative approaches:
    • Building inside Minikube: You can build the Docker image directly inside Minikube using eval $(minikube docker-env) && docker build .... This eliminates the need to configure your local Docker daemon but might be slower.
    • Private registries: For production environments or sharing images within a team, consider using a private Docker registry.
  • Limitations: This approach is primarily for development and testing. For production deployments, pushing images to a registry is recommended for better scalability, version control, and collaboration.
  • Troubleshooting:
    • ImagePullBackOff: If you encounter this error, double-check that your image is built correctly, tagged appropriately, and that your shell is configured to use Minikube's Docker daemon.
    • Minikube connectivity: Ensure your shell can communicate with the Minikube Docker daemon. You might need to restart Minikube or your terminal if you encounter issues.
  • Further exploration: Explore Minikube addons for additional functionalities like ingress controllers and persistent volumes to enhance your local Kubernetes development environment.

Summary

Step Description Command
1. Build Docker Image Build your Docker image and tag it. docker build -t my-image:latest .
2. Start Minikube Start Minikube and configure your shell to use its Docker daemon. minikube start
eval $(minikube docker-env)
3. Reference Local Image In your Kubernetes deployment YAML, reference the locally built image. image: my-image:latest
4. Deploy to Minikube Apply your deployment to the Minikube cluster. kubectl apply -f deployment.yaml

Summary: This process allows you to deploy a Kubernetes application to Minikube using a Docker image built locally on your machine, without needing to push the image to a container registry.

Conclusion

This guide explained how to streamline your Kubernetes development workflow using Minikube and local Docker images. By configuring your shell to utilize Minikube's Docker daemon, you can directly deploy and test applications without the need for a separate container registry. Remember that while this approach is excellent for development and testing environments, production deployments benefit from the robustness and scalability of dedicated container registries. As you advance in your Kubernetes journey, explore Minikube addons and alternative deployment strategies to further enhance your development process.

References

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
🤮Clickbait