5 include='iproute,iputils-ping'
6 arch='amd64' # intentionally undocumented for now
14 echo >&2 "usage: $0 [options] repo suite [mirror]"
17 echo >&2 'options: (not recommended)'
18 echo >&2 " -p set an http_proxy for debootstrap"
19 echo >&2 " -v $variant # change default debootstrap variant"
20 echo >&2 " -i $include # change default package includes"
21 echo >&2 " -d # strict debootstrap (do not apply any docker-specific tweaks)"
22 echo >&2 " -s # skip version detection and tagging (ie, precise also tagged as 12.04)"
23 echo >&2 " # note that this will also skip adding universe and/or security/updates to sources.list"
24 echo >&2 " -t # just create a tarball, especially for dockerbrew (uses repo as tarball name)"
27 echo >&2 " ie: $0 username/debian squeeze"
28 echo >&2 " $0 username/debian squeeze http://ftp.uk.debian.org/debian/"
31 echo >&2 " ie: $0 username/ubuntu precise"
32 echo >&2 " $0 username/ubuntu precise http://mirrors.melbourne.co.uk/ubuntu/"
35 echo >&2 " ie: $0 -t precise.tar.bz2 precise"
36 echo >&2 " $0 -t wheezy.tgz wheezy"
37 echo >&2 " $0 -t wheezy-uk.tar.xz wheezy http://ftp.uk.debian.org/debian/"
42 # these should match the names found at http://www.debian.org/releases/
45 # this should match the name found at http://releases.ubuntu.com/
46 ubuntuLatestLTS=precise
48 while getopts v:i:a:p:dst name; do
77 shift $(($OPTIND - 1))
81 mirror="${3:-}" # stick to the default debootstrap mirror if one is not provided
83 if [ ! "$repo" ] || [ ! "$suite" ]; then
88 # some rudimentary detection for whether we need to "sudo" our docker calls
90 if docker version > /dev/null 2>&1; then
92 elif sudo docker version > /dev/null 2>&1; then
94 elif command -v docker > /dev/null 2>&1; then
97 echo >&2 "warning: either docker isn't installed, or your current user cannot run it;"
98 echo >&2 " this script is not likely to work as expected"
100 docker='docker' # give us a command-not-found later
103 # make sure we have an absolute path to our final tarball so we can still reference it properly after we change directory
104 if [ "$justTar" ]; then
105 if [ ! -d "$(dirname "$repo")" ]; then
106 echo >&2 "error: $(dirname "$repo") does not exist"
109 repo="$(cd "$(dirname "$repo")" && pwd -P)/$(basename "$repo")"
112 # will be filled in later, if [ -z "$skipDetection" ]
115 target="/tmp/docker-rootfs-debootstrap-$suite-$$-$RANDOM"
117 cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"
124 sudo http_proxy=$http_proxy debootstrap --verbose --variant="$variant" --include="$include" --arch="$arch" "$suite" "$target" "$mirror"
128 if [ -z "$strictDebootstrap" ]; then
129 # prevent init scripts from running during install/update
130 # policy-rc.d (for most scripts)
131 echo $'#!/bin/sh\nexit 101' | sudo tee usr/sbin/policy-rc.d > /dev/null
132 sudo chmod +x usr/sbin/policy-rc.d
133 # initctl (for some pesky upstart scripts)
134 sudo chroot . dpkg-divert --local --rename --add /sbin/initctl
135 sudo ln -sf /bin/true sbin/initctl
136 # see https://github.com/dotcloud/docker/issues/446#issuecomment-16953173
138 # shrink the image, since apt makes us fat (wheezy: ~157.5MB vs ~120MB)
139 sudo chroot . apt-get clean
141 # while we're at it, apt is unnecessarily slow inside containers
142 # this forces dpkg not to call sync() after package extraction and speeds up install
143 # the benefit is huge on spinning disks, and the penalty is nonexistent on SSD or decent server virtualization
144 echo 'force-unsafe-io' | sudo tee etc/dpkg/dpkg.cfg.d/02apt-speedup > /dev/null
145 # we want to effectively run "apt-get clean" after every install to keep images small
146 echo 'DPkg::Post-Invoke {"/bin/rm -f /var/cache/apt/archives/*.deb || true";};' | sudo tee etc/apt/apt.conf.d/no-cache > /dev/null
148 # helpful undo lines for each the above tweaks (for lack of a better home to keep track of them):
149 # rm /usr/sbin/policy-rc.d
150 # rm /sbin/initctl; dpkg-divert --rename --remove /sbin/initctl
151 # rm /etc/dpkg/dpkg.cfg.d/02apt-speedup
152 # rm /etc/apt/apt.conf.d/no-cache
154 if [ -z "$skipDetection" ]; then
155 # see also rudimentary platform detection in hack/install.sh
157 if [ -r etc/lsb-release ]; then
158 lsbDist="$(. etc/lsb-release && echo "$DISTRIB_ID")"
160 if [ -z "$lsbDist" ] && [ -r etc/debian_version ]; then
166 # add the updates and security repositories
167 if [ "$suite" != "$debianUnstable" -a "$suite" != 'unstable' ]; then
168 # ${suite}-updates only applies to non-unstable
169 sudo sed -i "p; s/ $suite main$/ ${suite}-updates main/" etc/apt/sources.list
171 # same for security updates
172 echo "deb http://security.debian.org/ $suite/updates main" | sudo tee -a etc/apt/sources.list > /dev/null
176 # add the universe, updates, and security repositories
178 s/ $suite main$/ $suite main universe/; p;
179 s/ $suite main/ ${suite}-updates main/; p;
180 s/ $suite-updates main/ ${suite}-security main/
181 " etc/apt/sources.list
187 if [ "$justTar" ]; then
188 # create the tarball file so it has the right permissions (ie, not root)
192 sudo tar --numeric-owner -caf "$repo" .
194 # create the image (and tag $repo:$suite)
195 sudo tar --numeric-owner -c . | $docker import - $repo:$suite
198 $docker run -i -t $repo:$suite echo success
200 if [ -z "$skipDetection" ]; then
203 if [ "$suite" = "$debianStable" -o "$suite" = 'stable' ] && [ -r etc/debian_version ]; then
205 $docker tag $repo:$suite $repo:latest
207 if [ -r etc/debian_version ]; then
208 # tag the specific debian release version (which is only reasonable to tag on debian stable)
209 ver=$(cat etc/debian_version)
210 $docker tag $repo:$suite $repo:$ver
215 if [ "$suite" = "$ubuntuLatestLTS" ]; then
217 $docker tag $repo:$suite $repo:latest
219 if [ -r etc/lsb-release ]; then
220 lsbRelease="$(. etc/lsb-release && echo "$DISTRIB_RELEASE")"
221 if [ "$lsbRelease" ]; then
222 # tag specific Ubuntu version number, if available (12.04, etc.)
223 $docker tag $repo:$suite $repo:$lsbRelease
233 sudo rm -rf "$target"