[lxc-users] Howto/Tutorial wanted: Running an application inside a container

Fajar A. Nugraha list at fajar.net
Sun May 11 22:27:38 UTC 2014


On Mon, May 12, 2014 at 12:38 AM, Johannes Kastl <mail at ojkastl.de> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi Fajar,
>
> On 11.05.2014 07:10 Fajar A. Nugraha wrote:
>> On Sun, May 11, 2014 at 1:18 AM, Johannes Kastl <mail at ojkastl.de>
>> wrote:
>> https://www.stgraber.org/2014/02/09/lxc-1-0-gui-in-containers/ is a
>> good example
>
> Sorry, but that one talks about installing a complete OS in the container:

Correct. But the created container WOULD work to run only a single
application as well with some minor modifications (e.g. specificing
container networking options in the config file).


>> Like I mentioned above, it's easiest to just install full OS in
>> the container, or bind-mount some parts of the host. If you're
>> pressed for disk space, using something like btrfs/zfs
>> snapshot/clone would help.
>
> Basically I just wanted to avoid having to run a complete
> installation, cos that means e.g. keeping this one up to date as well.
> As disk space is not the main reason, having to run a complete OS just
> kind of takes away advantages LXC has in contrast to KVM...
>
> But bind-mounting seems like a good way, that way updates on the host
> are enough...

This example should help you get started. It assumes:
- you're using fully-up-to-date ubuntu trusty
- you have /opt/container/root and /opt/containter/data directories,
which are empty (e.g. freshly created by mkdir)

First, create a new root for the container by using overlay of the host

mount -t overlayfs -o lowerdir=/,upperdir=/opt/container/data
overlayfs /opt/container/root

Next, use this sample container config file to get started (I put this
as /var/lib/lxc/host/config, so the container name will be "host")

###########
lxc.include = /usr/share/lxc/config/ubuntu.common.conf
lxc.include = /usr/share/lxc/config/ubuntu.userns.conf
lxc.arch = x86_64
lxc.utsname = container

# use the overlay root
lxc.rootfs = /opt/container/root

# use hosts usr and lib, so updates on the host should propagate to the
lxc.mount.entry = /usr usr none bind,ro
lxc.mount.entry = /lib lib none bind,ro

# use bash as init to prevent running full startup sequence on lxc-start
lxc.mount.entry = /bin/bash sbin/init none bind,ro

lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = lxcbr0
lxc.network.hwaddr = 00:16:3e:ef:b5:6d
lxc.network.veth.pair=v-container-0

#static network setup for veth in lxcbr0
lxc.network.ipv4=10.0.3.11/24
lxc.network.ipv4.gateway=10.0.3.1
###########

Last, start an application (in my example, /usr/bin/top, which
requires TERM to be set) like this:

lxc-execute -n host -- env TERM=$TERM sudo -i /usr/bin/top

It should, in most part, do what you want: start a container to run an
application, then stop the container when the application exits.
Again, personally I prefer to simply use full OS in the container
(even with the consequence oh having to update it separately from the
host) combined with lxc-start and lxc-attach.

-- 
Fajar


More information about the lxc-users mailing list