~/kubernetes/how-configure-docker-repository-image-pull-secret.md

Kubernetes - How to Configure Docker Repository to Pull Image and Configure Secret

2 min readApril 23, 2022by Gorav Singal#kubernetes#k-8-s#docker
// TL;DR

Create a docker-registry secret from a docker-config.json file and reference it as imagePullSecrets in your Kubernetes pod spec.

Kubernetes - How to Configure Docker Repository to Pull Image and Configure Secret

Introduction

In most of cases, you are not pulling images from docker hub public repository. You might have your private registry or repository configured in your premises. In that case, you need to tell kubernetes how to pull images from that repository.

Create Docker-Config json file

First, you need to create docker-config json file. Filename: docker-config.json

Assumming, I have registry/repository host as: my-docker-repository.com Example:

{
    "auths": {
        "my-docker-repository.com": {
            "username": "<username>",
            "password": "<artifactory token>",
            "email": "<email>",
            "auth": "<base64(username:token)>"
        }
    }
}

Note: The auth above is calculated as base64 of "username:token".

You might have multiple repositories. In that case, just add multiple such json like below:

{
  "auths":{
    "<repo1>":{
      "username": "",
      "password": "",
      "email": "",
      "auth": ""
    },
    "<repo2>":{
      "username": "",
      "password": "",
      "email": "",
      "auth": ""
    },
    "<repo3>":{
      "username": "",
      "password": "",
      "email": "",
      "auth": ""
    },
  }
}

Shell Script for Creating Secret

Below is the script for creating kubernetes secret.

kubectl create secret generic dockerreg_cred --from-file=.dockerconfigjson=./secret/prod/docker-config.json --type=kubernetes.io/dockerconfigjson

Where, I have placed the docker-config.json file at secret/prod/docker-config.json.

Above script will create a kubernetes secret. Now, is the time to use this secret while pulling docker image.

Sample Deployment Yaml Config file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-api
  labels:
    app: my-api
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-api
  template:
    metadata:
      labels:
        app: my-api
    spec:
      containers:
      - name: my-api
        image: my-docker-repo.com/apps/head/my-api:latest
        imagePullPolicy: Always
        envFrom:
        - configMapRef:
            name: my-api-config
        ports:
        - containerPort: 8080
        volumeMounts:
            - name: my-api-pvc
              mountPath: /var/opt
        resources:
          limits:
            cpu: 2
            memory: 4Gi
          requests:
            cpu: 2
            memory: 4Gi
      imagePullSecrets:
      - name: dockerreg_cred
      volumes:
        - name: my-api-pvc
          persistentVolumeClaim:
            claimName: my-pvc

Note the section:

imagePullSecrets:
  - name: dockerreg_cred

You are all set.

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

Kubernetes - How to Solve Gateway Timeout with Http Statuscode Error 504

Kubernetes - How to Solve Gateway Timeout with Http Statuscode Error 504

Introduction You have a running kubernetes setup, and have a webservice (exposed…

Set a Default Kubernetes Namespace So You Stop Repeating --namespace

Set a Default Kubernetes Namespace So You Stop Repeating --namespace

Introduction In some of the cases, we need to specify namespace name along with…

How to Copy Local Docker Image to Another Host Without Repository and Load

How to Copy Local Docker Image to Another Host Without Repository and Load

Introduction Consider a scenario where you are building a docker image on your…

How to configure Grafana (Free version) with oAuth Okta, with SSL on Kubernetes

How to configure Grafana (Free version) with oAuth Okta, with SSL on Kubernetes

Introduction In our previous post How to configure Grafana on docker, we saw how…

How to connect to a running mysql service on host from a docker container on same host

How to connect to a running mysql service on host from a docker container on same host

Introduction I have a host running mysql (not on a container). I have to run an…

Docker: unauthorized: incorrect username or password.

Docker: unauthorized: incorrect username or password.

While running docker commands with some images, I started getting error: The…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…