Import a docker image in kubernetes
In this article we are going to explain how to import a docker image in kubernetes and make it a running pod just with few easy commands. Previously we explained here https://brewedbrilliance.net/kubernetes-pod-using-a-local-image/ how to spin up a pod using your local built image. That was a great experience I suspect, however the general rule is everything works great on your PC until you have to move to the server! Or…if you have in some way test (preferably before destroying anything) in a safe environment which could easily be your local dev server where you are happy to experiment. In the example below the assumptions made are
- you have a docker image on your PC/Laptop/Mac/Remote Linux system
- that you want to import that image into your dev kubernetes/k3s so that you can test in a semi-close-to-reality environment (you have full control on this dev kubernetes/k3s)
- k3s is running on a machine on which you can run command as administrator
Said that….
With these small steps I hope you could use these commands for importing a locally built image into your kubernetes space and use for creating a pod.
First of all we have to export the image, so from your computer/remote workstation or where you have your docker running, run this command for listing the images
brewed@brilliance:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my_local_image1 latest 2a11a0a4ba7f 3 days ago 94.2MB
my_local_image2 latest ffdbc3d69fc3 3 days ago 94.2MB
...
Let’s assume we want to export my_local_image1
We will export by running this command
docker save my_local_image1 > my_local_image1.tar
This will generate a tar file that must be uploaded to a location from which your machine can have access. Once done that (lets assume we scp to user@remote-machine:/images/external) all you have to do is to run this command that will import the image in the local registry (k3s or k8s or microk8s are the same for this command)
sudo k3s ctr image import /images/external/my_local_image1.tar
You can check that the image has been imported correctly by running this command
sudo k3s ctr image ls | grep my_local_image1
And now you can spin up a new pod using my_local_image as name and as tag you can use latest, something similar to the below
---
apiVersion: v1
kind: Pod
metadata:
name: my_local_image1
namespace: default
spec:
containers:
- name: my_local_image1
image: my_local_image1:latest
imagePullPolicy: Never
restartPolicy: Never
Once you have verified that everything is working as expected you can then merge your changes to your main branch and kick the CI/CD.
If you liked this article…share and help us grow!!
Share this content: