Merge pull request #1006 from justincormack/go-compile

Compile Go code with docker run not docker build
This commit is contained in:
Justin Cormack 2017-01-13 19:15:22 +00:00 committed by GitHub
commit d2655a92f1
9 changed files with 131 additions and 42 deletions

View File

@ -0,0 +1,8 @@
FROM golang:1.7-alpine
RUN apk update && apk add --no-cache build-base git
RUN go get -u github.com/golang/lint/golint
COPY lint.sh compile.sh /usr/bin/
ENTRYPOINT ["/usr/bin/compile.sh"]

View File

@ -0,0 +1,29 @@
.PHONY: tag push
BASE=golang:1.7-alpine
IMAGE=go-compile
default: push
hash: Dockerfile lint.sh compile.sh
DOCKER_CONTENT_TRUST=1 docker pull $(BASE)
tar cf - $^ | docker build --no-cache -t $(IMAGE):build -
docker run --rm --entrypoint=/bin/sh $(IMAGE):build -c 'cat /usr/local/go/bin/go /lib/apk/db/installed /usr/bin/lint.sh /go/bin/golint /usr/bin/compile.sh | sha1sum' | sed 's/ .*//' > hash
push: hash
docker pull mobylinux/$(IMAGE):$(shell cat hash) || \
(docker tag $(IMAGE):build mobylinux/$(IMAGE):$(shell cat hash) && \
docker push mobylinux/$(IMAGE):$(shell cat hash))
docker rmi $(IMAGE):build
rm -f hash
tag: hash
docker pull mobylinux/$(IMAGE):$(shell cat hash) || \
docker tag $(IMAGE):build mobylinux/$(IMAGE):$(shell cat hash)
docker rmi $(IMAGE):build
rm -f hash
clean:
rm -f hash
.DELETE_ON_ERROR:

View File

@ -0,0 +1,59 @@
#!/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

16
alpine/base/go-compile/lint.sh Executable file
View File

@ -0,0 +1,16 @@
#!/bin/sh
set -e
cd $1
>&2 echo "gofmt..."
test -z $(gofmt -s -l .| grep -v .pb. | grep -v */vendor/ | tee /dev/stderr)
>&2 echo "govet..."
test -z $(go tool vet -printf=false . 2>&1 | grep -v */vendor/ | tee /dev/stderr)
>&2 echo "golint..."
test -z $(find . -type f -name "*.go" -not -path "*/vendor/*" -not -name "*.pb.*" -exec golint {} \; | tee /dev/stderr)
>&2 echo "Successful lint check!"

View File

@ -1,12 +0,0 @@
# Tag: 2c9434f1c4ff70b102f34a97d2df1a8363a11a65
FROM mobylinux/alpine-build-go@sha256:d528bbf7102e4209bd59ef030d41de9003ab8e42c303956f62b2df47f3e17849
COPY ./ /go/src/diagnostics-server/
WORKDIR /go/src/diagnostics-server
RUN lint.sh .
RUN go install --ldflags '-extldflags "-fno-PIC"'
CMD ["tar", "cf", "-", "-C", "/go/bin", "diagnostics-server"]

View File

@ -1,12 +1,13 @@
all: usr/bin/diagnostics-server
# Tag: 470f68ec32e016484fb8e0bcc2f06cd3f201ea68
GO_COMPILE=mobylinux/go-compile@sha256:b723c0e95a6293300392e57d6ab52574f9217e2410b390a24cbfc4f9070edb6b
DEPS=Dockerfile $(wildcard *.go)
default: usr/bin/diagnostics-server
DEPS=$(wildcard *.go)
usr/bin/diagnostics-server: $(DEPS) ../vendor/manifest
BUILD=$$( tar cf - $(DEPS) -C .. vendor | docker build -q - ) && \
[ -n "$$BUILD" ] && \
echo "Built $$BUILD" && \
docker run --rm --net=none $$BUILD | tar xf - -C usr/bin
mkdir -p $(dir $@)
tar cf - $(DEPS) -C .. vendor | docker run --rm --net=none --log-driver=none -i $(GO_COMPILE) -o $@ | tar xf -
clean:
rm -f usr/bin/diagnostics-server

View File

@ -1,12 +1,12 @@
# Tag: 470f68ec32e016484fb8e0bcc2f06cd3f201ea68
GO_COMPILE=mobylinux/go-compile@sha256:b723c0e95a6293300392e57d6ab52574f9217e2410b390a24cbfc4f9070edb6b
all: usr/bin/slirp-proxy sbin/proxy-vsockd
DEPS=Dockerfile $(wildcard *.go libproxy/*.go)
proxy: $(DEPS) ../vendor/manifest
BUILD=$$( tar cf - $(DEPS) -C .. vendor | docker build -q - ) && \
[ -n "$$BUILD" ] && \
echo "Built $$BUILD" && \
docker run --rm --net=none $$BUILD | tar xf -
tar cf - $(DEPS) -C .. vendor | docker run --rm --net=none --log-driver=none -i $(GO_COMPILE) -o $@ | tar xf -
usr/bin/slirp-proxy: proxy
mkdir -p usr/bin

View File

@ -1,12 +0,0 @@
# Tag: 2c9434f1c4ff70b102f34a97d2df1a8363a11a65
FROM mobylinux/alpine-build-go@sha256:d528bbf7102e4209bd59ef030d41de9003ab8e42c303956f62b2df47f3e17849
COPY ./ /go/src/vsudd/
WORKDIR /go/src/vsudd
RUN lint.sh .
RUN go install --ldflags '-extldflags "-fno-PIC"'
CMD ["tar", "cf", "-", "-C", "/go/bin", "vsudd"]

View File

@ -1,13 +1,13 @@
all: vsudd
# Tag: 470f68ec32e016484fb8e0bcc2f06cd3f201ea68
GO_COMPILE=mobylinux/go-compile@sha256:b723c0e95a6293300392e57d6ab52574f9217e2410b390a24cbfc4f9070edb6b
DEPS=Dockerfile $(wildcard *.go)
default: sbin/vsudd
vsudd: $(DEPS) ../vendor/manifest
mkdir -p sbin
BUILD=$$( tar cf - $(DEPS) -C .. vendor | docker build -q - ) && \
[ -n "$$BUILD" ] && \
echo "Built $$BUILD" && \
docker run --rm --net=none $$BUILD | tar xf - -C sbin
DEPS=$(wildcard *.go)
sbin/vsudd: $(DEPS) ../vendor/manifest
mkdir -p $(dir $@)
tar cf - $(DEPS) -C .. vendor | docker run --rm --net=none --log-driver=none -i $(GO_COMPILE) -o $@ | tar xf -
clean:
rm -rf sbin