How can I pull docker.pkg.github.com
Docker images from within Kubernetes cluster?
Currently, the Github Docker registry requires authentication even for packages from public Github repositories.
How can I pull docker.pkg.github.com
Docker images from within Kubernetes cluster?
Currently, the Github Docker registry requires authentication even for packages from public Github repositories.
read:packages
scope at https://github.com/settings/tokens/new.Base-64 encode <your-github-username>:<TOKEN>
, ie.:
$ echo -n VojtechVitek:4eee0faaab222ab333aa444aeee0eee7ccc555b7 | base64
<AUTH>
Note: Make sure not to encode a newline character at the end of the string.
Create kubernetes.io/dockerconfigjson secret
A) Create secret manually:
$ echo '{"auths":{"docker.pkg.github.com":{"auth":"<AUTH>"}}}' | kubectl create secret generic dockerconfigjson-github-com --type=kubernetes.io/dockerconfigjson --from-file=.dockerconfigjson=/dev/stdin
B) Or, create .yml file that can be used in kubectl apply -f
:
kind: Secret
type: kubernetes.io/dockerconfigjson
apiVersion: v1
metadata:
name: dockerconfigjson-github-com
stringData:
.dockerconfigjson: {"auths":{"docker.pkg.github.com":{"auth":"<AUTH>"}}}
Note for GitOps: I strongly recommend not to store the above file in plain-text in your git repository. Hydrate the value in your CD pipeline or encrypt/seal the file with tools like https://github.com/mozilla/sops or https://github.com/bitnami-labs/sealed-secrets.
Now, you can reference the above secret from your pod's spec definition via imagePullSecrets
field:
spec:
containers:
- name: your-container-name
image: docker.pkg.github.com/<ORG>/<REPO>/<PKG>:<TAG>
imagePullSecrets:
- name: dockerconfigjson-github-com
data
instead of stringData
. But you'll have to base64 encode your secret value. –
Jury I had to migrate from docker.pkg.github.com to ghcr.io to get this to work with containerd: https://docs.github.com/en/packages/working-with-a-github-packages-registry/migrating-to-the-container-registry-from-the-docker-registry
Don't forget to create the token with read:packages
:
kubectl create secret docker-registry dockerconfigjson-github-com \
--dry-run=true \
--docker-server=https://docker.pkg.github.com \
--docker-username=<username> \
--docker-password=<https://github.com/settings/tokens/new> \
--namespace=default -o yaml
Add the pull secret:
spec:
containers:
- name: your-container-name
image: docker.pkg.github.com/<ORG>/<REPO>/<PKG>:<TAG>
imagePullSecrets:
- name: dockerconfigjson-github-com
Important notice as github changed it primary domain name from docker.pkg.github.com to ghcr.io (Release Note):
secrets.yml now should look like
kind: Secret
type: kubernetes.io/dockerconfigjson
apiVersion: v1
metadata:
name: dockerconfigjson-github-com
stringData:
.dockerconfigjson: {"auths":{"ghcr.io":{"auth":"<AUTH>"}}}
And the corresponding deployment spec
spec:
containers:
- name: your-container-name
image: ghcr.io/<ORG>/<REPO>/<PKG>:<TAG>
imagePullSecrets:
- name: dockerconfigjson-github-com
Other than described, the old version did not work for me anymore. Maybe this helps someone else!
Here is a solution for folks who are deploying a private ghcr.io image to a kubernetes cluster from within a Github Actions Workflow:
read:packages
scope. You can use this link: https://github.com/settings/tokens/new?scopes=read:packagesKUBERNETES_GHCR_PACKAGES_TOKEN
.github/workflows/[action].yml
:# ...
jobs:
build:
name: Deploy to kubernetes cluster
steps:
# ... checkout, save x509 keys
- name: "Generate ghcr.io imagepullsecret"
run: |
DOCKER_AUTH_TOKEN=$(echo -n "${GHCR_USER}:${KUBERNETES_GHCR_PACKAGES_TOKEN}" | base64)
DOCKER_CONFIG_JSON="{\"auths\":{\"${DOCKER_REGISTRY}\":{\"auth\":\"${DOCKER_AUTH_TOKEN}\"}}}"
echo "
kind: Secret
type: kubernetes.io/dockerconfigjson
apiVersion: v1
metadata:
name: dockerconfigjson-ghcr-io
stringData:
.dockerconfigjson: '${DOCKER_CONFIG_JSON}'
" > "${KUBERNETES_DIR}/dockerconfigjson-ghcr-io.yml"
env:
KUBERNETES_DIR: "k8"
GHCR_USER: "${{ github.actor }}"
KUBERNETES_GHCR_PACKAGES_TOKEN: "${{ secrets.KUBERNETES_GHCR_PACKAGES_TOKEN }}"
DOCKER_REGISTRY: ghcr.io
- name: Deploy to Kubernetes
run: |
# Of course you need to specify server and x509 login certificates
kubectl apply -f "${KUBERNETES_DIR}"
env:
KUBERNETES_DIR: "k8"
© 2022 - 2024 — McMap. All rights reserved.