LXC: How do I mount a folder from the host to the container?
Solution 1
To create the directory automatically in the container, you can also add the create=dir
option in the fstab :
/mnt/ssd/solr_data /var/lib/lxc/Solr4StandAlone/rootfs/data none bind,create=dir
Edit : this is specific to LXC. See this thread
Just like we already had "optional", this adds two new LXC-specific mount flags:
create=dir (will do a mkdir_p on the path)
create=file (will do a mkdir_p on the dirname + a fopen on the path)
This was motivated by some of the needed bind-mounts for the unprivileged containers.
Solution 2
In /var/lib/lxc/Solr4StandAlone/config
add a line reading:
lxc.mount.entry = /mnt/ssd/solr_data /var/lib/lxc/Solr4StandAlone/rootfs/data none bind 0 0
Then lxc-stop
stop your container and lxc-start
your container again.
That is all that is needed.
ref: reference link
Solution 3
I had to create the /data
folder in the local container before the mount would work properly.
I also used this fstab entry:
/mnt/ssd/solr_data /var/lib/lxc/Solr4StandAlone/rootfs/data none bind 0 0
Solution 4
As of 2015/09/30 a change resulting from a security patch breaks mounting to an absolute path with lxc.mount.entry in the config file.
Instead you can use a relative path
lxc.mount.entry = /mnt/ssd/solr_data data none bind 0 0
See: https://wiki.debian.org/LXC#Bind_mounts_inside_the_container
Solution 5
As LXC has changed over time this can be very simple, but it stumped me for a bit, so wanted to contribute. I also created a gist for this so i can remember myself, but simply using lxc config device will do the trick.
sudo lxc config device add Solr4StandAlone sdb disk source=/var/lib/lxc/Solr4StandAlone/rootfs/data path=mnt/ssd/solr_data
Note It is important to leave the front slash off the path argument due to a change mentioned by @biscuitNinja
Related videos on Youtube
alok chauve
Updated on September 18, 2022Comments
-
alok chauve over 1 year
I'm trying to mount a folder on the host to an LXC container.
The host has a folder
/mnt/ssd/solr_data
created (this is currently on the root filesystem, but later I'll mount an SSD drive there, so I'm prepping for that).I want that folder to mount as
/data
in the container.So in the containers fstab file I have the following:
/mnt/ssd/solr_data /var/lib/lxc/Solr4StandAlone/rootfs/data ext4 defaults,noatime 0 0
But that's a no-go, I get this error starting the container:
lxc-start: No such file or directory - failed to mount '/mnt/ssd/solr_data' on '/usr/lib/x86_64-linux-gnu/lxc//data' lxc-start: failed to setup the mounts for 'Solr4StandAlone' lxc-start: failed to setup the container lxc-start: invalid sequence number 1. expected 2 lxc-start: failed to spawn 'Solr4StandAlone'
-
0xC0000022L almost 10 yearsWhat version of
mount
does this apply to? I couldn't find the option described inmount(8)
on Ubuntu 14.04, for example. -
0xC0000022L almost 10 yearsAlso not in the latest
mount(8)
-
little-dude almost 10 yearsindeed... looks like it works with lxc only. See this thread on lxc-devel ML
-
Huygens over 8 yearsYour solution should get better review as it works with unprivileged LXC containers tool. The other ones won't work in this case. And probably selinux/apport would need to be tweaked to allow their method. +1 for your solution!
-
alok chauve over 8 yearsGreat addition, it works as an answer, it'll get upvoted so people see it soon. Thanks for adding to it!
-
imz -- Ivan Zakharyaschev almost 8 years
bind
mount option is something that was missing in the OP. I believe it is the most important correction (present also in all the other working answers). -
0_0 over 2 yearsThe ubuntu manpage has a nice example:
lxc config device add [<remote>:]container1 <device-name> disk source=/share/c1 path=opt
, wheresource
is on the host machine andpath
lies within the container.