How can I use local Docker images with Minikube?

Learn how to use local Docker images with Minikube. Follow our step-by-step guide to easily deploy your Docker images on a Minikube cluster
E
Edtoks4:17 min read

To use local Docker images with Minikube, you have several options. Minikube provides a Docker environment that you can interact with directly, so you can build and use Docker images without needing to push them to a remote registry like Docker Hub.

Here's how you can use local Docker images with Minikube:

Option 1: Use Minikube's Docker Daemon

Minikube runs a local Kubernetes cluster inside a virtual machine or container, and it comes with its own Docker daemon. You can build images directly inside Minikube's Docker environment, so they are immediately available to the Kubernetes cluster.

Steps to use Minikube's Docker Daemon:

  1. Start Minikube (if it's not already running):

    minikube start
  2. Point your terminal to Minikube's Docker daemon:
    Run the following command to configure your shell to use Minikube's Docker daemon:

    eval $(minikube -p minikube docker-env)
  3. Build your Docker image:
    Now, when you build your Docker image, it will be built inside Minikube's Docker environment.

    docker build -t my-local-image:latest .
  4. Deploy your application using the local image:
    Since the image is now in Minikube's Docker environment, you can reference it in your Kubernetes manifests without needing to pull it from an external 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: my-local-image:latest
            ports:
            - containerPort: 8080
  5. Revert to your local Docker daemon (optional):
    If you want to switch back to using your local Docker daemon (instead of Minikube's), run:

    eval $(minikube docker-env --unset)

Option 2: Load Docker Images Directly into Minikube

If you've already built a Docker image locally (using your own Docker environment, not Minikube's), you can load it into Minikube without needing to rebuild the image within Minikube.

Steps to load a Docker image into Minikube:

  1. Start Minikube (if it's not already running):

    minikube start
  2. Build your Docker image locally:

    docker build -t my-local-image:latest .
  3. Load the image into Minikube:
    Use the following command to load your locally built image into Minikube:

    minikube image load my-local-image:latest
  4. Deploy your application using the local image:
    Now you can reference the image in your Kubernetes manifests as shown previously:

    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: my-local-image:latest
            ports:
            - containerPort: 8080

Option 3: Use Minikube’s Built-In Registry Add-On

Minikube has a built-in registry add-on that you can use to push images to and then pull them from within your Minikube environment. This is more complex and is usually used when simulating a real environment.

Steps to use Minikube's registry add-on:

  1. Enable the registry add-on:

    minikube addons enable registry
  2. Build your Docker image locally:

    docker build -t localhost:5000/my-local-image:latest .
  3. Push the image to the local registry:

    docker push localhost:5000/my-local-image:latest
  4. Deploy your application using the image from the local 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-local-image:latest
            ports:
            - containerPort: 8080

Conclusion

Using Minikube's Docker daemon or loading images directly into Minikube are the simplest and most straightforward ways to work with local Docker images. The registry add-on is more suited for mimicking production environments. By following these steps, you can efficiently develop, test, and deploy your applications in a local Kubernetes environment using Minikube.

stack overflow link https://stackoverflow.com/questions/42564058/how-can-i-use-local-docker-images-with-minikube

Let's keep in touch!

Subscribe to keep up with latest updates. We promise not to spam you.