docker login to a private registry from a laptop, CI and Kubernetes

Serving developers since 2016 Founder-run, no venture capital MFA, SSO and audit log on every plan

Every feature on every plan, with unlimited users. 14 days free. No credit card required.

Start free trial

Starting at $99/month billed annually.

Log in once per registry host by piping a token on standard input. Then give CI and Kubernetes their own copy of that login without ever typing the token on a command line.

On your machine

echo "$REGISTRY_TOKEN" | docker login registry.example.com --username you --password-stdin
  • Log in to the host only: pass registry.example.com, not a path under it. Docker stores one login per registry host, and the image name carries the rest of the path when you pull.
  • Use a token, not your password: pipe it in with --password-stdin so it stays out of your shell history and logs.
  • Know where the login lands: if a credential store is configured (Docker Desktop sets one up), Docker saves the login there and ~/.docker/config.json only names the store in credsStore. Without a store, Docker writes the base64-encoded login into config.json under auths. That difference matters for Kubernetes below.

In CI

GitHub Actions

name: Build
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- name: Log in to the registry
uses: docker/login-action@v4
with:
registry: registry.example.com
username: ${{ vars.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_TOKEN }}
- run: docker build -t registry.example.com/team/my-app:${{ github.sha }} .

registry defaults to Docker Hub, so specify your registry. The action logs out when the job ends.

GitLab CI

build-image:
image: docker:29-cli
services:
- docker:29-dind
variables:
DOCKER_TLS_CERTDIR: "/certs"
before_script:
- echo "$REGISTRY_TOKEN" | docker login registry.example.com --username "$REGISTRY_USER" --password-stdin
script:
- docker build -t registry.example.com/team/my-app:$CI_COMMIT_SHORT_SHA .

Store REGISTRY_TOKEN as a masked CI/CD variable. That login is for the script.

The runner pulls the job’s image: and services: before the script runs, so a private job image needs a DOCKER_AUTH_CONFIG variable instead. Build its value once on your own machine with printf, not echo, so no newline ends up in the encoded login. read -s asks for the token without echoing it or saving it in your shell history:

read -rs TOKEN && printf "my_username:%s" "$TOKEN" | openssl base64 -A; unset TOKEN
{ "auths": { "registry.example.com": { "auth": "<the base64 from above>" } } }

In Kubernetes

A pod pulls images using a Secret of type kubernetes.io/dockerconfigjson, listed under imagePullSecrets. That Secret is a Docker config.json holding the login.

Create the Secret without the token on a command line

kubectl create secret docker-registry --docker-password=... works, but the Kubernetes docs warn that secrets typed on the command line may end up in your shell history. Build the Secret from a config.json instead, written by docker login into a fresh directory so it contains only this login:

export DOCKER_CONFIG="$(mktemp -d)"
printf '%s' '{"auths":{"registry.example.com":{}}}' > "$DOCKER_CONFIG/config.json"
printf '%s' "$REGISTRY_TOKEN" | docker login registry.example.com --username you --password-stdin
grep -q '"auth"' "$DOCKER_CONFIG/config.json" \
&& kubectl create secret generic regcred \
--type=kubernetes.io/dockerconfigjson \
--from-file=.dockerconfigjson="$DOCKER_CONFIG/config.json"
rm -r "$DOCKER_CONFIG"
unset DOCKER_CONFIG

The second line handles credential stores. With a store installed, docker login would save the credentials there and leave config.json without an auth entry, producing a Secret that cannot pull anything. The grep check verifies the login is actually in the file before creating the Secret.

Create the Secret in the pod’s namespace by adding --namespace. A pod can only use image pull secrets from its own namespace.

Use it in a pod

apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
containers:
- name: my-app
image: registry.example.com/team/my-app:1.0.0
imagePullSecrets:
- name: regcred

In a Deployment, put the same imagePullSecrets list in the pod template under spec.template.spec.

Or for every pod in a namespace

kubectl patch serviceaccount default -p '{"imagePullSecrets": [{"name": "regcred"}]}'

New pods in that namespace using the default ServiceAccount get spec.imagePullSecrets set for them.

When it fails

You see What it means What to do
unauthorized or 401 at docker login or docker pull The registry rejected the login: wrong, expired, or revoked token. Create a new token, log in again, and remake the Secret.
denied or 403 The login worked, but it cannot read that repository. Use a token with access to that repository.
not found or manifest unknown The image name or tag is wrong, or the tag does not exist. Check the full image name, path included, and the tag.
ErrImagePull, then ImagePullBackOff The pod could not pull the image, and Kubernetes is retrying with a growing delay. Run kubectl describe pod <name> and check the events for the real reason.
FailedToRetrieveImagePullSecret Kubernetes cannot find a Secret with that name in the pod’s namespace. Check the name under imagePullSecrets, and create the Secret in that namespace.

This is where CloudRepo helps

Give each cluster and each pipeline its own CloudRepo token that can only download. Revoke one and only that cluster stops pulling, with nothing else to rotate, and the Access Log shows what each token pulled. In Kubernetes, create the Secret with the same docker login steps against your organization’s host, and pull by the full name:

image: your-org.mycloudrepo.io/repositories/your-repo/my-app:1.0.0

More: Pull CloudRepo images in Kubernetes, Organization tokens for CI, What did your build agent pull?.

Every feature on every plan, with unlimited users

14 days free. No credit card required.

Starting at $99/month billed annually.

Chris Shellenbarger, founder of CloudRepo

Founder-run since 2016, with no venture capital. A real person answers within one business day. Usually it's me.

Chris, founder of CloudRepo