Command via a Container

There are occasions where a command might be useful within AstLinux, but it may use python, golang, etc. or is just large in comparison to the 50 MB size of the AstLinux image. A LXC container can be a handy tool to add such a command.

Resource Usage: (minimum)

  • Memory: 100 MB
  • Storage: 0.4 GB

Below is an example how to add the lynx command line web browser. The lynx container only runs when needed.

First, it is assumed the lxcbr0 interface is setup per the Quick Start Guide โ†’ Enable LXC Support.

Then from the CLI:

lxc-create -t download -n lynx -- -d debian -r buster -a amd64 --no-validate
service lxc restart
lxc-attach lynx
  apt update
  apt install lynx ca-certificates fping

Next, stop the lynx container and keep it from automatically starting.

lxc-stop lynx
sed -i 's/^' /var/lib/lxc/lynx/config

Finally, create a wrapper script to start/stop the container and issue a command.

Place this script at /mnt/kd/bin/lynx and give it chmod 755 permissions.

## lynx wrapper for LXC Debian 10 container

## generate a random uid
rand_uid=$((RANDOM % 4999 + 95000))

## Destination IP to test when container network is active

trap 'printf "Abort\n" >&2; lxc-stop lynx; exit 1' INT TERM EXIT

lxc-start lynx

printf "Waiting for container network to become active... "
lxc-attach lynx -u $rand_uid -- fping -q -r15 $ping_ip
printf "OK\n"

lxc-attach lynx -u $rand_uid -- lynx "$@"

lxc-stop lynx


exit 0

Tip โ†’ If you edit /var/lib/lxc/lynx/config and assign a manual IP address, the container starts very quickly, whereas using DHCP it takes 4-6 seconds to establish the network.

Test it!


Related Info โ†’ LXC container in AstLinux