Retrieving a Lost Docker Image Tag from a k3s Cluster
Background
If your docker image tag has been mistakenly deleted from Gitlab Container Registry and you do have it on k3s cluster on any Dev/QA server. Then you can push that image to GitLab by following this guide.
Step 1 :
List all images in a specific namespace and filter for the desired image:
CONTAINERD_ADDRESS=/run/k3s/containerd/containerd.sock ctr -n <namespace> i ls | grep <repository_url>:<tag>
Example:
CONTAINERD_ADDRESS=/run/k3s/containerd/containerd.sock ctr -n k8s.io i ls | grep gitimages.expertflow.com/cim/reporting-connector/build:1.0
Step 2 :
Export the desired image to a tar file:
CONTAINERD_ADDRESS=/run/k3s/containerd/containerd.sock ctr -n <namespace> i export <filename.tar> <repository_url>:<tag>
Example:
CONTAINERD_ADDRESS=/run/k3s/containerd/containerd.sock ctr -n k8s.io i export reporting-connector.tar gitimages.expertflow.com/cim/reporting-connector/build:1.0
Step 3 :
Import the tar file into a specific namespace:
CONTAINERD_ADDRESS=/run/k3s/containerd/containerd.sock ctr -n <import_namespace> i import <filename.tar>
Example:
CONTAINERD_ADDRESS=/run/k3s/containerd/containerd.sock ctr -n ef i import reporting-connector.tar
Step 4 :
Push the imported image to a gitlab repository:
CONTAINERD_ADDRESS=/run/k3s/containerd/containerd.sock ctr -n <import_namespace> i push -u <username:password> --platform <platform> <repository_url>:<tag>
Example:
CONTAINERD_ADDRESS=/run/k3s/containerd/containerd.sock ctr -n ef i push -u username:password --platform linux/amd64 gitimages.expertflow.com/cim/reporting-connector/build:1.0
It is important to note that the commands above will only work if the user executing the script has the appropriate permissions to access the k3s cluster, export and import images, and push images to the specified gitlab repository.
The above commands are using ctr(containerd client) to interact with the k3s cluster and containerd, ctr is a command-line tool for interacting with containerd. It allows you to manage images, containers, and other resources that containerd manages, it also allows you to interact with the containerd API.
Credits : The DevOps Team (@a user , Saif Ullah , Talha Farooqi)