From 7e671a0899a237c12a78d95cc849584ff31bd683 Mon Sep 17 00:00:00 2001 From: pancho horrillo Date: Fri, 6 Jan 2017 00:54:43 +0100 Subject: [PATCH] Improve build logic (#28) * dockerfiles: Drop duplicate mkdir The mkdir gets executed if "Building from local dev copy", whereas if built from upstream git, go get will take care of it. * dockerfiles: Drop extra community repo No longer needed with current edge or stable tags of alpine linux. * dockerfiles: Drop gcc as an explicit dep; go pulls it Current edge and 3.5 declare gcc as a dep for go. * dockerfiles: Install glide from package repo Both edge and 3.5 provide a packaged glide. By using the provided package, we reduce the build time a little. * dockerfiles: Set GLIDE_HOME to prevent glide to pollute /root glide promptly ignores HOME (it checks /etc/passwd), but honours GLIDE_HOME. So, by pointing GLIDE_HOME to a ephemeral dir, ~/.glide will be created there instead of /root/.glide, simplifying the cleanup process. * dockerfiles: Use --purge to ensure clean package uninstalls This way, /usr/lib/go gets purged, and we don't have to remove it manually. * dockerfiles: Move netboot tree instead of copying it Less space used and slightly faster build. * dockerfiles: Simplify build steps go build will honour GOBIN and leave the executable there. * dockerfiles: Move the installation of packages to the top * dockerfiles: Upgrade first, then install new stuff * dockerfiles: Tie build deps with a virtual package for easier removal By deleting the virtual .build-deps package, one ensures the no-package-left-behind policy :-) * dockerfiles: Split package list in multiple lines It's a good practice for enhancing the readability of pull requests, since addition or removal of packages from the list will result in single line changes. * dockerfiles: Drop changing dir to / No need to change directory in order do the cleanup. * dockerfiles: Test the existence of the go package, instead of .git This way we can add .git to .dockerignore, thus preventing copying it over when building the image. * dockerfiles: Add .dockerfile files to minimize the files that are copied over * dockerfiles: Place build logic in the Dockerfile Makes it easier to understand the build process if the whole sequence is stored in a single place. OTOH, editing the shell script inside the Dockerfile is a bit harder. * dockerfiles: Create a sandbox where to put all transient files * dockerfiles: Set GLIDE_HOME to the sandbox * dockerfiles: Set GOPATH to a dir under the sandbox * dockerfiles: Simplify cleanup * dockerfiles: Rename 'stuff' to 'context', to use Dockerfile lore * dockerfiles: Install entrypoint onto /usr/local/bin This is the default location for locally managed binaries, and as such, PATH includes it. This way, it is easier to run it interactively. * dockerfiles: Extract sandbox and context paths into variables * dockerfiles: Move vars up to help define other vars before they are used * dockerfiles: Add the required vars to eliminate duplicated paths * dockerfiles: Replace hardcoded paths with references to vars * dockerfiles: Fix wording for clarity * dockerfiles: Add emtpy line to improve readability * dockerfiles: Tell the go linker to strip debugging symbols This produces a sensibly smaller binary. * dockerfiles: Drop the --rm to docker build, as it is the default behaviour * dockerfiles: Use long flags for readability * dockerfiles: Base image onto alpine:3.5 Now that 3.5 has been released, we can use it instead of edge, since it provides go 1.7.3. We get a more stable environment, plus a smaller image. --- .dockerignore | 6 ++++ Makefile | 4 +-- dockerfiles/pixiecore/.dockerignore | 4 +++ dockerfiles/pixiecore/Dockerfile | 51 ++++++++++++++++++++++++++--- dockerfiles/pixiecore/build.sh | 29 ---------------- 5 files changed, 59 insertions(+), 35 deletions(-) create mode 100644 .dockerignore create mode 100644 dockerfiles/pixiecore/.dockerignore delete mode 100755 dockerfiles/pixiecore/build.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..fb943fb --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +**/.dockerignore +**/Dockerfile +.git +Makefile +*.md +LICENSE \ No newline at end of file diff --git a/Makefile b/Makefile index 34121ba..735cdaf 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ none: @echo "This makefile is just a shortcut for building docker containers." pixiecore: - sudo docker build --rm -t danderson/pixiecore -f dockerfiles/pixiecore/Dockerfile . + sudo docker build --tag danderson/pixiecore --file dockerfiles/pixiecore/Dockerfile . pixiecore-git: - sudo docker build --rm -t danderson/pixiecore dockerfiles/pixiecore + sudo docker build --tag danderson/pixiecore dockerfiles/pixiecore diff --git a/dockerfiles/pixiecore/.dockerignore b/dockerfiles/pixiecore/.dockerignore new file mode 100644 index 0000000..583e34f --- /dev/null +++ b/dockerfiles/pixiecore/.dockerignore @@ -0,0 +1,4 @@ +.dockerignore +# TODO: don't uncomment the next line until quay.io fixes how they handle +# .dockerignore that include Dockerfile (already notified them) +#Dockerfile diff --git a/dockerfiles/pixiecore/Dockerfile b/dockerfiles/pixiecore/Dockerfile index a6822c6..afd3999 100644 --- a/dockerfiles/pixiecore/Dockerfile +++ b/dockerfiles/pixiecore/Dockerfile @@ -1,7 +1,50 @@ -FROM alpine:edge +FROM alpine:3.5 + MAINTAINER David Anderson -COPY . /tmp/stuff -RUN cd /tmp/stuff; [ -f build.sh ] && ./build.sh || ./dockerfiles/pixiecore/build.sh +ENV PIXIECORE_SANDBOX /tmp/sandbox +ENV PIXIECORE_CONTEXT "$PIXIECORE_SANDBOX"/context -ENTRYPOINT ["/pixiecore"] +COPY . "$PIXIECORE_CONTEXT" + +RUN set -x; \ + set -e; \ + \ + apk upgrade --update-cache; \ + apk add ca-certificates; \ + apk add --virtual .build-deps \ + git \ + go \ + glide \ + musl-dev; \ + \ + # Pixiecore assets \ + NAMESPACE=go.universe.tf; \ + REPO=netboot; \ + PKG=cmd/pixiecore; \ + \ + export GOPATH="$PIXIECORE_SANDBOX"/go; \ + export GLIDE_HOME="$PIXIECORE_SANDBOX"; \ + \ + NAMESPACE_PATH="$GOPATH/src/$NAMESPACE"; \ + REPO_PATH="$NAMESPACE_PATH/$REPO"; \ + PKG_PATH="$REPO_PATH/$PKG"; \ + \ + if [ -d "$PIXIECORE_CONTEXT"/"$PKG" ]; then \ + echo "Building from local dev copy"; \ + mkdir -p "$NAMESPACE_PATH"; \ + mv -v "$PIXIECORE_CONTEXT" "$REPO_PATH"; \ + else \ + echo "Building from upstream git repo"; \ + fi; \ + \ + go get -v -d "$NAMESPACE/$REPO/$PKG"; \ + cd "$REPO_PATH"; \ + glide install; \ + go test $(glide nv); \ + GOBIN=/usr/local/bin go install -ldflags -s ./"$PKG"; \ + \ + apk del --purge .build-deps; \ + rm -rf "$PIXIECORE_SANDBOX" /var/cache/apk/*; + +ENTRYPOINT ["/usr/local/bin/pixiecore"] diff --git a/dockerfiles/pixiecore/build.sh b/dockerfiles/pixiecore/build.sh deleted file mode 100755 index c61fbd8..0000000 --- a/dockerfiles/pixiecore/build.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/sh - -set -x -set -e - -mkdir -p /tmp/go/src/go.universe.tf -if [ -d /tmp/stuff/.git ]; then - echo "Building from local dev copy" - mkdir -p /tmp/go/src/go.universe.tf - cp -R /tmp/stuff /tmp/go/src/go.universe.tf/netboot -else - echo "Building from git checkout" -fi - -export GOPATH=/tmp/go -echo "http://dl-4.alpinelinux.org/alpine/edge/community" >>/etc/apk/repositories -apk -U add ca-certificates git go gcc musl-dev -apk upgrade -go get -v github.com/Masterminds/glide -go get -v -d go.universe.tf/netboot/cmd/pixiecore -cd /tmp/go/src/go.universe.tf/netboot -/tmp/go/bin/glide install -go test $(/tmp/go/bin/glide nv) -cd cmd/pixiecore -go build . -cp ./pixiecore /pixiecore -cd / -apk del git go gcc musl-dev -rm -rf /tmp/go /tmp/stuff /root/.glide /usr/lib/go /var/cache/apk/*