Create Kubernetes Docker Registry secret

image.png


  • CLI
$ kubectl create secret docker-registry <secret_name> \
--docker-server=<registry_url> \
--docker-username=<username> \
--docker-password=<password>
# example
$ kubectl create secret docker-registry my-registry-secret \
--docker-server=https://ghcr.io \
--docker-username=xxxx \
--docker-password=yyyy

  • YMAL
# .dockerconfig
{
    "auths": {
        "<REGISTRY_URL>": {
            "auth": "$(echo "<USERNAME>:<PASSWORD>" | base64)\"
        }
    }
}
  • Convert to base64
# example
$ echo "{\"auths\": {\"https://ghcr.io\": {\"auth\": \"$(echo "xxx:yyy" | base64)\"}}}" | base64 | base64

...
# base64
ZXlKaGRYUm9jeUk2SUhzaWFIUjBjSE02THk5bmFHTnlMbWx2SWpvZ2V5SmhkWFJvSWpvZ0ltVklh
RFJQYm13MVpWRnZQU0o5ZlgwSwo=
  • secret.yml
# secret.yml
apiVersion: v1
data:
  .dockerconfigjson: <output_base64>
kind: Secret
metadata:
  name: <secret_name>
  namespace: <namespace_name>
type: kubernetes.io/dockerconfigjson
  • apply
$ kubectl apply -f secret.yml
...
secret/<secret_name> created

---
$ kubectl get secret
NAME                  TYPE                                  DATA   AGE
....
demo-registry         kubernetes.io/dockerconfigjson        1      23m