├── .gitignore ├── Dockerfile ├── README.md ├── hooks └── build └── loader /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.10-alpine 2 | 3 | ARG BUILD_DATE 4 | ARG VCS_REF 5 | 6 | LABEL org.label-schema.build-date=$BUILD_DATE \ 7 | org.label-schema.vcs-url="https://github.com/brimstone/docker-golang-musl" \ 8 | org.label-schema.vcs-ref=$VCS_REF \ 9 | org.label-schema.schema-version="1.0.0-rc1" 10 | 11 | ENV TAR="" \ 12 | VERBOSE="" \ 13 | LDFLAGS="" 14 | 15 | RUN apk -U add gcc make git musl-dev jq-dev 16 | 17 | COPY loader /loader 18 | 19 | WORKDIR /go/src/app 20 | 21 | ENTRYPOINT [ "/loader" ] 22 | 23 | ONBUILD ARG PACKAGE 24 | 25 | ONBUILD COPY . /go/src/${PACKAGE}/ 26 | 27 | ONBUILD WORKDIR /go/src/${PACKAGE}/ 28 | 29 | ONBUILD RUN /loader -o /app 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | golang-musl 2 | =========== 3 | 4 | This is a container to build golang static binaries with musl instead of glibc. 5 | 6 | [![](https://images.microbadger.com/badges/image/brimstone/golang-musl.svg)](https://microbadger.com/images/brimstone/golang-musl "Get your own image badge on microbadger.com") 7 | 8 | 9 | Usage 10 | ----- 11 | 12 | Check out your source files to a GOPATH compatible directory: 13 | 14 | ```bash 15 | mkdir -p src/github.com/user 16 | git clone https://github.com/user/repo.git src/github.com/user/repo 17 | ``` 18 | 19 | Then build! 20 | 21 | ```bash 22 | docker run --rm -it -v "$PWD:/go" -u "$UID:$GID" brimstone/golang-musl github.com/user/repo 23 | ``` 24 | 25 | Alternate build 26 | --------------- 27 | 28 | For when another repo is included in a `src` directory, for instance, a submodule: 29 | ```bash 30 | tar c src \ 31 | | docker run --rm -i -e TAR=1 brimstone/golang-musl github.com/user/repo \ 32 | | tar -x ./main 33 | ``` 34 | 35 | For when there's just source files in a diretory: 36 | ```bash 37 | tar c . \ 38 | | docker run --rm -i -e TAR=1 brimstone/golang-musl -o main \ 39 | | tar -x ./main 40 | 41 | 42 | Environment Variables 43 | --------------------- 44 | 45 | `VERBOSE` This makes the loader script more verbose 46 | 47 | ONBUILD 48 | ------- 49 | 50 | This image supports docker multistage builds. Simply use this as template for your Dockerfile: 51 | ``` 52 | FROM brimstone/golang-musl as builder 53 | 54 | FROM scratch 55 | ENV ADDRESS= 56 | EXPOSE 80 57 | ENTRYPOINT ["/repo", "serve"] 58 | COPY --from=builder /app /repo 59 | ``` 60 | 61 | Then build with this: 62 | ``` 63 | docker build -t user/repo --build-arg PACKAGE=github.com/user/repo . 64 | ``` 65 | 66 | References 67 | ---------- 68 | 69 | http://dominik.honnef.co/posts/2015/06/statically_compiled_go_programs__always__even_with_cgo__using_musl/ 70 | 71 | -------------------------------------------------------------------------------- /hooks/build: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # stolen from https://medium.com/microscaling-systems/labelling-automated-builds-on-docker-hub-f3d073fb8e1 4 | docker build --build-arg VCS_REF="$(git rev-parse --short HEAD)" \ 5 | --build-arg BUILD_DATE="$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \ 6 | -t "$IMAGE_NAME" . 7 | -------------------------------------------------------------------------------- /loader: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -ue 4 | 5 | LDFLAGS="${LDFLAGS:-}" 6 | [ -n "$VERBOSE" ] && set -x 7 | 8 | if [ "$#" = 0 ]; then 9 | echo "Assuming the working directory contains source files." >&2 10 | fi 11 | 12 | if [ -n "$TAR" ]; then 13 | tar -x >&2 14 | ( 15 | [ -n "${1:-}" ] && [ -d "src/$1" ] && cd "src/$1" 16 | /usr/local/go/bin/go get -v -d >&2 17 | ) 18 | fi 19 | 20 | /usr/local/go/bin/go build -a -ldflags "-linkmode external -extldflags \"-static\" -s -w $LDFLAGS" "$@" >&2 21 | 22 | if [ -n "$TAR" ]; then 23 | tar -c . 24 | fi 25 | --------------------------------------------------------------------------------