Alpine Linux in a chroot

Inside the chroot environment, you can build, debug, and run Alpine packages or develop things. It's the most known way to do so without replacing your system or using a Virtual Machine.
This document explains how to set up an Alpine build environment in a chroot under a host Linux distro, that can also be used to install Alpine Linux from a non-Alpine Linux system or live environment.
== Requirements ==
* Working Linux instalation where to perform all the process
* Linux kernel 2.6.22, with curl and chroot binary installed
* target media with at least 100M, 900MB for more complete solution as minimum
* internet connection
== Prerequisites ==
The variables below: 
*${chroot_dir} = Should point to the new root directory
*${mirror} = Should be replaced with one of the available Alpine Linux mirrors.
*${arch} = Should be the cpu architecture like x86 (i386) or amd64(x86_64)..
== Set up APK ==
Download the latest apk static package (replace ${version} with actual version):
.apk packages are just gzipped tarballs, you can unpack them using:
== Install the alpine base installation onto the chroot ==
== Seting up the chroot ==
Before you change root to the new directory, you need to create the required devices
===== Method 1.a Simple way: Using the host's /dev =====
===== Method 1.b Manual way: Creating needed nodes =====
If you need SCSI disc access you can create the device nodes like this:
==== Make the process fs and /sys fs available ====
==== Set up name resolution ====
A resolv.conf is needed for name resolution: 
You can either copy your host's resolv.conf:
or instead you can create a new one (this example uses OpenDNS):
==== Prepare the APK repositories ====
Set up APK main repository (replace ${branch} with the latest stable branch name, e.g. v3.3):
==== Entering your chroot ====
You then can enter your chroot by running this command as the root user (UID 0).
==== Preparing init services ====
If you plan to use your chroot with a init system or setup a new system on another device you should add these services:
= Troubleshooting =
== Hardened kernels or alpine as chroot host ==
If you are using Alpine as a native build system you will have to make sure that you can run chmod from a chroot. Add the following to /etc/sysctl.conf
kernel.grsecurity.chroot_deny_chmod = 0
Then reload the sysctl configuration
sysctl -p
== chroot: cannot run command ' ... Exec format error ==
This usually indicates that you booted with one architecture (e.g. armf) and are trying to chroot into another (e.g. x86_64). The binaries must be built for the architecture that the host runs!
Note that with one exception you can run 32 bit x86 chroot in x86_64, but not viceversa!
== WARNING: Ignoring APKINDEX.xxxx.tar.gz ==
Make sure ${chroot_dir}/etc/apk/repositories is valid and run:
apk update
= External links =
* You can also use script alpine-chroot-install
* https://web.archive.org/web/20190808203313/https://isc.sans.edu/forums/diary/Forensic+use+of+mount+bind/22854/
* Alpine Linux in a chroot on Fedora : https://git.alpinelinux.org/cgit/user/fab/scripts/tree/alpine-chroot.sh script
* Alpine Linux aarch64 in a chroot on AWS Linux : https://gist.github.com/emolitor/0567e51c0ce04f4b025fc78d2cf0b4f1 script
Category: Installation
category: System Administration