Moving a file from a container to the host?

So I want to move a file from inside an LXD container to a folder on the host.

I run a script in the container that creates the file, I just need to move it to the host from the script. The host is Ubuntu 16.04.

So I need to incorporate something in my script to automatically move the file once it is created.




i suppose you could use scp since its your own host that you are sending the file to.

you would use the following command from in your container lets say the file is called “foo”



scp foo najib@

hope this resolves your issue

Thanks najib. You got me started. As I will be running this inside of a script, I need to be able to pass the password to scp. I found pscp that does the trick. Thanks again.


You could create a ssh private and public keys and have trust in between the two.

These steps are to be done in the container.

Step 1
Create key

ssh-keygen -t rsa

keep defaults press enter until prompt comes back

Step 2
Copy the key to the host

ssh-copy-id hostuser@your_ip_here

and that should be it. Scp should not prompt you for a password no more.

There is also the option of sharing some disk space (a directory) from the host to the container.
Then, the container can write into that directory and the host can read from there.

What is important here, is that we generally consider that the containers are trusted less than the host.
Therefore, if you set up SSH to SSH from the container to the host, then you end up putting your SSH private key in the container (not so good because we do not trust the containers that much).

Obviously, if your use-case is such that you can trust the container, you may very well do so (and use SSH).

1 Like

Are you running Ubuntu in the container?

Yes I am.

When I was in the same situation, I used cloud-init to download the file using http during the first boot. The container’s config or the profile should look something like this:

  user.user-data: |
    # Download a file
      - /usr/bin/wget -q -O <target path on local fs> http://my.web.server/file_to_be_downloaded

Note: Cloud-init runs only once (first boot), not during every boot.