Linux::LXC - Manage your LXC containers.




  use Linux::LXC qw(ALLOW_UNDEF);

  my $c = Linux::LXC->new(
    utsname => 'web',
    template => 'download -- -d debian -r wheezy -a amd64'
  my ($uid_start, $uid_map) =
    $c->get_config('lxc.id_map', qr/^u 0 (\d+) (\d+)$/, ALLOW_UNDEF);
  $c->stop if $c->is_running;
  $c->destroy if $c->is_existing;
  $c->set_config('', '');
  my @cmd = (
    'route add default gw eth0',
    'echo "nameserver" > /etc/resolv.conf',
    'apt-get update -y',
    'apt-get install -y lighttpd'
  map {
    my ($res, $stdout, $stderr) = $c->exec($_);
    die $stderr if $stderr ne '';
  } @cmd;
  $c->put('lighttpd.conf', '/etc/lighttpd/lighttpd.conf');
  $c->put('www', '/var/www');


This module helps you to manage LXC container. Each container will be represented by an object of this module. Some module subroutine are also usable without any object instance.

Module subroutines


Will return an array with the name of all LXC containers existing on the system.


Will return an array with the name of all LXC containers currently running on the system.


Will return an array with the name of all LXC containers currently stopped on the system.


Will return the version number of LXC implementation that runs on the host.

Object methods


Instanciate a new Linux::LXC object. Params that can be initialized:


Mandatory parameter. Set the utsname of the container.


Mandatory only if you planned to deploy the container. Set the LXC template to use for deploying the container.


A Linux::LXC object.


Will deploy the container. Concretly, this method will check that the container is not existing, and after execute `lxc-deploy -n <utsname> -t <template>` shell command.


The previous Linux::LXC object.

del_config($attribute, $filter)

Will delete all LXC configuration container attributes that respect the $filter pattern.


The attribute to delete.


A regex or undef. It will be compared with all $attribute values. The ones that match will be removed. If undef, all values will be removed.


The number of elements deleted.


Will stop the container if it's existing and destroy it with the shell `lxc-destroy -n <utsname>` shell command.


The previous Linux::LXC object.


Will execute the $cmd command in the container. This method use the IPC::Run method, that allow us to don't think at all about priorities between shell operators. Eg: exec('echo "Hello" >> ~/file.txt') will write the file on the container, and not on the instance that actually runs the commnand.

return (if want array)

($result, $stdout, $stderr); $result true if shell command return 0 (it usually means that the command was a success), false otherwise. $stdout and $stderr are self-explaining.

return (if want scalar)

True if shell command return 0 (it usually means that the command was a success), false otherwise.

get_config($paramater, $filter, $flag)

Get an array of values corresponding to all data that match the LXC container configuration.


The parameter to match.


An regex, or undef. Values corresponding of the parameter to keep. If undef, we will keep all of them.


ALLOW_EMPTY: don't croak if the parameter asked was not found.


An array with all matched results.


Return the path to the LXC instance of the container. By default it's /var/lib/lxc/<utsname>/. The path is the folder that contains rootfs and config file.


Get the template of the LXC instance.


Will return the utsname of the container.


Retrun true if the container with the given utsname exists. False otherwise.


Retrun true if the container with the given utsname is running. False otherwise.


Return true if the container with the given utsname is stopped. False otherwise.

put($input, $destination)

Will copy the $input file or folder on the $destination path in the container instance. This method also takes care of ownership and will chown $destination to the container root uid. The ownership will also be set for all intermediate folders we have to create.


String corresponding to a relative or absolute path of a folder or a file we want to copy on the container root fs. This path should be readable by the user executing this script.


Location on the container to put the file or folder. This path has to be absolute.

set_config($attribute, $value, $flag)

Will set a LXC attribute in container configuration. The update can occurs in two modes: addition or erasing. In the first one, a new attribute with the given value will always be created. In the second case, the first previous value already existing of $attribute will be updated with the new $value. If none is found, the atribute will also be created.


Attribute to set.


Value to give to the attribute.


Can be ADDITION_MODE, ERASING_MODE or undef. If undef, ERASING_MODE will occur.


Will set the $template name to the given container. Note that this action should be done before the deployment.


Start the container.


Stop the container.


Spydemon <>


A bug tracker is available for this module at the address: but inscriptions are closed because of spamming issue. If you want an account for contributing, report any enhancement suggestion or bug report, please send me an email.


This software is copyright (c) 2020 by Spydemon.

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <>.