LXC Alpinelinux Simple

From Alpine Linux
Revision as of 19:39, 25 August 2025 by Vkrishn (talk | contribs) (reformatted config, add missing cmds)

DRAFT

Linux Containers (LXC) provides containers similar to BSD Jails, Linux VServers and Solaris Zones. It gives the impression of virtualization, but shares the kernel and resources with the "host".

Setup walkthrough

Basic setup that gets you going, you would still need to read main LXC documentations for advanced features.

Objective

  1. Install Alpinelinux.
  2. Install LXC and its dependencies.
  3. Download required files.
  4. Start a LXC instance.
  5. Test basic networking.

Test bed

WIP

  • Describe here the test bed/setup used to run the scripts below.
  1. MODEL: Dell Laptop.
  2. CPUs
    1. CPU: Intel i5 2nd generation.
    2. CPU: Intel i5 3rd generation with virtualization enabled (2nd setup).
  3. RAM: 8GB

Install Alpinelinux

  1. Diskless installs (Ram based)
  2. SYS installs
Tip: - for further setup to work correctly, its assumed that alpinelinux installed in done for version between v3.21.x and v3.22.x

Install LXC and its dependencies

v3.21.x-v3.22.x

Install the required packages (as root):

apk add lxc lxcfs bridge

Setup LXC

-v3.22.x

Get required files (as root):

cd ~ wget https://dl-cdn.alpinelinux.org/v3.21/releases/x86_64/alpine-minirootfs-3.21.1-x86_64.tar.gz cd /var/lib/lxc/ mkdir -p guest1/rootfs cd guest1/rootfs tar -zxf ~/alpine-minirootfs-3.21.1-x86_64.tar.gz apk add --root ./ alpine-base

v3.21.x-v3.22.x

Fix some issues (as root),

#### Minor/manual changes #### rm -f /var/lib/lxc/guest1/rootfs/dev/null rm -f /var/lib/lxc/guest1/rootfs/dev/zero

v3.21.x-v3.22.x

Content of /etc/lxc/bridgenat.conf. (Not needed for manual setup)

lxc.net.0.type = veth
lxc.net.0.flags = up
lxc.net.0.link = br0
lxc.net.0.name = eth1
lxc.net.0.ipv4.address = 192.168.10.2/24 192.168.1.255
lxc.net.0.ipv4.gateway = 192.168.10.1
lxc.net.0.veth.pair = veth-if-10

v3.21.x-v3.22.x

Content of guest1/config

# config file that was generated using 
#  'lxc-create -n alpine -f /etc/lxc/bridgenat.conf -t alpine'

lxc.net.0.type = veth
lxc.net.0.flags = up
lxc.net.0.link = br0
lxc.net.0.name = eth1
  
lxc.net.0.ipv4.address = 192.168.10.10/24 192.168.10.255
lxc.net.0.ipv4.gateway = 192.168.10.1
  
# needs to be uniq per guest
lxc.net.0.veth.pair = veth-if-10
  
lxc.rootfs.path = dir:/var/lib/lxc/guest1/rootfs
  
# Specify container architecture.
lxc.arch = x86_64
  
# Set hostname.
lxc.uts.name = guest1
  
# If something doesn't work, try to comment this out.
lxc.cap.drop = sys_admin
  
# Comment this out if you have to debug processes by tracing.
lxc.cap.drop = sys_ptrace
  
# Comment this out if required by your applications.
lxc.cap.drop = setpcap
  
# Include common configuration.
lxc.include = /usr/share/lxc/config/alpine.common.conf


v3.21.x-v3.22.x

Start a LXC instance

Start these net/services as root

# brctl addbr br0 # need to set this first once

# ln -s lxc /etc/init.d/lxc.guest1 # need to do once

# rc-service lxc.guest1 start

# KNOWN ISSUEs:
   # * /var/lib/lxc/guest1/rootfs is on tmpfs and ALLOW_TMPFS is not set
   # * ERROR: lxc.guest1 failed to start

# To avoid seeing error like above in DISKLESS installs,
# SOLUTION is: move /var/lib/lxc to disk, then create a symlink of lxc

v3.21.x-v3.22.x

Use LXC

Start these net/services as root

# lxc-attach -n guest1 passwd root # add a passwd exit lxc-console -n guest1 # this works !!!, need to create user first then su -l to root # OR use lxc-attach to change passwd of root

v3.21.x-v3.22.x

WIP

Fix some basic networkings

# brctl show
bridge name	bridge id		STP enabled	interfaces
br0		8000.4686bf1982a3	no		dummy0
							veth-if-10

See also

Resources

  1. https://linuxcontainers.org/lxc/manpages/
  2. https://manned.org/pkg/alpine-3.22/lxc-doc