Search code examples
dockerxargscontainerdnerdctl

How can I load multiple tar images using nerdctl? (containerd)


There are around 10 container image files on the current directory, and I want to load them to my Kubernetes cluster that is using containerd as CRI.

[root@test tmp]# ls -1
test1.tar
test2.tar
test3.tar
...

I tried to load them at once using xargs but got the following result:

[root@test tmp]# ls -1 | xargs nerdctl load -i
unpacking image1:1.0 (sha256:...)...done
[root@test tmp]#

The first tar file was successfully loaded, but the command exited and the remaining tar files were not processed.

I have confirmed the command nerdctl load -i succeeded with exit code 0.

[root@test tmp]# nerdctl load -i test1.tar
unpacking image1:1.0 (sha256:...)...done
[root@test tmp]# echo $?
0

Does anyone know the cause?


Solution

  • Your actual ls command piped to xargs is seen as a single argument where file names are separated by null bytes (shortly said... see for example this article for a better in-depth analyze). If your version of xargs supports it, you can use the -0 option to take this into account:

    ls -1 | xargs -0 nerdctl load -i
    

    Meanwhile, this is not really safe and you should see why it's not a good idea to loop over ls output in your shell

    I would rather transform the above to the following command:

    for f in *.tar; do
      nerdctl load -i "$f"
    done