Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Alpine in systemd-nspawn containers
#!/bin/bash -e
# Creates a systemd-nspawn container with Alpine
MIRROR=http://nl.alpinelinux.org/alpine
ARCH=x86_64
VERSION=v3.6
APKTOOLS_VERSION=2.7.3-r0
if [ $UID -ne 0 ]; then
echo "run this script as root" >&2
exit 1
fi
if [ -z "$1" ]; then
echo "Usage: $0 <destination>" >&2
exit 0
fi
dest="$1"
apkdir=$(mktemp -d)
hostarch=x86
[ $(uname -m) == x86_64 ] && hostarch=x86_64
wget -qO- $MIRROR/latest-stable/main/$hostarch/apk-tools-static-$APKTOOLS_VERSION.apk \
| tar -xz -C $apkdir
$apkdir/sbin/apk.static \
-X $MIRROR/$VERSION/main -U \
--allow-untrusted --root "$dest" \
--initdb add alpine-base
mkdir -p "$dest"/{etc/apk,root}
printf 'nameserver 208.67.222.222\nnameserver 2620:0:ccc::2' >"$dest"/etc/resolv.conf
printf '%s/%s/main\n' $MIRROR $VERSION >"$dest"/etc/apk/repositories
sed '/tty[0-9]:/ s/^/#/' -i "$dest"/etc/inittab
printf 'console::respawn:/sbin/getty 38400 console\n' >>"$dest"/etc/inittab
for s in hostname bootmisc syslog; do
ln -s /etc/init.d/$s "$dest"/etc/runlevels/boot/$s
done
for s in killprocs savecache; do
ln -s /etc/init.d/$s "$dest"/etc/runlevels/shutdown/$s
done
rm -r $apkdir
echo ""
echo "Your Alpine container was created successfully."

Awesome, thank you for sharing the script.
It's a little bit ugly but I change to use edge instead.

VERSION=edge
APKTOOLS_VERSION=$(curl -s $MIRROR/edge/main/$ARCH/ | awk -F'=' 'match($0, /href="apk-tools-static-(.*).apk">/, a) {print a[1]}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment