How do I copy a file from host into a LXD container?

I have a file on the host that I want to copy into the LXD container.

What is the command to copy the host → container?

1 Like

You need to use the lxc file push subcommand, to push a file from the host to a container.
For example,

lxc file push myfile.txt mycontainer/home/ubuntu/

The destination starts with the container name (mycontainer), and then you append the path (/home/ubuntu/) in my example. Note that you need to put a final / if you want to put a file in a directory, else you get an error.

More at https://blog.ubuntu.com/2016/03/22/lxd-2-0-your-first-lxd-container

5 Likes

Is it possible to do this remotely? I need to copy folders from my computer to the container host and then into the container. I can do this is two steps, like rsync to the container host, then lxc push to the container. Is there a better way to accomplish what I’m trying? Thanks for any tips.

1 Like

Hi!

lxc is a client, and can manage any LXD server (a remote) that you have configured.
To see the list of currently available servers, run the following

lxc remote list

The one called local (default), is your local LXD server. The others, such as ubuntu: and images: are repositories of container and VM images. Those two only serve images and are read-only.

What you can do, is

  1. configure your remote LXD server to be accessible remotely over the network
  2. configure your local lxc client to be able to also manage that remote LXD server of yours.

On the remote LXD server, run the following. Please change mypassword to something really long and unpredictable.

lxc config set core.https_address [::]:8443
lxc config set core.trust_password mypassword

On the local system, run the following. You will be prompted to type the password that you configured on the LXD server earlier. myserver is just a mnemonic for your remote LXD server.

lxc remote add myserver hostname_or_IP_of_LXD_server

Having done that, you can now do the following. That is, the same LXD commands, but you prepend myserver: to specify the remote LXD server.

lxc list myserver:
lxc shell myserver:mycontainer
lxc file push myfile.txt myserver:mycontainer/home/ubuntu/
2 Likes

Is there a function in the lxd go api to push files from the host to a container or to mount a host folder to a container?

Mounting a host folder into the container would be done by adding a disk device, which at the API level would be a PUT on /1.0/instances/NAME.

To push a file, there isn’t one to push a path from the host to the container but you can POST the content directly to /1.0/instances/NAME/files?path=/some/path

I just wanted to leave a go code example using the REST API here that worked for me:
/var/snap/lxd/common/lxd/unix.socket = location of my unix.socket file
a/1.0/instances/simple-monitor/files?path=/home/ubuntu/test.py = simple-monitor is my container name and /home/ubuntu/test.py the target-file i want to have in the container
/home/vm/Downloads/test.py = the source file that i want to push to the container

package main

import (
	"context"
	"fmt"
	"io"
	"net"
	"net/http"
	"os"
	"strings"
)

func main() {
	// http client to make http requests to a unix socket
	httpc := http.Client{
		Transport: &http.Transport{
			DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
				return net.Dial("unix", "/var/snap/lxd/common/lxd/unix.socket")
			},
		},
	}

	response, err := httpc.Post("http://unix"+"a/1.0/instances/simple-monitor/files?path=/home/ubuntu/test.py", "application/octet-stream", strings.NewReader("/home/vm/Downloads/test.py"))
	if err != nil {
		fmt.Println(err)
	}

	io.Copy(os.Stdout, response.Body)
}

the code outputs:

{“type”:“sync”,“status”:“Success”,“status_code”:200,“operation”:“”,“error_code”:0,“error”:“”,“metadata”:{}}

and the file test.py is now in the container.

Thank you to the maintainers in this forum and github you are awsome for always being nice and quick to reply.

1 Like