gists/lxc.md

3.3 KiB

LXC

Create a container

  • $ lxc launch [distribution] [name]
    • example: $ lxc launch images:debian/10 test-container

List available distribution images

  • $ lxc image list images:

List existing containers

  • $ lxc list
+----------------+---------+----------------------+---------------------------+------------+-----------+
|    NAME        |  STATE  |         IPV4         |            IPV6           |    TYPE    | SNAPSHOTS |
+----------------+---------+----------------------+---------------------------+------------+-----------+
| alpine         | STOPPED |                      |                           | PERSISTENT | 1         |
+----------------+---------+----------------------+---------------------------+------------+-----------+
| debian         | STOPPED |                      |                           | PERSISTENT | 0         |
+----------------+---------+----------------------+---------------------------+------------+-----------+
| ubuntu         | STOPPED |                      |                           | PERSISTENT | 3         |
+----------------+---------+----------------------+---------------------------+------------+-----------+
| minimal        | RUNNING | 10.10.10.10 (eth0)   | fd80::0000 (eth0)         | PERSISTENT | 0         |
+----------------+---------+----------------------+---------------------------+------------+-----------+
| test-container | STOPPED |                      |                           | PERSISTENT | 0         |
+----------------+---------+----------------------+---------------------------+------------+-----------+

Change state

  • Start a container: $ lxc start [container]
  • Stop a container: $ lxc stop [container]

Access a container

  • $ lxc exec [repository] [command]
    • example: $ lxc exec test-container bash

Snapshots

  • Create a snapshot: $ lxc snapshot [container] [snapshot_name]
    • example: $ lxc snapshot test-container base_state
  • Restore a snapshot: $ lxc restore [container] [snapshot_name]
    • example: $ lxc restore test-container base_state

Copy files

  • To a container: $ lxc file push [/local/files/to/copy] [container]/[destination/path]
    • example: $ lxc file push -r /home/user/files test-container/root (add -r to copy a folder)
  • From a container: $ lxc file pull [container][/files/to/copy] [destination/path]
    • example: $ lxc file pull test-container/root/files .

Privileged containers

This feature is only available when LXD is installed.

  • When creating a container: $ lxc launch [distribution] [name] -c security.privileged=true
    • Please note that this parameter apparently cannot be changed later.

When a container is privileged, we can share folders from the host to the container, in a read-write manner. We can still add a shared folder with unprivileged containers, but they will be read-only (chowned at nobody:nobody).

Note that you will need to change the group and user of the folder to the corresponding one of lxd (use id lxd to know it) before adding it.

  • Adding a shared folder: $ lxc config device add [container] [share_name] disk source=[/host/path/] path=[/container/path]
    • example: $ lxc config device add test-container home_dir disk source=/home/$USER path=/home/ubuntu