⎈ k8s knowledge compiler

Pull an Image from a Private Registry [page]deterministic

tasksstoragesecurity

This page shows how to create a Pod that uses a [Secret](#gloss:secret) to pull an image from a private container image registry or repository. There are many private registries in use. This task uses [Docker Hub](https://www.docker.com/products/docker-hub) as an example registry.

##

*

* To do this exercise, you need the `docker` command line tool, and a [Docker ID](https://docs.docker.com/docker-id/) for which you know the password. * If you are using a different private container registry, you need the command line tool for that registry and any login information for the registry.

## Log in to Docker Hub

On your laptop, you must authenticate with a registry in order to pull a private image.

Use the `docker` tool to log in to Docker Hub. See the _log in_ section of [Docker ID accounts](https://docs.docker.com/docker-id/#log-in) for more information.

```shell docker login ```

When prompted, enter your Docker ID, and then the credential you want to use (access token, or the password for your Docker ID).

The login process creates or updates a `config.json` file that holds an authorization token. Review [how Kubernetes interprets this file](/docs/concepts/containers/images#config-json).

View the `config.json` file:

```shell cat ~/.docker/config.json ```

The output contains a section similar to this:

```json { "auths": { "https://index.docker.io/v1/": { "auth": "c3R...zE2" } } } ```

> Note: If you use a Docker credentials store, you won't see that `auth` entry but a `credsStore` entry with the name of the store as value. In that case, you can create a secret directly. See [Create a Secret by providing credentials on the command line](#create-a-secret-by-providing-credentials-on-the-command-line).

## Create a Secret based on existing credentials {#registry-secret-existing-credentials}

A Kubernetes cluster uses the Secret of `kubernetes.io/dockerconfigjson` type to authenticate with a container registry to pull a private image.

If you already ran `docker login`, you can copy that credential into Kubernetes:

```shell kubectl create secret generic regcred \ --from-file=.dockerconfigjson=<path/to/.docker/config.json> \ --type=kubernetes.io/dockerconfigjson ```

If you need more control (for example, to set a namespace or a label on the new secret) then you can customise the Secret before storing it. Be sure to:

  • set the name of the data item to `.dockerconfigjson`
  • base64 encode the Docker configuration file and then paste that string, unbroken as the value for field `data[".dockerconfigjson"]`
  • set `type` to `kubernetes.io/dockerconfigjson`

Example:

```yaml apiVersion: v1 kind: Secret metadata: name: myregistrykey namespace: awesomeapps data: .dockerconfigjson: UmVhbGx5IHJlYWxseSByZWVlZWVlZWVlZWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGx5eXl5eXl5eXl5eXl5eXl5eXl5eSBsbGxsbGxsbGxsbGxsbG9vb29vb29vb29vb29vb29vb29vb29vb29vb25ubm5ubm5ubm5ubm5ubm5ubm5ubm5ubmdnZ2dnZ2dnZ2dnZ2dnZ2dnZ2cgYXV0aCBrZXlzCg== type: kubernetes.io/dockerconfigjson ```

If you get the error message `error: no objects passed to create`, it may mean the base64 encoded string is invalid. If you get an error message like `Secret "myregistrykey" is invalid: data[.dockerconfigjson]: invalid value ...`, it means the base64 encoded string in the data was successfully decoded, but could not be parsed as a `.docker/config.json` file.

## Create a Secret by providing credentials on the command line

Create this Secret, naming it `regcred`:

```shell kubectl create secret docker-registry regcred --docker-server=<your-registry-server> --docker-username=<your-name> --docker-password=<your-pword> --docker-email=<your-email> ```

where:

* `<your-registry-server>` is your Private Docker Registry FQDN. Use `https://index.docker.io/v1/` for DockerHub. * `<your-name>` is your Docker username. * `<your-pword>` is your Docker password. * `<your-email>` is your Dock …(trimmed)

Sources

tasks/configure-pod-container/pull-image-private-registry.md · docPull an Image from a Private Registry

Related (9)

references SecretSecret conf=1
part_of {{% heading "prerequisites" %}}describes conf=1
part_of Log in to Docker Hubdescribes conf=1
part_of Inspecting the Secret `regcred`describes conf=1
part_of Create a Pod that uses your Secretdescribes conf=1
part_of Using images from multiple registriesdescribes conf=1
part_of {{% heading "whatsnext" %}}describes conf=1

← all Docs