Skip to content
This repository has been archived by the owner on Mar 27, 2018. It is now read-only.

Commit

Permalink
Add initial "slim" variant support
Browse files Browse the repository at this point in the history
Ref #48
  • Loading branch information
tianon committed Nov 4, 2016
1 parent 6fadcab commit 7cd595b
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 16 deletions.
20 changes: 11 additions & 9 deletions generate-stackbrew-library.sh
Expand Up @@ -95,15 +95,17 @@ for version in "${versions[@]}"; do
Directory: $version
EOE

if [ "$(git show "$commit:$version/backports/Dockerfile" 2>/dev/null || true)" ]; then
echo
cat <<-EOE
Tags: $version-backports
GitFetch: refs/heads/$branch
GitCommit: $commit
Directory: $version/backports
EOE
fi
for imageVariant in slim backports; do
if [ "$(git show "$commit:$version/$imageVariant/Dockerfile" 2>/dev/null || true)" ]; then
echo
cat <<-EOE
Tags: $version-$imageVariant
GitFetch: refs/heads/$branch
GitCommit: $commit
Directory: $version/$imageVariant
EOE
fi
done
done

dockerfilesBase='https://github.com/tianon/dockerfiles'
Expand Down
10 changes: 10 additions & 0 deletions slim-excludes
@@ -0,0 +1,10 @@
# This file contains the list of files/directories which will be removed for the "slim" image variants.
# https://github.com/tianon/docker-brew-debian/issues/48
# https://wiki.ubuntu.com/ReducingDiskFootprint#Drop_unnecessary_files
/usr/share/doc/*
/usr/share/groff/*
/usr/share/info/*
/usr/share/linda/*
/usr/share/lintian/*
/usr/share/locale/*
/usr/share/man/*
99 changes: 92 additions & 7 deletions update.sh
Expand Up @@ -107,7 +107,7 @@ for version in "${versions[@]}"; do
mirror="$(get_part "$dir" mirror '')"
script="$(get_part "$dir" script '')"

args=( -d "$dir" debootstrap )
args=( --dir "$dir" --compression 'xz' debootstrap )
[ -z "$variant" ] || args+=( --variant="$variant" )
[ -z "$components" ] || args+=( --components="$components" )
[ -z "$include" ] || args+=( --include="$include" )
Expand Down Expand Up @@ -135,6 +135,7 @@ for version in "${versions[@]}"; do
echo 'https://github.com/docker/docker/blob/master/contrib/mkimage.sh'
} > "$dir/build-command.txt"

sudo rm -rf "$dir/rootfs"
sudo nice ionice -c 3 "$mkimage" "${args[@]}" 2>&1 | tee "$dir/build.log"

sudo chown -R "$(id -u):$(id -g)" "$dir"
Expand All @@ -158,13 +159,97 @@ for version in "${versions[@]}"; do
true
'
docker run --rm "${repo}:${suite}" dpkg-query -f '${Package}\t${Version}\n' -W > "$dir/build.manifest"
fi

if [ "${backports[$suite]}" ]; then
mkdir -p "$dir/backports"
echo "FROM $origRepo:$suite" > "$dir/backports/Dockerfile"
cat >> "$dir/backports/Dockerfile" <<-'EOF'
RUN awk '$1 ~ "^deb" { $3 = $3 "-backports"; print; exit }' /etc/apt/sources.list > /etc/apt/sources.list.d/backports.list
EOF

if [ "$repo" ]; then
( set -x && docker build -t "${repo}:${suite}-backports" "$dir/backports" )
fi
fi

IFS=$'\n'
set -o noglob
slimExcludes=( $(get_part "$dir" slim-excludes '' | grep -vE '^#|^$') )
set +o noglob
unset IFS
if [ "${#slimExcludes[@]}" -gt 0 ]; then
sudo rm -rf "$dir/slim/rootfs"
mkdir -p "$dir/slim/rootfs"
sudo tar --extract --file "$dir/rootfs.tar.xz" --directory "$dir/slim/rootfs"

dpkgCfgFile="$dir/slim/rootfs/etc/dpkg/dpkg.cfg.d/docker"
sudo mkdir -p "$(dirname "$dpkgCfgFile")"
{
echo '# This is the "slim" variant of the Debian base image.'
echo '# Many files which are normally unnecessary in containers are excluded,'
echo '# and this configuration file keeps them that way.'
} | sudo tee -a "$dpkgCfgFile"

if [ "${backports[$suite]}" ]; then
mkdir -p "$dir/backports"
echo "FROM $origRepo:$suite" > "$dir/backports/Dockerfile"
cat >> "$dir/backports/Dockerfile" <<-'EOF'
RUN awk '$1 ~ "^deb" { $3 = $3 "-backports"; print; exit }' /etc/apt/sources.list > /etc/apt/sources.list.d/backports.list
EOF
neverExclude='/usr/share/doc/*/copyright'
for slimExclude in "${slimExcludes[@]}"; do
{
echo
echo "# dpkg -S '$slimExclude'"
if dpkgOutput="$(sudo chroot "$dir/slim/rootfs" dpkg -S "$slimExclude" 2>&1)"; then
echo "$dpkgOutput" | sed 's/: .*//g; s/, /\n/g' | sort -u | xargs
else
echo "$dpkgOutput"
fi | fold -w 76 -s | sed 's/^/# /'
echo "path-exclude $slimExclude"
} | sudo tee -a "$dpkgCfgFile"
if [[ "$slimExclude" == *'/*' ]]; then
if [ -d "$dir/slim/rootfs/$(dirname "$slimExclude")" ]; then
# use two passes so that we don't fail trying to remove directories from $neverExclude
sudo chroot "$dir/slim/rootfs" \
find "$(dirname "$slimExclude")" \
-mindepth 1 \
-not -path "$neverExclude" \
-not -type d \
-delete
sudo chroot "$dir/slim/rootfs" \
find "$(dirname "$slimExclude")" \
-mindepth 1 \
-empty \
-delete
fi
else
sudo chroot "$dir/slim/rootfs" rm -f "$slimExclude"
fi
done
{
echo
echo '# always include these files, especially for license compliance'
echo "path-include $neverExclude"
} | sudo tee -a "$dpkgCfgFile"

sudo tar --numeric-owner --create --auto-compress --file "$dir/slim/rootfs.tar.xz" --directory "$dir/slim/rootfs" --transform='s,^./,,' .
sudo rm -rf "$dir/slim/rootfs"
cp "$dir/Dockerfile" "$dir/slim/"

sudo chown -R "$(id -u):$(id -g)" "$dir/slim"

ls -lh "$dir/rootfs.tar.xz" "$dir/slim/rootfs.tar.xz"

if [ "$repo" ]; then
( set -x && docker build -t "${repo}:${suite}-slim" "$dir/slim" )
docker images "${repo}:${suite}" | tail -1
docker images "${repo}:${suite}-slim" | tail -1
fi
fi

find "$dir" \
\( \
-name '*.txt' \
-or -name '*.log' \
\) \
-exec sed -i \
-e 's!'"$PWD"'!/«BREWDIR»!g' \
-e 's!'"$(dirname "$mkimage")"'!/«MKIMAGEDIR»!g' \
'{}' +
done

0 comments on commit 7cd595b

Please sign in to comment.