Minikube
Docker
Kubernetes
Local Development
Containerization

How can I use local Docker images with Minikube?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

When working with a local development environment using Docker and Kubernetes, a common setup involves using Minikube as a local Kubernetes cluster. Minikube supports a variety of container runtimes, including Docker, which enables you to build and test your applications using local Docker images. This article walks you through the steps to effectively utilize local Docker images with Minikube, making the process more streamlined and efficient.

Prerequisites

Before we begin, ensure you have the following installed on your system:

  • Minikube: The easiest way to set up a local Kubernetes cluster.
  • Docker: The platform for building, running, and managing container applications.
  • Kubectl: The Kubernetes command-line tool for managing clusters.

Docker Environment on Minikube

Minikube uses its own Docker daemon. If you build images on your local machine's Docker daemon, they are not directly accessible to Minikube unless explicitly pushed to a container registry. However, a more efficient way is to instruct your terminal session to use Minikube's Docker daemon.

Switching to Minikube's Docker Daemon

To configure your command line to use Minikube's Docker daemon, use the following command:

bash
eval $(minikube docker-env)

This command sets up environment variables such that any subsequent Docker commands run against the Docker daemon inside Minikube. You can build Docker images as you normally would, and they will be directly available to your Minikube cluster.

Building an Image with Minikube's Docker Daemon

Here's a sample Dockerfile for a simple Node.js application:

dockerfile
1# Sample Dockerfile
2FROM node:14
3WORKDIR /usr/src/app
4COPY package*.json ./
5RUN npm install
6COPY . .
7EXPOSE 8080
8CMD ["node", "app.js"]

To build an image using Minikube's Docker daemon:

bash
docker build -t my-node-app:1.0 .

Deploying the Image on Minikube

Once the image is built, you can deploy it on Minikube like any other image:

yaml
1# deployment.yaml
2apiVersion: apps/v1
3kind: Deployment
4metadata:
5  name: my-node-app
6spec:
7  replicas: 1
8  selector:
9    matchLabels:
10      app: my-node-app
11  template:
12    metadata:
13      labels:
14        app: my-node-app
15    spec:
16      containers:
17      - name: my-node-app
18        image: my-node-app:1.0
19        ports:
20        - containerPort: 8080

Apply the deployment to Minikube:

bash
kubectl apply -f deployment.yaml

Checking Your Deployment

After deployment, ensure everything is running correctly using:

bash
kubectl get pods
kubectl logs <pod-name>

Summary Table

StepDescription
Switch Docker DaemonUse eval $(minikube docker-env) to switch to Minikube's Docker.
Build ImageUse docker build as you would normally to build the Docker image.
Deploy on MinikubeCreate a Kubernetes manifest and use kubectl apply to deploy.
Verify DeploymentUse kubectl get pods and kubectl logs to ensure it's running.

Additional Considerations

  • Resetting Docker Env: Once done with Minikube, reset your Docker environment by executing eval $(minikube docker-env -u).
  • Leveraging Local Registry: For a more evolved setup, consider configuring a local Docker registry which Minikube can access, or use alternative container runtimes supported by Minikube like Podman.
  • Persistence: Note that Minikube's Docker environment is ephemeral per VM session, hence built images may not persist across Minikube restarts.

By following the described steps, you can seamlessly integrate local Docker development with a Minikube cluster, promoting a smoother development workflow without unnecessary image pushes to a remote registry. This setup is ideal for local testing and development, expediting the build-test loop owing to its localized nature.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.