🐶
Kubernetes

Run Kubernetes Pod with Local Image: A How-To Guide

By Jan on 02/11/2025

Learn how to pull a local image and use it to run a pod in your Kubernetes cluster with this step-by-step guide.

Run Kubernetes Pod with Local Image: A How-To Guide

Table of Contents

Introduction

When working with Kubernetes, you might encounter situations where you want to use Docker images built on your local machine. However, Kubernetes doesn't automatically pull images from your local Docker daemon. This is because Kubernetes is designed to fetch images from container registries. This article outlines several methods to use your local Docker images with Kubernetes, along with their pros and cons.

Step-by-Step Guide

To use a local Docker image with Kubernetes, you need to make sure Kubernetes can access the image. Here's a breakdown:

Understanding the Issue:

Kubernetes doesn't directly pull images from your local Docker daemon. By default, it tries to fetch images from a container registry (like Docker Hub).

Solutions:

  1. Using a Local Registry:

    • Set up a local container registry (e.g., using a tool like Docker Registry).

    • Push your image to the local registry: docker tag my-image localhost:5000/my-image and docker push localhost:5000/my-image

    • In your Kubernetes deployment file, reference the image from your local registry:

      spec:
        containers:
        - name: my-container
          image: localhost:5000/my-image
  2. Using imagePullPolicy: Never (Use with Caution):

    • This option tells Kubernetes to only use a local image and not attempt to pull from a registry.

    • Important: This assumes the image already exists on all nodes where the pod might run.

      spec:
        containers:
        - name: my-container
          image: my-image
          imagePullPolicy: Never 
  3. For Development with Tools like Minikube:

    • Tools like Minikube run a single-node Kubernetes cluster inside a VM.

    • You can configure your Docker client to interact directly with the Minikube VM's Docker daemon:

      eval $(minikube docker-env)
    • Now you can build and push images as usual, and Minikube will be able to use them.

Important Considerations:

  • Production Environments: Using local images directly is generally not recommended for production. It's better to use a proper container registry for image management and distribution.
  • Image Availability: Ensure the image is present on all nodes where the pod might be scheduled.
  • Security: Be mindful of the security implications of using imagePullPolicy: Never.

Code Example

This code provides a step-by-step guide for deploying a "Hello World" application to a Kubernetes cluster using a local Docker image. It covers building the Docker image, setting up a local Docker registry, pushing the image to the registry, defining a Kubernetes deployment, and verifying the deployment. The example emphasizes using a local registry for improved practices but acknowledges alternative approaches for development environments. Security considerations and the use of proper container registries for production are highlighted.

This example demonstrates the use of a local Docker registry to run a simple "Hello World" application in Kubernetes.

1. Create a Dockerfile for the Application:

FROM alpine:latest
CMD ["echo", "Hello from a local Docker image!"]

2. Build the Docker Image:

docker build -t my-hello-image .

3. Set up a Local Docker Registry (using Docker Registry):

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

4. Tag and Push the Image to the Local Registry:

docker tag my-hello-image localhost:5000/my-hello-image
docker push localhost:5000/my-hello-image

5. Create a Kubernetes Deployment File (deployment.yaml):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-hello-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-hello-app
  template:
    metadata:
      labels:
        app: my-hello-app
    spec:
      containers:
      - name: my-hello-container
        image: localhost:5000/my-hello-image

6. Deploy the Application to Kubernetes:

kubectl apply -f deployment.yaml

7. Verify the Deployment:

kubectl get pods

This will show you the pods running with your local image.

Important:

  • Replace my-hello-image and my-hello-app with your desired names.
  • Ensure your Kubernetes cluster can access the local registry (e.g., by configuring network access or using the same Docker network).

This example demonstrates using a local registry, which is generally a better practice than relying on imagePullPolicy: Never. However, for development environments like Minikube, you can follow the instructions in the article to directly use images from your local Docker daemon.

Remember to prioritize security and use a proper container registry for production environments.

Additional Notes

General Best Practices:

  • Use a Private Registry: For anything beyond very simple local testing, using a private container registry (like Docker Hub, ECR, GCR, or a self-hosted one) is highly recommended. This provides:
    • Centralized Image Storage: Easier image management and sharing.
    • Version Control: Track image versions and rollbacks.
    • Security: Control access to your images.
  • Optimize Images: Keep your Docker images small and efficient to reduce build times, push/pull times, and storage costs. Use multi-stage builds and minimal base images.
  • Understand imagePullPolicy: While Never has its uses in development, be very cautious with it. In most cases, IfNotPresent (default) or Always provide a better balance between using local caches and ensuring you have the correct image version.

Troubleshooting:

  • Image Pull Errors: If Kubernetes can't pull an image, check:
    • Image Name and Tag: Make sure the image name and tag are correct in your deployment file.
    • Registry Credentials: If using a private registry, ensure your Kubernetes cluster has the necessary credentials to authenticate.
    • Network Connectivity: Verify that your nodes can reach the registry or local Docker daemon.
  • Logs: Examine the logs of your pods and the Kubernetes control plane components (like kubelet) for error messages that can help diagnose issues.

Alternatives to Local Images:

  • Build Images in the Cluster: Tools like Kaniko and Buildah allow you to build container images directly inside your Kubernetes cluster, eliminating the need to push/pull from external sources.

Security Considerations:

  • imagePullPolicy: Never Risks:
    • Inconsistent Deployments: Different nodes might end up running different versions of your application if the image isn't manually updated everywhere.
    • Security Vulnerabilities: You won't automatically get updates or security patches if you don't pull new images.
  • Private Registry Security: Secure your private registry with authentication, authorization, and potentially image scanning tools to prevent unauthorized access and identify vulnerabilities.

Summary

Method Description Use Case Considerations
Local Registry Set up a local container registry (e.g., Docker Registry) and push your image to it. Reference the image from the local registry in your Kubernetes deployment file. Suitable for development and testing environments where you want to share images locally. Adds complexity of setting up and managing a local registry.
imagePullPolicy: Never Tells Kubernetes to only use a local image and not attempt to pull from a registry. Useful for development and testing when you are certain the image exists on all nodes. Use with caution! Assumes the image is already present on all nodes. Not recommended for production.
Minikube Integration Configure your Docker client to interact directly with the Minikube VM's Docker daemon. Specifically for development with Minikube. Limited to Minikube environments.

General Considerations:

  • Production Environments: Use a proper container registry for image management and distribution in production.
  • Image Availability: Ensure the image is present on all nodes where the pod might be scheduled.
  • Security: Be mindful of the security implications, especially when using imagePullPolicy: Never.

Conclusion

Choosing the right method for using local Docker images with Kubernetes depends on your specific use case and environment. While using a local registry offers a more structured approach suitable for development and testing, leveraging Minikube's integration simplifies the process in specific development scenarios. However, it's crucial to remember that directly using local images in production environments is generally discouraged. Prioritizing a proper container registry for image management and distribution in production ensures better security, scalability, and maintainability of your deployments. Always prioritize security considerations and carefully evaluate the implications of each method before implementation.

References

  • Microk8s kubernetes cannot pull local image - microk8s - Discuss ... Microk8s kubernetes cannot pull local image - microk8s - Discuss ... | Hello all, I have a remote node with private registry (IP: 10.34.104.50:32000) that only builds and store images, deployment is made on another node (IP: 10.34.104.5) that pulls the image from remote private registry. if I remove the image from the remote private registry and attempt to run a pod with the same image on a node where it HAS been pulled previously, Kubernetes will try to pull from remote registry even if it has image locally, and the pull will fail, imagePullPolicy: IfNotPr...
  • Cant access local images with Docker for Windows in Kubernetes ... Cant access local images with Docker for Windows in Kubernetes ... | I’m trying to understand the dev flow with Docker for Windows and Kubernetes. I created a simple “dummy” image based on nginx called mytest:latest Then I tried referencing this in a pod: kubectl run -it mytest --image mytest:latest This leads to an ImagePullError on the kubernetes pod. At this point I’m a bit unsure how this is supposed to work, but given that the Kubernetes runs in the same moby instance as “regular” docker, and my local docker client is using the moby vm’s docker engine I...
  • Images | Kubernetes Images | Kubernetes | A container image represents binary data that encapsulates an application and all its software dependencies. Container images are executable software bundles that can run standalone and that make very well defined assumptions about their runtime environment. You typically create a container image of your application and push it to a registry before referring to it in a Pod. This page provides an outline of the container image concept. Note:If you are looking for the container images for a Kubernetes release (such as v1.
  • How can I create a deployment from a local image in the rancher ... How can I create a deployment from a local image in the rancher ... | I am new to rancher desktop and I am trying to set up a local cluster to test my application but I am a bit lost on how to do this. What I have done so far: I made sure that I am using the rancher ...
  • How do you use the local images in Kubernetes? | by Tanmay ... How do you use the local images in Kubernetes? | by Tanmay ... | We frequently encounter situations when navigating the Kubernetes images that need us to work with local container images that are kept…
  • kubelet fails to pull local image while nodes can docker pull the ... kubelet fails to pull local image while nodes can docker pull the ... | Posted by u/[Deleted Account] - 6 votes and 15 comments
  • KiND - How I Wasted a Day Loading Local Docker Images KiND - How I Wasted a Day Loading Local Docker Images | How to load local docker image into kind cluster node. Why loaded docker image may not work. How Kubernetes imagePullPolicy work.
  • containerd can't find local image for container, even though ctr ...](https://opengraph.githubassets.com/04266f70dc74ddb8bf82eb78caced90e205254032e7c37fb9d7aa1960d77731f/k3s-io/k3s/issues/7015) [containerd can't find local image for container, even though ctr ... | Environmental Info: K3s Version: k3s version v1.24.3+k3s1 (990ba0e8) go version go1.18.1 Node(s) CPU architecture, OS, and Version: Linux ben-rhel1 5.14.0-70.17.1.el9_0.x86_64 #1 SMP PREEMPT Tue Ju...
  • My deployment have some difficulties pulling images : r/kubernetes My deployment have some difficulties pulling images : r/kubernetes | Posted by u/Many-Director3375 - 2 votes and 6 comments

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
🤮Clickbait