Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a local docker image in Kubernetes?

I have this basic Dockerfile:

FROM nginx
RUN apt-get -y update && apt install -y curl

In the master node of my Kubernetes cluster I build that image:

docker build -t cnginx:v1 . 

docker images shows that the image has been correctly generated:

REPOSITORY                                 TAG                 IMAGE ID            CREATED             SIZE
cgninx                                     v1                  d3b1b19d069e        39 minutes ago      141MB

I use this deployment referencing this custom image:

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2 
  template: 
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: cnginx
        image: cnginx:v1
        imagePullPolicy: Never
        ports:
        - containerPort: 80
      nodeSelector:
        nodetype: webserver

However the image is not found:

NAME                                READY     STATUS              RESTARTS   AGE
nginx-deployment-7dd98bd746-lw6tp   0/1       ErrImageNeverPull   0          4s
nginx-deployment-7dd98bd746-szr9n   0/1       ErrImageNeverPull   0          4s

Describe pod info:

Events:
  Type     Reason                 Age              From                Message
  ----     ------                 ----             ----                -------
  Normal   Scheduled              1m               default-scheduler   Successfully assigned nginx-deployment-7dd98bd746-szr9n to kubenode2
  Normal   SuccessfulMountVolume  1m               kubelet, kubenode2  MountVolume.SetUp succeeded for volume "default-token-bpbpl"
  Warning  ErrImageNeverPull      9s (x9 over 1m)  kubelet, kubenode2  Container image "cnginx:v1" is not present with pull policy of Never
  Warning  Failed                 9s (x9 over 1m)  kubelet, kubenode2  Error: ErrImageNeverPull

I have also tried using the default imagePullPolicy, and some other things such as tagging the image with latest...

So, how can I make Kubernetes use a locally generated docker image?

like image 334
codependent Avatar asked Jun 03 '18 22:06

codependent


People also ask

How do I use local image in Kubernetes?

Set the environment variables with eval $(minikube docker-env) Build the image with the Docker daemon of Minikube (eg docker build -t my-image . ) Set the image in the pod spec like the build tag (eg my-image ) Set the imagePullPolicy to Never , otherwise Kubernetes will try to download the image.

How do I run a local Docker image?

You can also run a Docker image from your own Docker file using the docker-compose command. With compose, you can configure your application's services and then you can start all services with a single command. For example, set up a docker-compose. yml like this in your repository root (where the Dockerfile is):


1 Answers

Your PODs are scheduled on your worker nodes. Since you set imagePullPolicy to Never you need to make your image available to both nodes. In other words, you need to build it on both nodes as you did on the master.

As a sidenote, it would be probably easier in the long term if you setup a custom docker registry and push your images there.

like image 126
whites11 Avatar answered Oct 18 '22 16:10

whites11