Install Alpine on LXC

From Alpine Linux
Revision as of 13:49, 18 July 2023 by Psykose (talk | contribs) (dl-cdn)

LXC

LXC is an operating-system-level virtualization method for running multiple isolated Linux systems (containers) on a control host using a single Linux kernel.

With LXC you can run Alpine Linux on container and start services in it using native Alpine Linux' init system (openrc).

This is instruction on how to install Alpine Linux on LXC container.

Preparation

LXC installation

You have to install "lxc" package on your host system. For example, in Arch Linux you can install it by running:

sudo pacman -S lxc

Bridge creation

You also have to create network bridge on your host.

Setting up bridge in Arch Linux or Arch Linux based systems

Setting up bridge in Ubuntu or Ubuntu based systems

Here is example, how to setup bridge with netctl:

Copy sample file "bridge"

sudo cp /etc/netctl/examples/bridge /etc/netctl/myBridge

Modify your bridge configuration file as needed (network interfaces: eno1 and tap0 are, of cource, according to your needs)

sudo vim /etc/netctl/myBridge

Description="Bridge connection"
Interface=br0
Connection=bridge
BindsToInterfaces=(eno1 tap0)
IP=dhcp

Stop active connection

sudo systemctl stop dhcpcd

Start your bridge

sudo netctl start myBridge

Perhaps you may wish to setup your system to start your bridge automatically at boot time.

Container creation

To install Alpine Linux edge version run:

sudo lxc-create --name alpine-edge -t alpine -- --release edge

You can also configure shared directory which will be accessible from both host and container. In this example we make host's /media/d directory available in container

sudo mkdir /var/lib/lxc/alpine-edge/rootfs/media/d

Modify container's configuration file:

sudo nano /var/lib/lxc/alpine-edge/config

Add folowing lines:

lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = br0
lxc.mount.entry=/media/d media/d none bind 0 0

Booting container:

sudo lxc-start -n alpine-edge

Shutting down container:

sudo lxc-stop -n alpine-edge

Going to Alpine Linux console:

sudo lxc-attach -n alpine-edge

Container setup

Modify your apk/repositories configuration file

vi /etc/apk/repositories

It is recommended to include "main", "testing" and "community" repositories.

http://dl-cdn.alpinelinux.org/alpine/edge/main
http://dl-cdn.alpinelinux.org/alpine/edge/testing
http://dl-cdn.alpinelinux.org/alpine/edge/community

Upgrading your Alpine Linux system:

apk update && apk upgrade -a

Starting services:

/etc/init.d/service_name start

or

rc-service service_name start

Adding services to autostart

rc-update add service_name

Restarting your container:

reboot