Skip to content

Instantly share code, notes, and snippets.

@aputs
Last active February 6, 2019 10:00
Show Gist options
  • Save aputs/6247216 to your computer and use it in GitHub Desktop.
Save aputs/6247216 to your computer and use it in GitHub Desktop.
shell script for creating ubuntu base image for running inside docker containers
#!/bin/sh
set -x
control_c()
# run if user hits control-c
{
echo -en "\n*** Ouch! Exiting ***\n"
exit $?
}
trap control_c SIGINT
## warning!!!
## sudo access
[ ! -e $(which debootstrap 2> /dev/null) ] && echo "error: debootstrap not found" && exit 1
arch=amd64
release=${1:-raring}
repo=${2:-aputs}
dest_path=$(readlink -f -- "$(dirname $0)")
target_path=$(mktemp --directory)
hostname="$release-$arch"
extra_packages="iproute,net-tools,ca-certificates,dialog"
[ ! -e "$target_path" ] && echo "error: error generating temporary directory" && exit 1
sudo rm -fr $target_path
sudo mkdir -p $target_path
sudo mkdir -p $target_path/etc/apt/apt.conf.d $target_path/etc/dpkg/dpkg.cfg.d
# minimize installation configs
cat << EOF | sudo tee $target_path/etc/apt/apt.conf.d/02compress-indexes
Acquire::GzipIndexes "true";
Acquire::CompressionTypes::Order:: "gz";
EOF
cat << EOF | sudo tee $target_path/etc/apt/apt.conf.d/02nocache
Dir::Cache {
srcpkgcache "";
pkgcache "";
}
EOF
cat << EOF | sudo tee $target_path/etc/apt/apt.conf.d/97norecommends
APT
{
Install-Recommends "false";
};
EOF
cat << EOF | sudo tee $target_path/etc/dpkg/dpkg.cfg.d/01_nodoc
path-exclude /usr/share/doc/*
# we need to keep copyright files for legal reasons
path-include /usr/share/doc/*/copyright
path-exclude /usr/share/man/*
path-exclude /usr/share/groff/*
path-exclude /usr/share/info/*
# lintian stuff is small, but really unnecessary
path-exclude /usr/share/lintian/*
path-exclude /usr/share/linda/*
# lang
path-exclude /usr/share/locale/*
path-include /usr/share/locale/en*
# landscape
path-exclude /usr/share/pyshared/twisted/test*
path-exclude /usr/lib/python*/dist-packages/twisted/test*
path-exclude /usr/share/pyshared/twisted/*/test*
path-exclude /usr/lib/python*/dist-packages/twisted/*/test*
EOF
sudo debootstrap --verbose --arch=$arch --variant=minbase --include=$extra_packages $release $target_path
sudo rm -rf $target_path/dev
sudo mkdir -p $target_path/dev
sudo mknod -m 0666 $target_path/dev/null c 1 3
sudo mknod -m 0666 $target_path/dev/zero c 1 5
sudo mknod -m 0666 $target_path/dev/random c 1 8
sudo mknod -m 0666 $target_path/dev/urandom c 1 9
sudo mkdir -m 0755 $target_path/dev/pts
sudo mkdir -m 1777 $target_path/dev/shm
sudo mknod -m 0666 $target_path/dev/tty c 5 0
sudo mknod -m 0666 $target_path/dev/tty0 c 4 0
sudo mknod -m 0666 $target_path/dev/tty1 c 4 1
sudo mknod -m 0600 $target_path/dev/console c 5 1
sudo mknod -m 0666 $target_path/dev/full c 1 7
sudo mknod -m 0600 $target_path/dev/initctl p
sudo mknod -m 0666 $target_path/dev/ptmx c 5 2
cat << EOF | sudo tee $target_path/etc/fstab
devpts /dev/pts devpts defaults 0 0
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
EOF
cat << EOF | sudo tee $target_path/etc/hosts
127.0.0.1 localhost
EOF
cat << EOF | sudo tee $target_path/etc/apt/sources.list
deb http://archive.ubuntu.com/ubuntu $release main universe
deb http://archive.ubuntu.com/ubuntu $release-updates main restricted universe
EOF
# link to /tmp
sudo rm -rf $target_path/run
sudo ln -fs tmp $target_path/run
# fixup & cleanup
sudo mount -t proc none $target_path/proc
sudo mount -t sysfs none $target_path/sys
sudo mount -t tmpfs none $target_path/tmp
sudo chroot $target_path /bin/bash -x <<'EOF'
# LC_LANG errors
locale-gen en_US en_US.utf8
touch /etc/resolv.conf
cp -pf /etc/skel/.* /root
apt-get -y update
# fix for install using chroot
dpkg-divert --local --rename --add /sbin/initctl
dpkg-divert --local --rename --add /sbin/start-stop-daemon
ln -s /bin/true /sbin/initctl
ln -s /bin/true /sbin/start-stop-daemon
apt-get -y upgrade
apt-get -y install cron
# cleanup
apt-get -y clean all
apt-get -y autoremove
rm -rf /var/lib/apt/lists/*
# restore diversion for install fix
rm -rf /sbin/initctl /sbin/start-stop-daemon
dpkg-divert --local --rename --remove /sbin/start-stop-daemon
dpkg-divert --local --rename --remove /sbin/initctl
find /usr/share/doc -depth -type f ! -name copyright | xargs rm || true
find /usr/share/doc -empty | xargs rmdir || true
rm -rf /usr/share/man /usr/share/groff /usr/share/info /usr/share/lintian /usr/share/linda /var/cache/man
find /usr/share/locale -mindepth 1 -maxdepth 1 ! -name 'en' | xargs rm -r
rm -rf /var/cache/apt/*.bin
# mtab
ln -fs /proc/mounts /etc/mtab
# upstart fixes (disable all jobs)
for a in $(ls /etc/init/*.conf)
do
b=$(basename -s.conf $a)
echo "manual" > /etc/init/$b.override
done
dpkg-divert --local --rename --add /etc/init.d/ondemand
rm -rf /etc/init/upstart-file-bridge.override
rm -rf /etc/init/upstart-socket-bridge.override
rm -rf /etc/init/rc-sysinit.override
rm -rf /etc/init/rc.override
rm -rf /etc/init/cron.override
rm -rf /etc/init/passwd.override
# apt-get install fixes
mkdir -p /usr/share/man/man{1,2,3,4,5,6,7,8,9}
EOF
cat << EOF | sudo tee $target_path/etc/init/fix-upstart.conf
env DEFAULT_RUNLEVEL=3
start on startup
script
: > "/run/utmp"
chmod 664 "/run/utmp"
chgrp utmp "/run/utmp"
mkdir -p /run/sendsigs.omit.d
telinit \$DEFAULT_RUNLEVEL
initctl emit --no-wait net-device-up IFACE=lo
initctl emit --no-wait static-network-up
initctl emit --no-wait mounted
initctl emit --no-wait filesystem
initctl emit --no-wait virtual-filesystems
end script
EOF
cat << EOF | sudo tee $target_path/etc/init/fix-upstart-reboot.conf
start on runlevel [06]
script
initctl emit --no-wait startup
end script
EOF
sudo umount $target_path/proc
sudo umount $target_path/sys
sudo umount $target_path/tmp
tag=$(grep DISTRIB_RELEASE $target_path/etc/lsb-release | cut -d'=' -f2)
img=$(sudo tar --numeric-owner -C $target_path -c . | docker import - $repo/$release)
sudo rm -fr $target_path
@ncadou
Copy link

ncadou commented Aug 28, 2013

Just so you know, the resulting docker image at https://index.docker.io/u/aputs/raring/ has a strange behavior (at least on my machine) with apt tasks. Running "apt-cache show mail-server^" hangs there with 100% CPU usage. No idea why unfortunately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment