mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-07-26 20:18:52 +00:00
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>
This commit is contained in:
parent
3ef65e50f2
commit
b184b3f0e1
8
alpine/base/go-compile/Dockerfile
Normal file
8
alpine/base/go-compile/Dockerfile
Normal 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"]
|
29
alpine/base/go-compile/Makefile
Normal file
29
alpine/base/go-compile/Makefile
Normal 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:
|
59
alpine/base/go-compile/compile.sh
Executable file
59
alpine/base/go-compile/compile.sh
Executable 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
16
alpine/base/go-compile/lint.sh
Executable 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!"
|
@ -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"]
|
|
@ -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
|
usr/bin/diagnostics-server: $(DEPS) ../vendor/manifest
|
||||||
BUILD=$$( tar cf - $(DEPS) -C .. vendor | docker build -q - ) && \
|
mkdir -p $(dir $@)
|
||||||
[ -n "$$BUILD" ] && \
|
tar cf - $(DEPS) -C .. vendor | docker run --rm --net=none --log-driver=none -i $(GO_COMPILE) -o $@ | tar xf -
|
||||||
echo "Built $$BUILD" && \
|
|
||||||
docker run --rm --net=none $$BUILD | tar xf - -C usr/bin
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f usr/bin/diagnostics-server
|
rm -f usr/bin/diagnostics-server
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
|
# Tag: 470f68ec32e016484fb8e0bcc2f06cd3f201ea68
|
||||||
|
GO_COMPILE=mobylinux/go-compile@sha256:b723c0e95a6293300392e57d6ab52574f9217e2410b390a24cbfc4f9070edb6b
|
||||||
|
|
||||||
all: usr/bin/slirp-proxy sbin/proxy-vsockd
|
all: usr/bin/slirp-proxy sbin/proxy-vsockd
|
||||||
|
|
||||||
DEPS=Dockerfile $(wildcard *.go libproxy/*.go)
|
DEPS=Dockerfile $(wildcard *.go libproxy/*.go)
|
||||||
|
|
||||||
proxy: $(DEPS) ../vendor/manifest
|
proxy: $(DEPS) ../vendor/manifest
|
||||||
BUILD=$$( tar cf - $(DEPS) -C .. vendor | docker build -q - ) && \
|
tar cf - $(DEPS) -C .. vendor | docker run --rm --net=none --log-driver=none -i $(GO_COMPILE) -o $@ | tar xf -
|
||||||
[ -n "$$BUILD" ] && \
|
|
||||||
echo "Built $$BUILD" && \
|
|
||||||
docker run --rm --net=none $$BUILD | tar xf -
|
|
||||||
|
|
||||||
usr/bin/slirp-proxy: proxy
|
usr/bin/slirp-proxy: proxy
|
||||||
mkdir -p usr/bin
|
mkdir -p usr/bin
|
||||||
|
@ -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"]
|
|
@ -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
|
DEPS=$(wildcard *.go)
|
||||||
mkdir -p sbin
|
|
||||||
BUILD=$$( tar cf - $(DEPS) -C .. vendor | docker build -q - ) && \
|
sbin/vsudd: $(DEPS) ../vendor/manifest
|
||||||
[ -n "$$BUILD" ] && \
|
mkdir -p $(dir $@)
|
||||||
echo "Built $$BUILD" && \
|
tar cf - $(DEPS) -C .. vendor | docker run --rm --net=none --log-driver=none -i $(GO_COMPILE) -o $@ | tar xf -
|
||||||
docker run --rm --net=none $$BUILD | tar xf - -C sbin
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf sbin
|
rm -rf sbin
|
||||||
|
Loading…
Reference in New Issue
Block a user