Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Exposing a directory on the host machine to an LXC container

Exposing a directory on the host machine to an LXC container

  1. Log into the container and create an empty directory, this will be the mount point
  2. Log out and stop the container.
  3. Open to your container's config file
    • For regular LXC containers: /var/lib/lxc/mycontainer/config
    • For unprivileged LXC containers: $HOME/.local/share/lxc/mycontainer/config
  4. Add a new line above the lxc.mount directive, that follows the format below. Substitute proper paths as necessary:
    • lxc.mount.entry = /path/to/folder/on/host /path/to/mount/point none bind 0 0
    • Both of these paths are relative to the host machine.
    • Location of the root fs in the container can be found at:
      • For regular LXC containers: /var/lib/lxc/mycontainer/rootfs/
      • For unprivileged LXC containers: $HOME/.local/share/lxc/mycontainer/rootfs

Note: If the host's user does not exist in the container, the container will still be mounted, but with nobody:nogroup as the owner. This may not be a problem unless you need to write to these files, in which case you'll need to give everybody write permission to that folder. (i.e. chmod -R go+w /folder/to/share)

Example

I want to share /home/julianlam/foobar to my unprivileged container bazquux. In bazquux, I want this folder to be found at /mnt/baz.

In the container:

$ cd /mnt
$ sudo mkdir baz
$ logout

In the host, I will add the following line above lxc-mount in /home/julianlam/.local/share/lxc/bazquux/config:

lxc.mount.entry = /home/julianlam/foobar /home/julian/.local/share/lxc/bazquux/rootfs/mnt/baz none bind 0 0

Then

$ lxc-start -n bazquux -d

Further Reading

@hnagri

This comment has been minimized.

Copy link

@hnagri hnagri commented Oct 4, 2016

"If the host's user does not exist in the container, the container will still be mounted, but with nobody:nogroup" I have the exact same problem. Can you please elaborate how to get past this issue?

@Daniel15

This comment has been minimized.

Copy link

@Daniel15 Daniel15 commented Feb 26, 2017

You said:

Add a new line above the lxc.mount directive

But I don't have an lxc.mount directive. This is my whole config:

# Template used to create this container: /usr/share/lxc/templates/lxc-debian
# Parameters passed to the template: -r stretch
# For additional config options, please look at lxc.container.conf(5)

# Uncomment the following line to support nesting containers:
#lxc.include = /usr/share/lxc/config/nesting.conf
# (Be aware this has security implications)

# Masquerade for IPv4
lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = virbr0
lxc.network.hwaddr = 00:16:3e:c1:61:b7

# Bridge for IPv6
lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = br0
lxc.network.hwaddr = 00:16:3e:c1:61:b7

lxc.rootfs = /var/lib/lxc/build03/rootfs
lxc.rootfs.backend = dir

# Common configuration
lxc.include = /usr/share/lxc/config/debian.common.conf

# Container specific configuration
lxc.tty = 4
lxc.utsname = build03
lxc.arch = amd64
@oquidave

This comment has been minimized.

Copy link

@oquidave oquidave commented Apr 7, 2017

You can also use absolute path for debian systems https://wiki.debian.org/LXC#Bind_mounts_inside_the_container

@alex-eri

This comment has been minimized.

Copy link

@alex-eri alex-eri commented Feb 19, 2018

how to bind dir online?

@begleysm

This comment has been minimized.

Copy link

@begleysm begleysm commented Jun 22, 2018

I am using LXC in Proxmox 5.2. My problem may be Proxmox specific but here it goes. I am able to get this functionality working with your guide. I am modifying /var/lib/lxc/103/config. However when I shutdown and restart my container, the changes I made to the config are gone. How/where do I add these lines so that they will persist between reboots of the container?

@jasonhalljax

This comment has been minimized.

Copy link

@jasonhalljax jasonhalljax commented Jul 19, 2018

Same as above - when restarting the container, the config file changes for the mount are gone and must be re-entered. Can’t seem to find anything about this elsewhere.

@terpetter

This comment has been minimized.

Copy link

@terpetter terpetter commented Aug 3, 2018

In Proxmox you must edit the config files in /etc/pve/lxc to make any changes persist during reboots:

root@pve:/etc/pve/lxc# ls
100.conf 102.conf 103.conf 106.conf 107.conf

@korg91

This comment has been minimized.

Copy link

@korg91 korg91 commented Apr 28, 2019

On LXC 3.0.3 (Ubuntu 16.04), this guide seems to work perfectly, but the mount point must be given as a relative path for the container. This is what the Debian wiki recommends.

So the line in the example above should be instead:
lxc.mount.entry = /home/julianlam/foobar baz none bind 0 0

@kokizzu

This comment has been minimized.

Copy link

@kokizzu kokizzu commented Dec 11, 2019

i prefer using command line '__')

lxc config device add CONTA1 shareddir1 disk path=/root/test1 source=/home/`whoami`/test1

this will mount /home/whoami/test1 to container's /root/test1

@ernierasta

This comment has been minimized.

Copy link

@ernierasta ernierasta commented Feb 18, 2020

@kokizzu, you are talking about LXD (which provides also lxc command). But it is about LXC, so commands are lxc-*.
LXC has no command for mounting, from what I know.

@Hillsie

This comment has been minimized.

Copy link

@Hillsie Hillsie commented Sep 6, 2020

Been trying a number of recommendation and finally found @kokizzu example, which worked. Written it below a little more explicitly so I can recall and understand.

Permissions in the container are an issue, but at least I can see the file created in the host.

lxc config device add YourLxcContainersName sharename disk path=/home/hosts/share source="/home/lxcshare" Used parens as the path had a space.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.