How to Use Local Docker Images With Minikube

How to use local docker images with Minikube?

As the README describes, you can reuse the Docker daemon from Minikube with eval $(minikube docker-env).

So to use an image without uploading it, you can follow these steps:

  1. Set the environment variables with eval $(minikube docker-env)
  2. Build the image with the Docker daemon of Minikube (eg docker build -t my-image .)
  3. Set the image in the pod spec like the build tag (eg my-image)
  4. Set the imagePullPolicy to Never, otherwise Kubernetes will try to download the image.

Important note: You have to run eval $(minikube docker-env) on each terminal you want to use, since it only sets the environment variables for the current shell session.

How do I deploy a local docker image to minikube?

Minikube comes with its own docker daemon and not able to find images by default, the below works in my local env, i noticed the first step is already done and looks like the the image is still being pulled, step-2 might solve the problem below.

  1. Set the environment variables with eval $(minikube docker-env), i see you have set this already.
  2. Set ImagePullPolicy to Never in order to use local docker images with the deployment, this will ensure that the image is not pulled from docker repo.

you can try running the below yaml on your cluster for your use-case.

apiVersion: v1
kind: Pod
metadata:
name: test
spec:
containers:
image: door_controls
name: door_controls
imagePullPolicy: Never
ports:
- containerPort: 7777

blog article: https://medium.com/bb-tutorials-and-thoughts/how-to-use-own-local-doker-images-with-minikube-2c1ed0b0968

Using a local image on minikube

When you run eval $(minikube docker-env) you will get the docker-engine of the minikube installation.

In some environments, your local docker-engine could be the same one with minikube, however in most cases it's not. It depends on the drivers settings if the both engines are same.

In case where the engines are different, you will need to set the minikube environment with eval $(minikube docker-env) and than build the image.

how to use local images in kubernetes using minikube

Docker Desktop and Docker from Minikube are two separate Docker Engines. That is why they don't share the Docker image registry.

So you have the following options:

  1. Use Kubernetes from Docker Desktop instead of Minikube (Docker Desktop comes with Kubernetes cluster built-in).

  2. Build your Docker image with Docker Engine from Minikube (instead of Docker Desktop).

  3. Use external Docker registry (you can use Docker Hub or set up your own registry)

Running local docker image in Minikube

If you target the Docker daemon running in the minikube VM when you run docker build instead of the Docker for Windows daemon running on the host, then the minikube Docker will have access to that image and subsequent kubectl run commands will work as desired. I'm not sure exactly which commands to run on Windows, but on a POSIX system like Macs or Linux you can run:

# make 'docker' commands use daemon in minikube
eval $(minikube docker-env)

# build image so that minikube Docker daemon has it
docker build -t hello-world:v1 .

# deploy to kubernetes
kubectl run hello-world --image=hello-world:v1 --port=8080

# unset DOCKER environment variables so it goes back to
# targetting the usual Docker for Windows
eval $(minikube docker-env -u)

I don't know if eval is the right thing to run on Windows, but if you just run minikube docker-env it will likely give you some instructions, e.g. for me it gives:

$ minikube docker-env
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.103:2376"
export DOCKER_CERT_PATH="/Users/amitgupta/.minikube/certs"
# Run this command to configure your shell:
# eval $(minikube docker-env)

How to use local docker images in kubernetes deployments (NOT minikube)

One can do so with a combination of crictl and ctr, if using containerd.

TLDR: these steps, which are also described in the crictl github documentation:

1- Once you get the image on the node (in my case, a VM), make sure it is in an archive (.tar). You can do that with the docker save or ctr image export commands.

2- Use sudo ctr -n=k8s.io images import myimage.tar while in the same directory as thearchived image to add it to containerd in the namespace that kubernetes uses to track it's images. It should now appear when you run sudo crictl images.


As suggested, I tried listing images with crictl and my app:test did not appear. However, trying to import my local image through crictl didn't seem to work either. I used crictl pull app:test and it showed the following error message:

FATA[0000] pulling image failed: rpc error: code = Unknown desc = failed to pull and unpack image "docker.io/library/app:test": failed to resolve reference "docker.io/library/app:test": pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed.

However, when following these steps, my image is finally recognized as an existing local image in kubernetes. They are actually the same as suggested in the crictl github documentation

How does one explain this? How do images get "registered" in the kubernetes cluster? Why couldn't crictl import the image? I might post another issue to ask that...

How to create docker images inside minikube registry

I have solved the problem by following these steps:

  1. Restarted minikube by executing minikube start
  2. I ran minikube -p minikube docker-env and it returned following:

    output of command

  3. I executed eval $(minikube -p minikube docker-env)

  4. Then I ran docker-compose commands.

Images and Containers was created within minikube docker registry.

To be honest I don't know why eval $(minikube docker-env) didn't do the job and the differences between eval $(minikube docker-env) and eval $(minikube -p minikube docker-env).

If you know the reason please let me know.



Related Topics



Leave a reply



Submit