Skip to content

Instantly share code, notes, and snippets.

@nicerobot
Last active January 17, 2021 06:03
Show Gist options
  • Save nicerobot/c11ee7e2e2b3135e8df8083a585607e2 to your computer and use it in GitHub Desktop.
Save nicerobot/c11ee7e2e2b3135e8df8083a585607e2 to your computer and use it in GitHub Desktop.
A Docker Multi-Stage Scratch Build for Go with binary compression using UPX and including SSL certificates.

Test

git clone https://gist.github.com/nicerobot/c11ee7e2e2b3135e8df8083a585607e2
cd c11ee7e2e2b3135e8df8083a585607e2
docker build --tag c11ee7e2e2b3135e8df8083a585607e2 .

or

curl -s https://gist.githubusercontent.com/nicerobot/c11ee7e2e2b3135e8df8083a585607e2/raw/install | /bin/bash -s --
FROM golang:1.9.2-alpine3.7 as build-env
RUN apk add --no-cache git build-base && \
echo "http://dl-cdn.alpinelinux.org/alpine/edge/main" >> /etc/apk/repositories && \
echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories && \
echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories && \
apk add --no-cache upx=3.94-r0
ADD . /go/src/github.com/gomatic/app/
WORKDIR /go/src/github.com/gomatic/app
RUN go get -d ./ && \
CGO_ENABLED=0 GOOS=linux go build -o app -a -ldflags="-s -w" -installsuffix cgo && \
upx --ultra-brute -qq app && \
upx -t app
FROM scratch
COPY --from=build-env /go/src/github.com/gomatic/app/app .
COPY --from=build-env /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
CMD ["/app"]
#!/bin/bash
git clone https://gist.github.com/nicerobot/c11ee7e2e2b3135e8df8083a585607e2
cd c11ee7e2e2b3135e8df8083a585607e2
docker build --tag c11ee7e2e2b3135e8df8083a585607e2 .
package main
import (
"log"
"time"
)
func main() {
log.Printf("%v", time.Now())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment