How to use Kubernetes Secret to pull private Docker Images from DockerHub
Tags: kubernetes, security
Published: 2023-03-27 09:26:33

In this article, you will learn how we pull the private docker image from DockerHub using Kubernetes Secret and create a Kubernetes Pod from the docker private image.

**Docker Hub:**

> Docker Hub is a hosted repository service provided by Docker for finding and sharing container images with your team. Key features include Private Repositories: Push and pull container images. Automated Builds: Automatically build container images from GitHub and Bitbucket and push them to Docker Hub.

**Kubernetes Secrets:**

> A Secret is an object that contains a small amount of sensitive data such as a password, a token, or a key. Such information might otherwise be put in a Pod specification or in a container image. Using a Secret means that you don’t need to include confidential data in your application code.

**Example:**

To use a secret to pull a private image from a container registry, you can create a “imagePullSecrets” field in your deployment or pod YAML file. Here’s an example:

**Step1: Create a secret**

kubectl create secret docker-registry my-registry-secret \
— docker-username=DOCKER_USER \
— docker-password=DOCKER_PASSWORD \
— docker-email=DOCKER_EMAIL

Replace the DOCKER_REGISTRY_SERVER, DOCKER_USER, DOCKER_PASSWORD, and DOCKER_EMAIL with your container registry server address, username, password, and email respectively.


![My secret screenshot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2woecf5ztk5hulyisnxz.png)


![Created secret](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l4jyppr9smpeopwb7skt.png)

**Step2: My Dockerhub account, where I have my private docker image**


![Screenshot of my Dockerhub account](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/acjutwoyrh4v91qvnbpb.png)

**Step3: Create a deployment file with “imagePullSecrets”**

Modify your deployment or pod YAML file to include the imagePullSecrets field:


![Here I am pulling my own private Docker Image from Docker Hub](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0j76h5eo22eiq2p21fet.png)

In this example, we added the _imagePullSecrets_ field to the deployment YAML file, and set the value to the name of the secret we created in step 1 _(my-registry-secret)_. Kubernetes will use this secret to authenticate with the container registry when pulling the private-registry/my-image image.

When you apply the modified YAML file to your cluster, Kubernetes will use the specified secret to authenticate with the container registry and pull the private image.

**Step4: Final result**

For this article I am using “minikube” cluster, so you can see that before creating the deployment we don’t have the docker image “usm87/jenkins-cicd-maven-project:v4”


![Docker Images](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6q7yya2ughrx7pih7u4t.png)


![Kubernetes Deployment file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9xk81rm95lzpzq5a3j2v.png)

After creating the deployment, below are the Pod event logs


![Event Logs of Pod](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a8gvubrx3hn3w4bxrn4r.png)


![Final Result of Docker Images](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dldi18p9e7yopglt45ob.png)

Now you can see we have the docker image “usm87/jenkins-cicd-maven-project:v4” pulled from the docker hub successfully.