LinuxCommandLibrary

lxc-create

Create a new Linux container

TLDR

Create a container interactively in /var/lib/lxc/

$ sudo lxc-create [[-n|--name]] [container_name] [[-t|--template]] download
copy

Create a container in a target directory
$ sudo lxc-create [[-P|--lxcpath]] /[path/to/directory]/ [[-n|--name]] [container_name] [[-t|--template]] download
copy

Create a container passing options to a template
$ sudo lxc-create [[-n|--name]] [container_name] [[-t|--template]] download -- [[-d|--dist]] [distro-name] [[-r|--release]] [release-version] [[-a|--arch]] [arch]
copy

Display help
$ lxc-create [[-?|--help]]
copy

SYNOPSIS

lxc-create {-n name} {-t template} [-f config_file] [-B backingstore] [-s size] [-P path] [-L fssize] [-- template-options]

PARAMETERS

-n, --name=name
    Specifies the unique name for the new container. This name will be used to identify and manage the container.

-t, --template=template
    Defines the template script to use for container creation. Common templates include download, ubuntu, debian, busybox, and more. These scripts handle the distribution-specific setup.

-f, --config=config_file
    Provides an alternative path to the container's configuration file. By default, LXC generates a config file at LXC_PATH/containers/name/config.

-B, --backingstore=backingstore
    Sets the storage backend type for the container's root filesystem. Options include dir (directory), btrfs, lvm, zfs, or loop. Each has different performance and management characteristics.

-s, --size=size
    Specifies the size of the backing store for storage types like LVM or ZFS. For example, '10G' or '500M'.

-P, --path=path
    Specifies an alternative root directory where containers will be stored. Default is usually /var/lib/lxc.

-L, --fssize=fssize
    Specifies the maximum size for the container's root filesystem, typically used with LVM or ZFS thin provisioning.

-d, --debug
    Enables verbose debugging output for troubleshooting container creation issues.

-o, --logfile=logfile
    Specifies a file to write debug and log messages to, instead of standard error.

-l, --logpriority=level
    Sets the log priority level for messages, such as FATAL, CRIT, WARN, INFO, or DEBUG.

--
    Separates lxc-create options from arguments that should be passed directly to the chosen template script. For example, specifying a release for an lxc-download template.

DESCRIPTION

lxc-create is a fundamental utility within the Linux Containers project, designed to instantiate new container environments. It leverages specialized template scripts to automate the setup of a container's root filesystem and initial configuration. When executed, lxc-create invokes a specified template (e.g., lxc-ubuntu, lxc-download) which handles tasks such as downloading base images, installing necessary packages, and configuring basic network settings. The result is a lightweight, isolated Linux environment with its own filesystem, process tree, and network interfaces, distinct from the host system. This command is essential for quickly provisioning various types of system containers, from minimal BusyBox installations to full-fledged distribution environments.

CAVEATS

Creating LXC containers typically requires root privileges.
The command's success heavily depends on the chosen template script; network connectivity is often required for templates that download images.
Specific backing store types (e.g., LVM, ZFS) necessitate the corresponding tools and configuration to be present on the host system.
Resource limits (CPU, memory) are not set during creation; they must be configured manually in the container's config file or via other LXC commands after creation.

TEMPLATES

LXC templates are executable scripts, typically found in /usr/share/lxc/templates/, that automate the setup of a container's root filesystem and basic configuration. They define the operating system (e.g., Ubuntu, Debian, Fedora, Alpine, BusyBox) and the initial state of the container. The flexibility of templates allows users to provision diverse container types efficiently. Arguments specified after the '--' option are passed directly to these template scripts, enabling customization like selecting a specific OS release or architecture.

CONTAINER CONFIGURATION FILE

Upon creation, each LXC container receives its own configuration file, commonly located at /var/lib/lxc/<name>/config. This file is crucial as it dictates the container's behavior, including its root filesystem path, network configuration (e.g., bridge, veth pairs), cgroup limits for resource control (CPU, memory), and various mount points. While lxc-create sets up a basic configuration, administrators frequently modify this file post-creation to fine-tune the container's environment, such as assigning static IP addresses or adjusting resource allocations, following the syntax detailed in lxc.container.conf(5).

HISTORY

The LXC (Linux Containers) project emerged around 2008 as a pioneering lightweight virtualization technology, leveraging Linux kernel features like control groups (cgroups) and namespaces. lxc-create has been a cornerstone command since LXC's inception, providing a robust mechanism for provisioning new container instances. Its design, centered on extensible template scripts, has allowed it to adapt to various Linux distributions and support a growing array of storage backends (e.g., LVM, Btrfs, ZFS) as kernel capabilities evolved. While the container ecosystem has seen the rise of alternative technologies like Docker, lxc-create remains integral for users building and managing 'system containers' that closely mimic virtual machines, offering persistent and full operating system environments.

SEE ALSO

lxc(7), lxc-start(1), lxc-stop(1), lxc-destroy(1), lxc-ls(1), lxc-attach(1), lxc.container.conf(5)

Copied to clipboard