Install Alpine on LXC: Difference between revisions

From Alpine Linux
m (Missing slash in mount?)
(neutralize sudo, vim and nano usages)
 
(2 intermediate revisions by 2 users not shown)
Line 11: Line 11:
=== LXC installation ===
=== LXC installation ===
You have to install "lxc" package on your host system. For example, in Arch Linux you can install it by running:
You have to install "lxc" package on your host system. For example, in Arch Linux you can install it by running:
{{Cmd|sudo pacman -S lxc}}
{{Cmd|# pacman -S lxc}}


=== Bridge creation ===
=== Bridge creation ===
Line 23: Line 23:


Copy sample file "bridge"
Copy sample file "bridge"
{{Cmd|sudo cp /etc/netctl/examples/bridge /etc/netctl/myBridge}}
{{Cmd|# 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)
Modify your bridge configuration file as needed (network interfaces: eno1 and tap0 are, of cource, according to your needs)
{{Cmd|sudo vim /etc/netctl/myBridge}}
{{cat|/etc/netctl/myBridge|Description{{=}}"Bridge connection"
<pre>
Interface{{=}}br0
Description="Bridge connection"
Connection{{=}}bridge
Interface=br0
BindsToInterfaces{{=}}(eno1 tap0)
Connection=bridge
IP{{=}}dhcp
BindsToInterfaces=(eno1 tap0)
}}
IP=dhcp
</pre>


Stop active connection
Stop active connection
{{Cmd|sudo systemctl stop dhcpcd}}
{{Cmd|# systemctl stop dhcpcd}}


Start your bridge
Start your bridge
{{Cmd|sudo netctl start myBridge}}
{{Cmd|# netctl start myBridge}}


Perhaps you may wish to setup your system to start your bridge automatically at boot time.
Perhaps you may wish to setup your system to start your bridge automatically at boot time.
Line 46: Line 44:


To install Alpine Linux edge version run:
To install Alpine Linux edge version run:
{{Cmd|sudo lxc-create --name alpine-edge -t alpine -- --release edge}}
{{Cmd|# 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
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
{{Cmd|sudo mkdir /var/lib/lxc/alpine-edge/rootfs/media/d}}
{{Cmd|# mkdir /var/lib/lxc/alpine-edge/rootfs/media/d}}


Modify container's configuration file:
Add the following lines to the container's configuration file:
{{Cmd|sudo nano /var/lib/lxc/alpine-edge/config}}
{{Cat|/var/lib/lxc/alpine-edge/config|...
 
lxc.network.type {{=}} veth
Add folowing lines:
lxc.network.flags {{=}} up
<pre>
lxc.network.link {{=}} br0
lxc.network.type = veth
lxc.mount.entry{{=}}/media/d media/d none bind 0 0
lxc.network.flags = up
}}
lxc.network.link = br0
lxc.mount.entry=/media/d /media/d none bind 0 0
</pre>


Booting container:
Booting container:
{{cmd|sudo lxc-start -n alpine-edge}}
{{cmd|# lxc-start -n alpine-edge}}


Shutting down container:
Shutting down container:
{{cmd|sudo lxc-stop -n alpine-edge}}
{{cmd|# lxc-stop -n alpine-edge}}


Going to Alpine Linux console:
Going to Alpine Linux console:
{{cmd|sudo lxc-attach -n alpine-edge}}
{{cmd|# lxc-attach -n alpine-edge}}


== Container setup ==
== Container setup ==


Modify your apk/repositories configuration file
Modify your apk/repositories configuration file. It is recommended to include "main", "testing" and "community" repositories.
{{cmd|vi /etc/apk/repositories}}
{{Cat|/etc/apk/repositories|http://dl-cdn.alpinelinux.org/alpine/edge/main
 
http://dl-cdn.alpinelinux.org/alpine/edge/testing
It is recommended to include "main", "testing" and "community" repositories.
http://dl-cdn.alpinelinux.org/alpine/edge/community
<pre>
}}
http://nl.alpinelinux.org/alpine/edge/main
http://nl.alpinelinux.org/alpine/edge/testing
http://nl.alpinelinux.org/alpine/edge/community
</pre>


Upgrading your Alpine Linux system:
Upgrading your Alpine Linux system:
{{cmd|apk update && apk upgrade}}
{{cmd|# apk update && apk upgrade -a}}


Starting services:
Starting services:
{{Cmd|/etc/init.d/service_name start}}
{{Cmd|# /etc/init.d/service_name start}}
or
or
{{Cmd|rc-service service_name start}}
{{Cmd|# rc-service service_name start}}


Adding services to autostart
Adding services to autostart
{{Cmd|rc-update add service_name}}
{{Cmd|# rc-update add service_name}}


Restarting your container:
Restarting your container:

Latest revision as of 13:32, 25 August 2023

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:

# 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"

# 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)

Contents of /etc/netctl/myBridge

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

Stop active connection

# systemctl stop dhcpcd

Start your bridge

# 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:

# 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

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

Add the following lines to the container's configuration file:

Contents of /var/lib/lxc/alpine-edge/config

... 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:

# lxc-start -n alpine-edge

Shutting down container:

# lxc-stop -n alpine-edge

Going to Alpine Linux console:

# lxc-attach -n alpine-edge

Container setup

Modify your apk/repositories configuration file. It is recommended to include "main", "testing" and "community" repositories.

Contents of /etc/apk/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