From 8d3b2658f04076185f6e4be3d13e5ba5172df6da Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Thu, 18 Oct 2018 14:24:57 +0200 Subject: [PATCH] Uses a simple Go binary server in Scratch Downsize from 45.5MB to 29.6MB --- Dockerfile | 19 ++++++++++++++----- main.go | 13 +++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 main.go diff --git a/Dockerfile b/Dockerfile index 65661d3..f7f6813 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,13 +6,22 @@ WORKDIR /stackedit RUN npm install RUN npm run build -FROM nginx:1.15-alpine +FROM golang:alpine AS server +RUN apk --update add git build-base upx +WORKDIR /go/src/app +COPY main.go ./ +RUN CGO_ENABLED=0 GOOS=linux go build -a -ldflags="-s -w" -installsuffix cgo -o server . && \ + upx -v --best --overlay=strip server && \ + upx -t server + +FROM scratch LABEL maintainer="quentin.mcgaw@gmail.com" \ description="StackEdit server in a lightweight Docker container" \ - download="15.6MB" \ - size="45.5MB" \ + size="29.6MB" \ ram="7MB" \ cpu_usage="Very low" \ github="https://github.com/qdm12/stackedit-docker" -COPY --from=stackedit /stackedit/dist /usr/share/nginx/html/ -ENTRYPOINT nginx -g "daemon off;" +EXPOSE 80 +COPY --from=stackedit /stackedit/dist / +COPY --from=server /go/src/app/server /server +ENTRYPOINT [ "/server" ] diff --git a/main.go b/main.go new file mode 100644 index 0000000..7357f2c --- /dev/null +++ b/main.go @@ -0,0 +1,13 @@ +package main + +import ( + "log" + "net/http" +) + +func main() { + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + http.ServeFile(w, r, r.URL.Path[1:]) + }) + log.Fatal(http.ListenAndServe(":80", nil)) +}