node-lxc
    Preparing search index...

    Type Alias Container

    type Container = {
        get configFileName(): string;
        get configPath(): string;
        set configPath(path: string): void;
        get daemonize(): boolean;
        set daemonize(value: boolean): void;
        get defined(): boolean;
        get error(): { num: number; string: string | null };
        get initPID(): number;
        get name(): string;
        set name(newName: string): void;
        get running(): boolean;
        get state(): ContainerState;
        addDeviceNode(src_path: string, dest_path?: string): Promise<void>;
        attach(options?: Partial<lxc_attach_options>): Promise<number>;
        attachInterface(dev: string, dst_dev?: string): Promise<void>;
        checkpoint(
            directory: string,
            stop?: boolean,
            verbose?: boolean,
        ): Promise<void>;
        clearConfig(): void;
        clearConfigItem(key: string): void;
        clone(options: lxc_clone_options): Promise<Container>;
        console(
            ttynum: number,
            stdio: [number, number, number],
            escape: number,
        ): Promise<void>;
        consoleAsync(ttynum?: number): Promise<ConsoleSession>;
        consoleGetFds(ttynum?: number): Promise<[number, number]>;
        consoleLog(options?: Partial<lxc_console_log>): Promise<string>;
        create(
            options: {
                argv?: string[];
                bdev_specs?: Partial<bdev_specs>;
                bdevtype?: string;
                flags?: LXC_CREATE;
                template?: string;
            },
        ): Promise<void>;
        destroy(
            options?: { force?: boolean; include_snapshots?: boolean },
        ): Promise<void>;
        detachInterface(dev: string, dst_dev?: string): Promise<void>;
        devptsFd(): Promise<number>;
        exec(
            options: Partial<lxc_attach_options> & { argv: string[] },
        ): Promise<number>;
        execAsync(
            options: Omit<Partial<lxc_attach_options>, "stdio"> & {
                argv: string[];
            },
        ): Promise<ExecSession>;
        execOutput(
            options: Omit<Partial<lxc_attach_options>, "stdio"> & {
                argv: string[];
            },
        ): Promise<ExecOutputResult>;
        freeze(): Promise<void>;
        getCGroupItem(subsys: string): string | undefined;
        getConfigItem(key: string): string | null;
        getConfigItems(prefix?: string): Record<string, string | null>;
        getInterfaces(): Promise<string[]>;
        getIPs(iface: string, family: "inet"): Promise<string[]>;
        getIPs(iface: string, family: "inet6", scope: number): Promise<string[]>;
        getKeys(prefix?: string): string[];
        getRunningConfigItem(key: string): string | null;
        initPIDFd(): Promise<number>;
        loadConfig(alt_file: string): Promise<void>;
        mayControl(): boolean;
        migrate(cmd: LXC_MIGRATE, options?: Partial<migrate_opts>): Promise<void>;
        mount(
            source: string,
            target: string,
            filesystemtype: string | undefined,
            mountflags: number | bigint,
            mnt: lxc_mount,
        ): Promise<void>;
        reboot(timeout?: number): Promise<boolean>;
        removeDeviceNode(src_path: string, dest_path?: string): Promise<void>;
        restore(directory: string, verbose?: boolean): Promise<void>;
        save(alt_file: string): Promise<void>;
        seccompNotifyFd(): Promise<number>;
        seccompNotifyFdActive(): Promise<number>;
        setCGroupItem(subsys: string, value: string): void;
        setConfigItem(key: string, value: string): void;
        setTimeout(timeout: number): boolean;
        shutdown(timeout?: number): Promise<boolean>;
        snapshot(commentfile: string): Promise<number>;
        snapshotDestroy(snapname: string): Promise<void>;
        snapshotDestroy(all: true): Promise<void>;
        snapshotList(): Promise<lxc_snapshot[]>;
        snapshotRestore(snapname: string, newname?: string): Promise<void>;
        start(useinit?: number, argv?: string[]): Promise<void>;
        stats(): Promise<ContainerStats>;
        stop(): Promise<void>;
        umount(
            source: string,
            mountflags: number | bigint,
            mnt: lxc_mount,
        ): Promise<void>;
        unfreeze(): Promise<void>;
        wait(state: ContainerState, timeout?: number): Promise<void>;
        wantCloseAllFds(state: boolean): Promise<void>;
        new (name: string, configPath?: string, alt_file?: string): Container;
    }
    • Parameters

      • name: string
      • OptionalconfigPath: string
      • Optionalalt_file: string

      Returns Container

    Index

    Accessors

    • get configPath(): string

      Full path to the container's configuration directory. Each container can have a custom configuration path; by default it is the lxc.lxcpath global config value (typically /var/lib/lxc).

      Returns string

    • set configPath(path: string): void

      Parameters

      • path: string

      Returns void

    • get daemonize(): boolean

      Whether the container wishes to be daemonized (detached from the controlling terminal) on start.

      Returns boolean

    • set daemonize(value: boolean): void

      Parameters

      • value: boolean

      Returns void

    • get defined(): boolean

      Determine whether the container configuration file exists (/var/lib/lxc/$name/config). Does not indicate whether the container is running.

      Returns boolean

      true if the container is defined (config exists), false otherwise.

    • get error(): { num: number; string: string | null }

      Last error number and human-readable description of the last error that occurred.

      Returns { num: number; string: string | null }

    • get initPID(): number

      Process ID of the container's init process as seen from outside the container. Returns -1 if the container is not running.

      Returns number

    • get name(): string

      Name of the container. Getting this property calls lxc_container.name directly. Setting this property calls rename — the container must be stopped.

      Returns string

      rename

    • set name(newName: string): void

      Parameters

      • newName: string

      Returns void

    • get running(): boolean

      Determine whether the container is currently running.

      Returns boolean

      true if running, false otherwise.

    Methods

    • Add a host device node into a running container.

      Parameters

      • src_path: string

        {string} Full path of the device on the host (e.g. "/dev/sdb").

      • Optionaldest_path: string

        {string | undefined} Path inside the container. Defaults to src_path.

      Returns Promise<void>

      Resolves on success; rejects on error.

    • Open a shell inside the container and wait for it to exit. Uses lxc_attach_run_shell internally, which spawns /bin/sh (or the container's default shell). The shell inherits the attach options (namespaces, environment, uid/gid, etc.).

      Parameters

      • Optionaloptions: Partial<lxc_attach_options>

        {Partial<lxc_attach_options>} Attach options. Uses LXC defaults if omitted.

      Returns Promise<number>

      Exit code of the shell process.

    • Move a host network interface into a running container.

      Parameters

      • dev: string

        {string} Name of the network device on the host (e.g. "eth1").

      • Optionaldst_dev: string

        {string | undefined} Name to use inside the container. Defaults to dev.

      Returns Promise<void>

      Resolves on success; rejects on error.

    • Checkpoint a running container using CRIU. The container state is written to directory and the container can optionally be stopped afterwards.

      Parameters

      • directory: string

        {string} Directory path to write the checkpoint images to.

      • Optionalstop: boolean

        {boolean} Stop the container after checkpointing. Default false.

      • Optionalverbose: boolean

        {boolean} Enable verbose CRIU logging. Default false.

      Returns Promise<void>

      Resolves on success; rejects on error.

    • Completely clear the container's in-memory configuration. Does not affect the configuration file on disk. Call loadConfig to reload from disk.

      Returns void

    • Clear a single key from the container's in-memory configuration.

      Parameters

      • key: string

        {string} Name of the configuration key to remove (e.g. "lxc.net.0.type").

      Returns void

    • Clone (copy) a stopped container into a new container. The source container must be stopped.

      Parameters

      • options: lxc_clone_options

        {lxc_clone_options} Clone parameters including new name, backing store, and flags.

      Returns Promise<Container>

      Resolves with a Container instance for the new clone.

      If bdevtype is not specified and LXC_CLONE_SNAPSHOT is set in flags, the native backing store snapshot mechanism is used if available, otherwise overlayfs is used.

      clone

    • Allocate and run a console TTY, blocking until the user exits. This connects the calling process's stdio to the container console. Use consoleAsync instead for non-blocking programmatic access.

      Parameters

      • ttynum: number

        {number} TTY number to allocate (-1 = first available, 0 = console).

      • stdio: [number, number, number]

        {[number, number, number]} File descriptors [stdinfd, stdoutfd, stderrfd] to use.

      • escape: number

        {number} Escape character as integer (1 = Ctrl+a, 2 = Ctrl+b, …).

      Returns Promise<void>

      Resolves when the user exits the console.

    • Allocate a console TTY and return an async event-emitting session object. The returned ConsoleSession emits data events with Buffer chunks and a close event when the session ends. Supports write(data), resize(cols, rows), and close(). This is a custom enhancement not directly in the LXC C API.

      Parameters

      • Optionalttynum: number

        {number} TTY number to allocate, or -1 for the first available. Default -1.

      Returns Promise<ConsoleSession>

      Active console session.

    • Allocate a console TTY file descriptor pair for the container without attaching to it. The returned ttyfd keeps the TTY allocated; close both descriptors when done.

      Parameters

      • Optionalttynum: number

        {number} TTY number to allocate, or -1 for the first available. Default -1.

      Returns Promise<[number, number]>

      Tuple of [ttyfd, ptxfd] — the TTY and pseudoterminal master fds.

    • Query or clear the container's in-kernel console ring buffer log.

      Parameters

      • Optionaloptions: Partial<lxc_console_log>

        {Partial<lxc_console_log>} Options controlling whether to read, clear, and how many bytes to return.

      Returns Promise<string>

      The console log data as a string.

    • Create a new container using a template or a pre-built image. This runs the template script (e.g. download, ubuntu) to populate the rootfs.

      Parameters

      • options: {
            argv?: string[];
            bdev_specs?: Partial<bdev_specs>;
            bdevtype?: string;
            flags?: LXC_CREATE;
            template?: string;
        }
        • Optionalargv?: string[]

          {string[]} Arguments to pass to the template script (e.g. distro/release/arch).

        • Optionalbdev_specs?: Partial<bdev_specs>

          {Partial<bdev_specs>} Backing store parameters (LVM VG, ZFS root, etc.).

        • Optionalbdevtype?: string

          {string} Backing store type ("dir", "lvm", "zfs", "rbd", etc.). Default "dir".

        • Optionalflags?: LXC_CREATE

          {LXC_CREATE} Creation flags (e.g. LXC_CREATE.QUIET).

        • Optionaltemplate?: string

          {string} Template name to use (e.g. "download", "ubuntu"). Default "none".

      Returns Promise<void>

      Resolves when the container has been created; rejects on error.

      create

    • Delete the container and optionally its snapshots. The container must be stopped before calling this unless force is set. Internally calls destroy_with_snapshots when include_snapshots is true.

      Parameters

      • Optionaloptions: { force?: boolean; include_snapshots?: boolean }
        • Optionalforce?: boolean

          {boolean} Stop the container first if it is running. Default false.

        • Optionalinclude_snapshots?: boolean

          {boolean} Destroy all snapshots too. Default false.

      Returns Promise<void>

      Resolves on success; rejects if destruction fails.

    • Remove a network interface from a running container and return it to the host.

      Parameters

      • dev: string

        {string} Name of the network device inside the container.

      • Optionaldst_dev: string

        {string | undefined} Name to restore on the host. Defaults to dev.

      Returns Promise<void>

      Resolves on success; rejects on error.

    • Retrieve a mount fd for the container's devpts instance. This fd can be used to access the container's pseudo-terminal device hierarchy.

      Returns Promise<number>

      The mount fd for the container's devpts.

    • Run a command inside the container and wait for it to exit. Uses lxc_attach_run_command internally. argv[0] is the program to run; subsequent entries are its arguments.

      Parameters

      • options: Partial<lxc_attach_options> & { argv: string[] }

        {Partial<lxc_attach_options>} Additional attach options (uid, gid, env, namespaces, etc.).

        • argv

          {string[]} Command and arguments (e.g. ["/bin/ls", "-la", "/tmp"]).

        • argv: string[]

          {string[]} Command and arguments (e.g. ["/bin/ls", "-la", "/tmp"]).

      Returns Promise<number>

      Exit code of the command that ran inside the container.

    • Run a command inside the container and stream its output as events. Resolves with a session object once the process has started. Register all listeners before the first await after this call so no output is lost between process start and listener registration.

      Parameters

      • options: Omit<Partial<lxc_attach_options>, "stdio"> & { argv: string[] }

        {Partial<lxc_attach_options>} Attach options. stdio is ignored.

        • argv

          {string[]} Command and arguments.

        • argv: string[]

          {string[]} Command and arguments.

      Returns Promise<ExecSession>

      Active streaming session.

      const session = await c.execAsync({ argv: ['/usr/bin/top', '-b', '-n1'] });
      session
      .on('stdout', chunk => process.stdout.write(chunk))
      .on('stderr', chunk => process.stderr.write(chunk))
      .on('exit', code => console.log('exit:', code));
    • Run a command inside the container and capture its output. Internally creates pipes for stdout and stderr so the child never writes to the caller's terminal. Both streams are collected in memory and returned when the process exits.

      Parameters

      • options: Omit<Partial<lxc_attach_options>, "stdio"> & { argv: string[] }

        {Partial<lxc_attach_options>} Attach options (uid, gid, env, …). stdio is ignored.

        • argv

          {string[]} Command and arguments.

        • argv: string[]

          {string[]} Command and arguments.

      Returns Promise<ExecOutputResult>

      { exitCode, stdout, stderr }.

    • Freeze a running container (pause all processes via cgroup freezer). The container must be in the RUNNING state; it will transition to FROZEN.

      Returns Promise<void>

      Resolves when frozen; rejects on failure.

      freeze

    • Retrieve the value of a cgroup subsystem entry for the container. Returns undefined if the subsystem does not exist or is not available.

      Parameters

      • subsys: string

        {string} cgroup subsystem path (e.g. "memory.usage_in_bytes", "cpu.stat").

      Returns string | undefined

      The cgroup value string, or undefined.

    • Retrieve the value of a configuration key from the container's in-memory config. Returns null if the key does not exist or has no value.

      Parameters

      • key: string

        {string} Configuration key to retrieve (e.g. "lxc.net.0.type").

      Returns string | null

      The value string, or null if absent.

    • Retrieve all configuration keys and their values in a single call. More efficient than calling getKeys(prefix) followed by N getConfigItem(key) calls.

      Parameters

      • Optionalprefix: string

        {string | undefined} Optional key prefix to filter by (e.g. "lxc.net"). Pass undefined to retrieve all keys.

      Returns Record<string, string | null>

      Object mapping each key to its value, or null if the key exists but has no value.

    • Retrieve all network interfaces currently visible inside the container. The container must be running.

      Returns Promise<string[]>

      Array of interface name strings (e.g. ["lo", "eth0"]).

    • Retrieve IPv4 addresses assigned to a container interface. The container must be running.

      Parameters

      • iface: string

        {string} Network interface name (e.g. "eth0").

      • family: "inet"

        {"inet"} Address family — use "inet" for IPv4.

      Returns Promise<string[]>

      Array of IPv4 address strings in dotted-decimal notation.

    • Retrieve IPv6 addresses assigned to a container interface. The container must be running.

      Parameters

      • iface: string

        {string} Network interface name (e.g. "eth0").

      • family: "inet6"

        {"inet6"} Address family — use "inet6" for IPv6.

      • scope: number

        {number} IPv6 scope ID to filter by (e.g. 0 for global).

      Returns Promise<string[]>

      Array of IPv6 address strings.

    • Retrieve all configuration keys that match the given prefix. Use this to enumerate available keys, then call getConfigItem on each. For bulk reads, prefer getConfigItems which fetches keys and values in one call.

      Parameters

      • Optionalprefix: string

        {string} Key prefix to filter by (e.g. "lxc.net"). Pass undefined for all keys.

      Returns string[]

      Array of matching key strings.

    • Retrieve the value of a configuration key from a running container's live state. Unlike getConfigItem, this reads values that may differ from the on-disk config (e.g. effective network settings, runtime cgroup values). Returns null if the key does not exist or the container is not running.

      Parameters

      • key: string

        {string} Configuration key to retrieve.

      Returns string | null

      The live value string, or null if absent.

    • Retrieve a pidfd for the container's init process. A pidfd is a file descriptor that refers to a process and is immune to PID reuse. Useful for safely signalling or polling the container's init with waitid(P_PIDFD).

      Returns Promise<number>

      The pidfd of the container's init process.

    • Load the specified configuration file for the container, replacing any previously loaded config.

      Parameters

      • alt_file: string

        {string} Full path to the alternate configuration file to load.

      Returns Promise<void>

      Resolves on success; rejects if the file cannot be loaded.

    • Determine whether the calling process has sufficient privileges to control this container. Useful as a pre-flight check before attempting operations that require control privileges.

      Returns boolean

      true if the caller may control the container.

    • Perform a migration operation (dump, restore, or pre-dump) using CRIU. Use the LXC_MIGRATE enum values for cmd.

      Parameters

      • cmd: LXC_MIGRATE

        {LXC_MIGRATE} Operation to perform (MIGRATE_DUMP, MIGRATE_RESTORE, or MIGRATE_PRE_DUMP).

      • Optionaloptions: Partial<migrate_opts>

        {Partial<migrate_opts>} Migration options including directory, page server address, etc.

      Returns Promise<void>

      Resolves on success; rejects on error.

    • Bind-mount a host path into the running container. Equivalent to calling mount(2) inside the container's mount namespace.

      Parameters

      • source: string

        {string} Host filesystem path to mount.

      • target: string

        {string} Mount point inside the container.

      • filesystemtype: string | undefined

        {string | undefined} Filesystem type (e.g. "tmpfs"), or undefined for bind mounts.

      • mountflags: number | bigint

        {bigint | number | LXC_MOUNT} Flags passed to mount(2) (e.g. MS_BIND, MS_RDONLY).

      • mnt: lxc_mount

        {lxc_mount} Mount API version descriptor. Set version to 0.

      Returns Promise<void>

      Resolves on success; rejects on error.

      mount

    • Request the container to reboot by sending it SIGINT to its init process. Waits up to timeout seconds for the reboot to complete. Internally uses reboot2 which supports a timeout parameter.

      Parameters

      • Optionaltimeout: number

        {number} Seconds to wait; -1 waits forever, 0 does not wait. Default -1.

      Returns Promise<boolean>

      Resolves with true if the reboot completed within the timeout.

    • Remove a device node from a running container.

      Parameters

      • src_path: string

        {string} Full path of the device on the host.

      • Optionaldest_path: string

        {string | undefined} Path inside the container. Defaults to src_path.

      Returns Promise<void>

      Resolves on success; rejects on error.

    • Restore a container from a CRIU checkpoint directory.

      Parameters

      • directory: string

        {string} Directory path containing the CRIU checkpoint images.

      • Optionalverbose: boolean

        {boolean} Enable verbose CRIU logging. Default false.

      Returns Promise<void>

      Resolves on success; rejects on error.

    • Persist the container's in-memory configuration to a file.

      Parameters

      • alt_file: string

        {string} Full path to the file to write. Pass the current configFileName to overwrite the existing config.

      Returns Promise<void>

      Resolves on success; rejects if the file cannot be written.

    • Retrieve a file descriptor for the container's seccomp notification filter. Used to receive seccomp notifications from the container via poll(2). The container does not need to be running; the fd is pre-loaded at container start.

      Returns Promise<number>

      The seccomp notification fd.

    • Retrieve a file descriptor for the running container's active seccomp filter. Unlike seccompNotifyFd, this reflects the live filter of a running container.

      Returns Promise<number>

      The active seccomp notification fd.

    • Set a cgroup subsystem value for the container. The container must be running for most cgroup writes to take effect.

      Parameters

      • subsys: string

        {string} cgroup subsystem path (e.g. "memory.limit_in_bytes").

      • value: string

        {string} Value to write (e.g. "536870912" for 512 MiB).

      Returns void

    • Set a single key/value pair in the container's in-memory configuration. Call save() afterwards to persist the change to disk.

      Parameters

      • key: string

        {string} Configuration key (e.g. "lxc.log.file").

      • value: string

        {string} Value to set.

      Returns void

    • Set a global timeout for LXC operations on this container. After the timeout expires LXC will abort the operation with an error. Pass -1 to disable the timeout (wait indefinitely).

      Parameters

      • timeout: number

        {number} Timeout in seconds, or -1 to disable.

      Returns boolean

      true on success.

      Requires LXC ≥ 4.0.12. Throws if the library version is older.

    • Request the container to shut down gracefully by sending SIGPWR to its init process. Waits up to timeout seconds for the shutdown to complete.

      Parameters

      • Optionaltimeout: number

        {number} Seconds to wait; -1 waits forever, 0 does not wait. Default -1.

      Returns Promise<boolean>

      Resolves with true if shutdown completed within the timeout.

    • Create a container snapshot at the current state. Snapshots are stored as snap<N> under the container's snapshot directory (typically /var/lib/lxc/<name>/snaps/snap<N>).

      Parameters

      • commentfile: string

        {string} Full path to a file containing a human-readable description of the snapshot.

      Returns Promise<number>

      The zero-based snapshot number assigned (e.g. 0 for snap0).

    • Destroy a single named snapshot.

      Parameters

      • snapname: string

        {string} Name of the snapshot to destroy (e.g. "snap0").

      Returns Promise<void>

    • Destroy all snapshots for this container.

      Parameters

      • all: true

        {true} Must be true.

      Returns Promise<void>

    • Restore a container from a named snapshot. The restored container is a full copy of the snapshot placed at newname in the same lxcpath.

      Parameters

      • snapname: string

        {string} Snapshot name (e.g. "snap0").

      • Optionalnewname: string

        {string} Name for the restored container. Defaults to the current container's name, which replaces the container in-place (fails for overlay-based snapshots that pin the original).

      Returns Promise<void>

      Resolves when the restore is complete.

    • Start the container. If the container is already running, this is a no-op.

      Parameters

      • Optionaluseinit: number

        {number} Pass 1 to use lxcinit rather than /sbin/init. Default 0.

      • Optionalargv: string[]

        {string[]} Optional arguments to pass to the init process.

      Returns Promise<void>

      Resolves when the container has started; rejects on error.

      start

    • Retrieve a snapshot of common resource usage metrics for a running container. Reads six cgroup v1 entries in a single async operation. Returns null for any metric not available on the host kernel or cgroup hierarchy.

      Returns Promise<ContainerStats>

      Object with cgroup metric values.

    • Stop the container by sending it a SIGKILL. If the container is already stopped, this is a no-op.

      Returns Promise<void>

      Resolves when stopped; rejects on error.

      stop

    • Unmount a path inside the running container.

      Parameters

      • source: string

        {string} Path inside the container to unmount.

      • mountflags: number | bigint

        {bigint | number} Flags passed to umount2(2) (e.g. MNT_DETACH).

      • mnt: lxc_mount

        {lxc_mount} Mount API version descriptor. Set version to 0.

      Returns Promise<void>

      Resolves on success; rejects on error.

      umount

    • Thaw (unfreeze) a frozen container, resuming all paused processes. The container must be in the FROZEN state; it will transition back to RUNNING.

      Returns Promise<void>

      Resolves when thawed; rejects on failure.

    • Block until the container reaches the specified state or the timeout expires.

      Parameters

      • state: ContainerState

        {ContainerState} The target state to wait for.

      • Optionaltimeout: number

        {number} Seconds to wait; -1 waits forever, 0 returns immediately. Default -1.

      Returns Promise<void>

      Resolves when the state is reached; rejects on timeout or error.

      wait

    • Set whether all file descriptors should be closed before the container's init process starts.

      Parameters

      • state: boolean

        {boolean} true to close all fds on startup.

      Returns Promise<void>

      true on success.