Using Docker Private Repos in Kubernetes

July 5, 2019
kubernetes | deployment | docker

To use images from a private repository on hub.docker.com, you need to add a secret containing the credentials for accessing that repository. This can be done via the kubectl command:

$ kubectl create secret docker-registry <my-secret-name> \
    --docker-server=docker.io \
    --docker-username=<your-docker-username> \
    --docker-password=<your-docker-password> \
    --docker-email=<your-docker-email>

If the secret already exists and you want to update it, you first need to delete it:

$ kubectl delete secret <my-secret-name>

When you then define a deployment, you can use the imagePullSecrets option in the deployment yaml to indicate which secret needs to be used to grab the docker image:

my-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
  labels:
    app: my-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-deployment
  template:
    metadata:
      labels:
        app: my-deployment
    spec:
      containers:
      - name: my-deployment
        image: <my-docker-user>/<my-docker-private-repo<
      imagePullSecrets:
      - name: <my-secret-name>

The deployment can then be done using the apply command:

$ kubectl apply -f my-deployment.yaml

You can read many more details about this on the kubernetes.io website.