Today, I wanted to copy some files to the container filesystem of an lxc container I was spinning up.
There are a number of different approaches to achieve this, and they’re all great, but I wanted the simplest thing I could do, rather than grepping through fstab records, and copying into arcane folders, etc.
I’m using lxc / lxd on Ubuntu Xenial, so it’s a little different than a lot of the advice I found for earlier versions of lxc.
In a nutshell, I just did:
find ~/.ssh/id_rsa* | cpio -o | lxc exec ${CONTAINER_NAME} -- cpio -i -d -v
-
The first command
find ~/.ssh/id_rsa*
just lists the names of the files that match that search pattern. -
The output of that is piped to the second command
cpio -o
, which just encodes the contents of those files as a cpio package and outputs it on stdout. -
That is piped into the stdin of
cpio -i -d -v
within the container using thelxc exec ${CONTAINER_NAME} -- <container command>
command. Briefly the-d
option creates the directories if they are missing, and-v
shows the names of the files as they’re being extracted from the stream.Note
This works with any process that accepts stdin, for example with ssh:
find .ssh/id_rsa* | cpio -o | ssh remote-host cpio -i -d -v
(of course, if you can
ssh
you might as well usescp
to copy the files instead.)