How to pull image from dockerhub in kubernetes?

49,837

Solution 1

One line command to create a Docker registry secret

kubectl create secret docker-registry regcred --docker-username=<your-name> --docker-password=<your-pword> --docker-email=<your-email> -n <your-namespace>

Then you can use it in your deployment file under spec

spec:
  containers:
  - name: private-reg-container-name
    image: <your-private-image>
  imagePullSecrets:
  - name: regcred

More details: https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/#create-a-secret-in-the-cluster-that-holds-your-authorization-token

Solution 2

Kubernetes run docker pull pseudo/your-image:latest under the hood. image field in Kubernetes resources is simply the docker image to run.

spec:
  containers:
  - name: app
    image: pseudo/your-image:latest
[...]

As the docker image name contains no specific docker registry url, the default is docker.io. Your image is in fact docker.io/pseudo/your-image:latest

If your image is hosted in a private docker hub repo, you need to specify an image pull secret in the spec field.

spec:
  containers:
  - name: app
    image: pseudo/your-image:latest
  imagePullSecrets:
  - name: dockerhub-credential

Here is the documentation to create the secret containing your docker hub login: https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/

Solution 3

using docker pull or kubectl set image

example yaml deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

start container and show status deployment with kubectl get deployments

result

 NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3/3     3            3           18s

and now update image in kubernetes using set image

kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1

and show status update image with rollout

kubectl rollout status deployment/nginx-deployment

Note: ngnix is name of container ->name

  containers:
      - name: nginx
        image: nginx:1.14.2

nginx:1.16.1 is image version in docker hub, is recommendable change version for update

if you decided remove update and rollback to the previous revision, use rollout undo

kubectl rollout undo deployment/nginx-deployment

for more information, use the documentation

Share:
49,837
Snipper03
Author by

Snipper03

Updated on January 13, 2022

Comments

  • Snipper03
    Snipper03 over 2 years

    I am planning to deploy an application in my kubernetes-clustering infra. I pushed image to dockerhub repo. How can I pull image from dockerhub?

  • DevOops
    DevOops about 5 years
    Good callout on namespace, the documents gloss right over this
  • ekkis
    ekkis about 3 years
    is it possible to get kubectl to run Docker inside the node? that way I could try an image pull by hand (I can't seem to SSH into my nodes)
  • ekkis
    ekkis about 3 years
    I can do kubectl run my-new-pod --image=<a dockerhub image name> and it will build me the pod. is there an equivalent of docker pull?