Copy files and directories between host and a LXD container?
In this tutorial, we will show you how to use the “push” and “pull” command to easily copy files and directories between a host server and a LXC container and visa versa.
In this example, on the host server, we will first create a file “myfile.txt” in the “tmp” directory.
# touch /tmp/myfile.txt
Now copy the file to our container “my-container” using the “lxc file push” command.
# lxc file push /tmp/myfile.txt my-container/tmp/
To show how this works with directories, we create on the host server a directory “foo” with 2 sub directories “test1” and “test2” in the “tmp” directory.
# mkdir -p /tmp/foo/{test1,test2}
Create a file “myfile.txt” in the directory “/tmp/foo/test1”.
# touch /tmp/foo/test1/myfile.txt
Copy the directory with sub-directories and all file to our container “my-container” using the command “lxc file push -r”.
# lxc file push -r /tmp/foo/ my-container/tmp/
You can also copy files and folders from the container to the host with the pull command.
Let’s first remove the foo folder with contents.
# rm -r /tmp/foo/
To restore the folder, we use the “pull” command to copy the folder still in our container “my-container” to the “tmp” folder on our host.
# lxc file pull -r my-container/tmp/foo/ /tmp/
Of course, you can also use the “pull” command to copy a single file from a container to our host.
# lxc file pull my-container/tmp/foo/test1/myfile.txt /tmp/