How to Load a Local Docker Image into the MicroK8s Repository on a Remote Server
When operating Kubernetes on a home server or an in-house server, here’s how to push a Docker image built on your local Mac to the server via SSH, without using an external image repository (like DockerHub or ECR).
Loading a Local Docker Image as a Docker Image on a Remote Server
First, regardless of Kubernetes, here’s how to send an image from your local Docker to a remote Docker:
docker save ${image_name}:${image_tag} | ssh -C username@remote.example.com docker load
With this command, you can directly send your local Docker image to the remote server.
However, compared to the method of using an image repository and using docker push / docker pull, this method does not perform differential updates for updated layers and transmits all layers entirely, making it slower in comparison.
When Using MicroK8s (ctr) on a Remote Server
If you are using MicroK8s on the server, a separate image repository for ctr is created apart from the Docker image repository within the server. Since MicroK8s uses ctr, images loaded with docker load cannot be used.
To import an image into ctr with MicroK8s:
docker save mynginx > myimage.tar
microk8s ctr image import myimage.tar
Reference: https://microk8s.io/docs/registry-images
As shown above, use the ctr image import command. This command does not support importing from standard input, so you need to save the image to a file first.
To load a local image into the remote ctr:
docker save ${image_name}:${image_tag} | ssh -C username@remote.example.com "cat > /tmp/_exported_image.tar"
ssh username@remote.example.com "sudo microk8s ctr image import /tmp/_exported_image.tar"
By writing to a file once and then using microk8s ctr image import, you can import the image.
Since sudo is required, it’s good to register the following in the sudoers file:
/etc/sudoers.d/91-working-users
username ALL=(ALL) NOPASSWD: /snap/bin/microk8s
We look forward to discussing your development needs.