Run docker images locally with minikube

Building docker images locally and running them on minikube locally

I’d like to share 2 tricks with you for locally testing docker images.

This post is docker focused.

Trick 1:

docker-compose

Pre requisites:

Lean on docker-compose for your local building and tagging of images.

When you think docker-compose you’re probably thinking that you can run your images locally as containers and test them locally.

However docker-compose can also be very useful to build and tag images locally:

Example:

Create a file called: Dockerfile

Add the following contents to the file:

1
2
FROM nginx:latest
EXPOSE 80

That’s it, we’ll test using this simple nginx image.

Create a file called: docker-compose.yaml

Add the following contents to the file:

1
2
3
4
5
6
7
version: "3.9"
services:
  nginx:
    image: localtest:v0.0.1
    build: .
    ports:
      - "80:80"

Run with docker-compose

1
$ docker-compose up -d

You can check that your container is running:

1
$ docker ps

Now check your images

1
$ docker images

You should now see your image built and tagged and available locally:

1
2
3
REPOSITORY   TAG       IMAGE ID         CREATED          SIZE
localtest       v0.0.1    a1dcd6663272   xxx        133MB
nginx           latest    6084105296a9   xxx             133MB

Now you can view this in your browser:

Go to: http://localhost:80

Trick 2:

minikube

Pre requisites:

Running this locally built image on minikube.

Let’s get your local environment ready to run the image on minikube.

Make sure your minikube is running:

1
$ minikube status

Run this command

1
$ eval $(minikube docker-env)

Run the container

1
$ kubectl run localtest --image=localtest:v0.0.1 --image-pull-policy=Never

View pods:

1
$ kubectl get pods

You should see your pod creating and running:

1
2
NAME        READY   STATUS              RESTARTS   AGE
localtest   0/1     ContainerCreating   0          4s
1
2
NAME        READY   STATUS    RESTARTS   AGE
localtest   1/1     Running   0          27s

If you don’t see that, don’t forget to check you ran “eval $(minikube docker-env)”.

Can you create a deployment.yaml file and run it? Sure! Just add the imagePullPolicy as Never:

Create a file called: deployment.yaml

Add the following contents to the file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: localtest
  name: localtest
spec:
  replicas: 1
  selector:
    matchLabels:
      app: localtest
  template:
    metadata:
      labels:
        app: localtest
    spec:
      containers:
      - image: localtest:v0.0.1
        name: localtest
        imagePullPolicy: Never
        ports:
        - containerPort: 80

Create the deployment on minikube (remember to check you’re connected to your minikube cluster):

1
$ kubectl apply -f deployment.yaml
1
$ kubectl get deployment,pod
1
2
3
4
5
6
NAME                        READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/localtest   1/1     1            1           63s

NAME                             READY   STATUS    RESTARTS   AGE
pod/localtest                    1/1     Running   0          14m
pod/localtest-55888c9fc7-j8mkx   1/1     Running   0          63s

Your pod will have a different name to “localtest-6c755dd5d-m4g5l“, remember to copy your pod and replace this value with your pod’s value.

You can test your newly deployed container:

1
$ kubectl port-forward localtest-6c755dd5d-m4g5l 8080:80

Except this time we’ve portforwarded to port 8080

Go to: http://localhost:8080

(This was a bonus tip ^ you can test pods with port-forward without a service).

References:

Some other references

https://minikube.sigs.k8s.io/docs/commands/docker-env/

https://kubernetes.io/docs/concepts/containers/images/#updating-images

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

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.