How to use local docker images with Minikube?

274,626

Solution 1

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.

Solution 2

What worked for me, based on the solution by @svenwltr:

# Start minikube
minikube start

# Set docker env
eval $(minikube docker-env)             # unix shells
minikube docker-env | Invoke-Expression # PowerShell

# Build image
docker build -t foo:0.0.1 .

# Run in minikube
kubectl run hello-foo --image=foo:0.0.1 --image-pull-policy=Never

# Check that it's running
kubectl get pods

Solution 3

Notes:

  • This Answer isnt limited to minikube!

  • If wanting to create the registry on minikube's Docker then run eval $(minikube docker-env) first (to make docker available on the host machine's terminal).
    Otherwise enter in the virtual machine via minikube ssh, and then proceed with the following steps

  • depending on your operative system, minikube will automatically mount your homepath onto the VM.

  • as Eli stated, you'll need to add the local registry as insecure in order to use http (may not apply when using localhost but does apply if using the local hostname)
    Don't use http in production, make the effort for securing things up.


Use a local registry:

docker run -d -p 5000:5000 --restart=always --name local-registry registry:2

Now tag your image properly:

docker tag ubuntu localhost:5000/ubuntu

Note that localhost should be changed to dns name of the machine running registry container.

Now push your image to local registry:

docker push localhost:5000/ubuntu

You should be able to pull it back:

docker pull localhost:5000/ubuntu

Now change your yaml file to use the local registry.

Think about mounting volumes at appropriate location, to persist the images on the registry.

Solution 4

There is one essay and effective way to push your local Docker image directly to minikube, which will save time from building the images in minikube again.

minikube image load <image name>

(minikube cache add <image name> - old deprecated way, for reference)

More details here

All possible method to push images to minikube are mention here: https://minikube.sigs.k8s.io/docs/handbook/pushing/

Solution 5

Adding to to @Farhad 's answer based on this answer,

This are the steps to setup a local registry.

Setup in local machine

Setup hostname in local machine: edit /etc/hosts to add this line

docker.local 127.0.0.1

Now start a local registry (remove -d to run non-daemon mode) :

docker run -d -p 5000:5000 --restart=always --name registry registry:2

Now tag your image properly:

docker tag ubuntu docker.local:5000/ubuntu

Now push your image to local registry:

docker push docker.local:5000/ubuntu

Verify that image is pushed:

curl -X GET http://docker.local:5000/v2/ubuntu/tags/list

Setup in minikube

ssh into minikube with: minukube ssh

edit /etc/hosts to add this line

docker.local <your host machine's ip>

Verify access:

curl -X GET http://docker.local:5000/v2/ubuntu/tags/list

Now if you try to pull, yo might get an http access error.

Enable insecure access:

If you are always planning to use minkube with this local setup then create a minikube to use insecure registry by default (wont work on existing cluster).

minikube start --insecure-registry="docker.local:5000"

else follow below steps:

systemctl stop docker

edit the docker serice file: get path from systemctl status docker

it might be :

/etc/systemd/system/docker.service.d/10-machine.conf or /usr/lib/systemd/system/docker.service

append this text (replace 192.168.1.4 with your ip)

--insecure-registry docker.local:5000 --insecure-registry 192.168.1.4:5000

to this line

ExecStart=/usr/bin/docker daemon -H tcp://0.0.0.0:2376 -H unix:///var/run/docker.sock --tlsverify --tlscacert /etc/docker/ca.pem --tlscert /etc/docker/server.pem --tlskey /etc/docker/server-key.pem --label provider=virtualbox --insecure-registry 10.0.0.0/24

systemctl daemon-reload
systemctl start docker

try pulling:

docker pull docker.local:5000/ubuntu

Now change your yaml file to use local registry.

  containers:
    - name: ampl-django
      image: dockerhub/ubuntu

to

  containers:
    - name: ampl-django
      image: docker.local:5000/nymbleup

Don't use http in production, make the effort for securing things up.

Share:
274,626
Kapil Gupta
Author by

Kapil Gupta

Updated on December 07, 2021

Comments

  • Kapil Gupta
    Kapil Gupta over 2 years

    I have several docker images that I want to use with minikube. I don't want to first have to upload and then download the same image instead of just using the local image directly. How do I do this?

    Stuff I tried:
    1. I tried running these commands (separately, deleting the instances of minikube both times and starting fresh)

    kubectl run hdfs --image=fluxcapacitor/hdfs:latest --port=8989
    kubectl run hdfs --image=fluxcapacitor/hdfs:latest --port=8989 imagePullPolicy=Never
    

    Output:

    NAME                    READY     STATUS              RESTARTS   AGE
    hdfs-2425930030-q0sdl   0/1       ContainerCreating   0          10m
    

    It just gets stuck on some status but never reaches the ready state.


    2. I tried creating a registry and then putting images into it but that didn't work either. I might've done that incorrectly but I can't find proper instructions to do this task.

    Please provide instructions to use local docker images in local kubernetes instance.
    OS: ubuntu 16.04
    Docker : Docker version 1.13.1, build 092cba3
    Kubernetes :

    Client Version: version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.3", GitCommit:"029c3a408176b55c30846f0faedf56aae5992e9b", GitTreeState:"clean", BuildDate:"2017-02-15T06:40:50Z", GoVersion:"go1.7.4", Compiler:"gc", Platform:"linux/amd64"}
    Server Version: version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.2", GitCommit:"08e099554f3c31f6e6f07b448ab3ed78d0520507", GitTreeState:"clean", BuildDate:"1970-01-01T00:00:00Z", GoVersion:"go1.7.1", Compiler:"gc", Platform:"linux/amd64"}
    

    If someone could help me get a solution that uses docker-compose to do this, that'd be awesome.

    Edit:

    Images loaded in eval $(minikube docker-env:

    REPOSITORY                                            TAG                 IMAGE ID            CREATED             SIZE
    fluxcapacitor/jupyterhub                              latest              e5175fb26522        4 weeks ago         9.59 GB
    fluxcapacitor/zeppelin                                latest              fe4bc823e57d        4 weeks ago         4.12 GB
    fluxcapacitor/prediction-pmml                         latest              cae5b2d9835b        4 weeks ago         973 MB
    fluxcapacitor/scheduler-airflow                       latest              95adfd56f656        4 weeks ago         8.89 GB
    fluxcapacitor/loadtest                                latest              6a777ab6167c        5 weeks ago         899 MB
    fluxcapacitor/hdfs                                    latest              00fa0ed0064b        6 weeks ago         1.16 GB
    fluxcapacitor/sql-mysql                               latest              804137671a8c        7 weeks ago         679 MB
    fluxcapacitor/metastore-1.2.1                         latest              ea7ce8c5048f        7 weeks ago         1.35 GB
    fluxcapacitor/cassandra                               latest              3cb5ff117283        7 weeks ago         953 MB
    fluxcapacitor/apachespark-worker-2.0.1                latest              14ee3e4e337c        7 weeks ago         3.74 GB
    fluxcapacitor/apachespark-master-2.0.1                latest              fe60b42d54e5        7 weeks ago         3.72 GB
    fluxcapacitor/package-java-openjdk-1.8                latest              1db08965289d        7 weeks ago         841 MB
    gcr.io/google_containers/kubernetes-dashboard-amd64   v1.5.1              1180413103fd        7 weeks ago         104 MB
    fluxcapacitor/stream-kafka-0.10                       latest              f67750239f4d        2 months ago        1.14 GB
    fluxcapacitor/pipeline                                latest              f6afd6c5745b        2 months ago        11.2 GB
    gcr.io/google-containers/kube-addon-manager           v6.1                59e1315aa5ff        3 months ago        59.4 MB
    gcr.io/google_containers/kubedns-amd64                1.9                 26cf1ed9b144        3 months ago        47 MB
    gcr.io/google_containers/kube-dnsmasq-amd64           1.4                 3ec65756a89b        5 months ago        5.13 MB
    gcr.io/google_containers/exechealthz-amd64            1.2                 93a43bfb39bf        5 months ago        8.37 MB
    gcr.io/google_containers/pause-amd64