How to set up Minikube to use your local Docker registry?
Instead of pushing your Docker images to external registries — like Docker Hub & then pulling them, learn how you can use your own local Docker registry for Minikube.
If you have ever tried to play with Minikube (which is a great way to learn Kubernetes), you might have seen that Minkube pulls the image from a Docker registry — which, in most cases, is Docker Hub.
While it is more convenient to pull images from Docker Hub, sometimes it becomes a challenge when you don’t want to push/pull big images. Not only does it take a lot of time, but it also exhausts your data plan if you don’t have unlimited internet access.
So, instead of using Docker Hub you can instruct Minikube to use your local registry to that it only pulls from there.
Requirements
- Minukube
- VirtualBox or any other compatible hypervisor.
Steps to perform
Use Minikube’s own Docker daemon rather than your computer’s.
Minikube runs its own Docker daemon to pull images. So, our first step will be to connect to Minikube’s Docker daemon so that all the docker commands point to Minikube instead of your host machine.
admin@shashank-mbp ~/D/quote-randomizer> eval (minikube docker-env)
Note that
eval
command is only valid for a given Terminal session/tab. Make sure to type all the following commands in this post unless specifically told.
Create a local Docker registry so that Minikube can pull images from there.
For this, you can follow my other Medium post.
To keep things simple, however, you can issue the below command.
admin@shashank-mbp ~/D/quote-randomizer> docker run -d -p 5005:5005 --restart=always --name registry registry:2
Unable to find image 'registry:2' locally
2: Pulling from library/registry
486039affc0a: Pull complete
ba51a3b098e6: Pull complete
8bb4c43d6c8e: Pull complete
6f5f453e5f2d: Pull complete
42bc10b72f42: Pull complete
Digest: sha256:7d081088e4bfd632a88e3f3bcd9e007ef44a796fddfe3261407a3f9f04abe1e7
Status: Downloaded newer image for registry:2
034d339e9ea38ee4690dc48b1a920c0922a95eb82daaf0dbf850990bbdef4b13
Build your Docker image
It is now time to build our Docker image using the Dockerfile we have.
admin@shashank-mbp ~/D/quote-randomizer> docker build -t quote-randomizer:latest .
Sending build context to Docker daemon 1.423MB
Step 1/9 : FROM python:3.7-alpine
3.7-alpine: Pulling from library/python
c9b1b535fdd9: Pull complete
2cc5ad85d9ab: Pull complete
29edaae8dc30: Pull complete
ad2b1dc8253c: Pull complete
f5cf370601a5: Pull complete
Digest: sha256:04c0e1365bf119f30e965ae7bd3ac6dc37ce59a8c1277e1b256de002cd364b78
Status: Downloaded newer image for python:3.7-alpine
Tag this newly created image with the local registry location.
Since we have to use our local Docker registry, we first have to tag the newly built image using our registry location.
admin@shashank-mbp ~/D/quote-randomizer> docker tag quote-randomizer localhost:5005/quote-randomizer
Push this tagged image to the local Docker registry.
After tagging our image, we need to push it to our local Docker registry from where Minikube will pull it.
admin@shashank-mbp ~/D/quote-randomizer> docker push localhost:5005/quote-randomizer:latest
The push refers to repository [localhost:5005/quote-randomizer]
1d3e2f9b5fda: Pushed
835fcee89415: Pushed
0f73b0bcb7e2: Pushed
4a527934c5e0: Pushed
09634ba958ec: Pushed
e34f4a8ecda7: Pushed
12322df4c9cf: Pushed
ec6b918078b4: Pushed
d2a9b53ed7b4: Pushed
61b675163d2a: Pushed
5216338b40a7: Pushed
latest: digest: sha256:7681366af3563f6ea6cc0479b6e12965609b843b785d73ca503f29348778da26 size: 2622
Create a Kubernetes deployment using our Docker image from the local registry.
Below is the relevant section from my Kubernetes deployment file.
containers:- name: quote-randomizer
image: localhost:5005/quote-randomizer:latest
ports:
- containerPort: 5001
As you can see above that I am using an image from my local Docker registry.
Let’s apply this file.
admin@shashank-mbp ~/D/quote-randomizer> kubectl apply -f qr.yml
deployment.apps/quote-randomizer createdadmin@shashank-mbp ~/D/quote-randomizer> kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
quote-randomizer 1/1 1 1 6sadmin@shashank-mbp ~/D/quote-randomizer> kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
quote-randomizer-c84f764d8-l2w2m 1/1 Running 0 13m app=quote-randomizer,pod-template-hash=c84f764d8
You can see that my Minikube cluster is using the Docker image from my local registry.
Once you’re done with Minikube, you can revert to the host Docker daemon by typing…
eval $(docker-machine env -u)
You can open the Minikube dashboard by typing minikube dashboard
. It will launch the dashboard in a browser tab.
That’s all for this post. I hope this was informative & useful. I will write about deploying applications to Kubernetes using Minikube in upcoming post(s).