53

I am using Docker for Mac with Kubernetes support and I'm struggling to create a Kubernetes Deployment that references a locally built image.

Output of docker images:

REPOSITORY  TAG     IMAGE 
test        latest  2c3bdb36a5ed

My deployment.yaml :

apiVersion: apps/v1
kind: Deployment
metadata:
  name: helloworld-deployment
spec:
  selector:
    matchLabels:
      app: helloworld
  replicas: 1
  template:
    metadata:
      labels:
        app: helloworld
    spec:
      containers:
      - name: aaa
        image: test:latest
        ports:
        - containerPort: 8080

When i run kubectl apply -f deplyment.yaml pods are created but:

helloworld-deployment-764b8b85d8-2c4kl   0/1       ImagePullBackOff   0          
helloworld-deployment-764b8b85d8-rzq7l   0/1       ImagePullBackOff   0

kubectl describe of one of these pods gives:

  Normal   Scheduled              20s               default-scheduler            Successfully assigned helloworld-deployment-79f66d97c6-7tj2x to docker-for-desktop
  Normal   SuccessfulMountVolume  19s               kubelet, docker-for-desktop  MountVolume.SetUp succeeded for volume "default-token-72f44"
  Normal   BackOff                16s               kubelet, docker-for-desktop  Back-off pulling image "test:latest"
  Warning  Failed                 16s               kubelet, docker-for-desktop  Error: ImagePullBackOff
  Normal   Pulling                4s (x2 over 19s)  kubelet, docker-for-desktop  pulling image "test:latest"
  Warning  Failed                 2s (x2 over 17s)  kubelet, docker-for-desktop  Failed to pull image "test:latest": rpc error: code = Unknown desc = Error response from daemon: pull access denied for test, repository does not exist or may require 'docker login'
  Warning  Failed                 2s (x2 over 17s)  kubelet, docker-for-desktop  Error: ErrImagePull

What is interesting is that if i try to run some image hosted on dockerhub then everything is fine, I also tried to use skaffold and it also works like a charm...

I see some similar issues regarding minikube where the solution is to use the minikube docker daemon to build images so that they can be referenced from the Kubernetes cluster.

I would like to avoid setting up local repo, so how can I make it work with Docker's Kubernetes ?

5
  • Possibly this can help: blog.hasura.io/… Jun 7, 2018 at 11:20
  • 1
    @VishalBiyani thx but i would like to avoid setting up localhost:5000 repo - it works but then i d need to push to it each time i create a new version of app. Skaffold seems to find some solution for this problem...
    – user62058
    Jun 7, 2018 at 12:11
  • 3
    try to use tagged version of image rather than latest. Jun 7, 2018 at 12:11
  • @techtrainer you seems to be right. As soon as i give a valid tagged version everything works. Do you know why is that ? I just wanted to use Docker Edge Kubernetes for local develpoment only and thought that using "latest" would be a good idea..
    – user62058
    Jun 8, 2018 at 7:35
  • 4
    the reason is simple, no one knows what is latest. So, docker has to go online and check what is value of latest. As soon as you tag it, it can see that tagged version of image is present locally and does check it online. Jun 8, 2018 at 9:13

3 Answers 3

108

I was able to run a local image by setting the imagePullPolicy to Never.

For example:

apiVersion: v1
kind: Pod
metadata:
  name: local-image-test
spec:
  containers:
  - name: local-image-container
    image: local-image:latest
    imagePullPolicy: Never

(Credit to https://github.com/kubernetes/kubernetes/issues/1293#issuecomment-357326426 for this solution)

4
  • Don't know why "imagePullPolicy: Never" works but it did Dec 5, 2018 at 6:14
  • 9
    B/C as here explained kubernetes.io/docs/concepts/containers/images/#updating-images using "latest" tag will always force a pull, as if set imagePullPolicy:always while the default value is IfNotPresent. Set imagePullPolicy:IfNotPresent should work too.
    – Qiulang
    Jul 8, 2019 at 3:05
  • 1
    Command-line equivalent: kubectl run local-image-test --image=local-image --image-pull-policy=Never
    – Daniel
    Dec 4, 2020 at 17:44
  • Im no expert, but i assume this works because without it, the first thing it tries to do is go pull the image from the public registry. And obviously it wont be able to find it because its only built locally. So basically, by telling it to never go look for a newer image to pull, it will be forced to use what it already has locally. Qiulang is right, imagePullPolicy: IfNotPresent is a more accurate option to what we are doing here and does also work. FWIW: i had this same issue and I was using a versioned tag, not latest.
    – alewitt
    Jul 12, 2022 at 12:54
14

Use tagged version of image rather than latest because If you are shipping Docker images to a production environment, you should just ignore the latest tag. Don’t use it. Don’t be tempted by it. It’s easy to look at it and think that your deployment script should just pull “latest” and your build process will ensure that’s valid.

2
  • 4
    You are absolutely right. My idea was to use "latest" tag ONLY for local development with Kubernetes on Docker Edge. The development workflow would be: 1. modify application code. 2. run docker build ( so to create image always with tag being latest) 3. run kubectl apply -f deployment.yaml . This way i would NOT need a new tag each time i change sth in my app and (what's worse) reference that "new tag" in my deployment.yaml. That was the whole idea to streamline my development workflow
    – user62058
    Jun 9, 2018 at 15:40
  • @user62058 if this worked for you, please accept the answer. Jun 10, 2018 at 9:00
2

In addition to techtrainer comment and answer, I would like to provide some examples of how to do it.

General rule. You have to use tag version of images rather than latest. With Docker tags, the more specific you can get, the better. Get specific to avoid using the wrong image. Consider this if you don't want your colleagues or other Docker users pulling down images and having no idea how recent they are. Get specific to avoid such issues.

docker tag IMAGE ID image/TAG:version.d.m.y

For better management of your images, you should have some smart convention of naming. I prefer to use a stage, version, and date of creation of the image. For example:

docker tag 113a43faa138 ubuntu/prod:v1.8.6.2018

It means production stage, version 1, created on 8 June 2018.

And that's all. Your version is available, and naming is easier to understand for you and further users of this image.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.