Skip to content

Instantly share code, notes, and snippets.

@mauricioprado00
Last active February 15, 2020 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauricioprado00/2e7f2fbdc117d44eef88a04dae1706a0 to your computer and use it in GitHub Desktop.
Save mauricioprado00/2e7f2fbdc117d44eef88a04dae1706a0 to your computer and use it in GitHub Desktop.
alpine-chroot-apache
# one-liner:
# curl -L https://goo.gl/H7iAZn | bash
# one liner with params:
# curl -L https://goo.gl/H7iAZn | bash -s myalpinedir x86_64 v3.4
# one liner from yandex:
# curl -L https://goo.gl/H7iAZn | MIRROR_FILTER=yandex bash -s myalpinedir x86_64 v3.4
chroot_dir=${1:-alpine}
arch=${2:-x86} #x86_64
branch=${3:-latest-stable}
# install apine chroot
curl -L https://gist.githubusercontent.com/mauricioprado00/1e40021b7e5a8bfdc8049e51b851d4b9/raw/alpine-chroot.sh | \
MIRROR_FILTER=${MIRROR_FILTER:-.} bash -s ${chroot_dir} ${arch} ${branch}
if [ $? -ne 0 ]; then
echo 'aborting apache installation could not install alpine chroot'
rm /tmp/alpine-chroot.sh
exit 1
fi
# mount just installed alpine
${chroot_dir}/mount.sh
# install apache
chroot ${chroot_dir} apk update
chroot ${chroot_dir} apk add apache2
# add custom index
index_file=$(find ${chroot_dir}/var/www/ -type f -iname "index.html" | head -n1)
mv ${index_file} ${index_file}.orig
cat <<MYINDEX > ${index_file}
<html>
<body>
<h1>this is my chrooted apache alpine</h2>
</body>
</html>
MYINDEX
# fix start issue
# see https://github.com/neeravkumar/dockerfiles/blob/8581133/alpine-openrc/Dockerfile
# see https://github.com/gliderlabs/docker-alpine/issues/42
chroot ${chroot_dir} apk add openrc
mkdir ${chroot_dir}/run/openrc
touch ${chroot_dir}/run/openrc/softlevel
sed -i 's/#rc_sys=""/rc_sys="lxc"/g' ${chroot_dir}/etc/rc.conf
echo 'rc_provide="loopback net"' >> ${chroot_dir}/etc/rc.conf
sed -i '/tty/d' /etc/inittab
sed -i 's/hostname $opts/# hostname $opts/g' ${chroot_dir}/etc/init.d/hostname
sed -i 's/mount -t tmpfs/# mount -t tmpfs/g' ${chroot_dir}/lib/rc/sh/init.sh
sed -i 's/cgroup_add_service /# cgroup_add_service /g' ${chroot_dir}/lib/rc/sh/openrc-run.sh
chroot ${chroot_dir} openrc
# cleanup apk cache to make it smaller
rm -rf ${chroot_dir}/var/cache/apk/*
# umount after chrooting command into the alpine container
${chroot_dir}/umount.sh
CYAN='\033[1;36m'
NC='\033[0m'
printf "${CYAN}to start apache${NC}: chroot ${chroot_dir} /etc/init.d/apache2 start \n"
printf "${CYAN}to stop apache${NC}: chroot ${chroot_dir} /etc/init.d/apache2 stop \n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment