Files
linuxkit/alpine/base/go-compile/compile.sh
Justin Cormack 5143cef148 Compile Go code with docker run not docker build
Go code is really fast to compile so we do not really need to use the
cache features of `docker build`. So make a compile container instead.
This can also output a build context and Dockerfile if you want to do
a build.

For reference, an uncached `docker build` of our Go code takes about
7s, a cached one 1.2s, and this takes 1.7s, so the best case is a little
worse, but we save a lot of images, and the worst case is better.

This is mainly designed to make the nested builds for containerd
containers simpler too. Will add a variant for the C code as well.

Also add `-static` to the flags so we always make static executables,
which was omitted previously.

Signed-off-by: Justin Cormack <justin.cormack@docker.com>
2017-01-13 15:49:51 +00:00

60 lines
968 B
Bash
Executable File

#!/bin/sh
# This is designed to compile a single package to a single binary
# so it makes some assumptions about things to simplify config
# to output a single binary (in a tarball) just use -o file
# use --docker to output a tarball for input to docker build -
set -e
usage() {
echo "Usage: -o file [--docker]"
exit 1
}
[ $# = 0 ] && usage
while [ $# -gt 1 ]
do
flag="$1"
case "$flag" in
-o)
out="$2"
mkdir -p "$(dirname $2)"
shift
;;
*)
echo "Unknown option $1"
exit 1
esac
shift
done
[ $# -gt 0 ] && [ $1 = "--docker" ] && DOCKER=1 && shift
[ $# -gt 0 ] && usage
[ -z "$out" ] && usage
package=$(basename "$out")
dir="$GOPATH/src/$package"
mkdir -p $dir
# untar input
tar xf - -C $dir
/usr/bin/lint.sh $dir
go build -o $out --ldflags '-extldflags "-fno-PIC -static"' "$package"
if [ -z "$DOCKER" ]
then
tar cf - $out
exit 0
fi
printf "FROM scratch\nCOPY $out $out\nENTRYPOINT [\"$out\"]\n" > Dockerfile
tar cf - Dockerfile $out