Use Local Docker Images with Minikube



Why Use a Local Docker Image

You may find yourself in the position where you will need to test a pod locally for the following reasons:

  • Your organisation does not allow you to submit Docker images to public repositories for intellectual or security reasons  
  • You may not want to have access to your ECR host locally for development 
  • You may be just trying something out and want to test it in a local test environment without making it available to the rest of your team

The first thing you have to do is make sure that you have set the following command.

eval $(minikube docker-env)

This essentially sets up the following environment vars.

export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://127.0.0.1:[port-number]"
export DOCKER_CERT_PATH="/Users/[your-user]/.minikube/certs"
export MINIKUBE_ACTIVE_DOCKERD="minikube"

You must then build and tag the image so that you can then specify it in your Kubernetes resource, such as a pod or deployment.

docker build -t minikube-image .

Or pick one from you local list of already built images.

docker ps

Create a pod in a file in dry-run mode and write it to a file like so.

kubectl run website --image=minikube-image --dry-run=client -o yaml > website.yaml

Use your editor such as vi or vim to edit the website.yaml file and set the imagePullPolicy to never.This will ensure that the pod will never try and to online to search for the image.

      apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: website
name: website
spec:
containers:
- image: minikube-image
name: website
imagePullPolicy: Never
resources: { }
dnsPolicy: ClusterFirst
restartPolicy: Always

Then apply the image to the server using either the create or the apply method.

kubectl apply -f website.yaml 

If this is something that you would like to use on a more permanent bases then perhaps setting the `eval $(minikube docker-env)` command in your profile file such as .bash_profile or .zprofile would stop you having to remember to call this command for every terminal.


Happy Minikubing!

jamie macdonald
jamie macdonald

KubeSmash

Updated on: 2024-11-18T14:58:14+05:30

67 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements