From 394db4aaf4fd5e566469e4f17992053ad7885422 Mon Sep 17 00:00:00 2001 From: Ian Lai Date: Fri, 27 Oct 2017 15:56:23 +0100 Subject: [PATCH 1/2] Allow database file path to be configured Set it to `/data/` by default for Docker --- Dockerfile | 2 ++ config.go | 7 ++++--- main.go | 6 +++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index ce3dfc5c..9af9f520 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,5 +16,7 @@ RUN /bin/bash -c "source /tmp/nvm/nvm.sh && npm install" RUN /bin/bash -c "source /tmp/nvm/nvm.sh && npm run-script build" RUN cp /go/src/commento/assets /go/bin/assets -vr +RUN mkdir /data/ + # set entrypoint ENTRYPOINT /go/bin/commento diff --git a/config.go b/config.go index 5aef2b5e..236d05d0 100644 --- a/config.go +++ b/config.go @@ -1,15 +1,16 @@ package main import ( - "strings" - "os" "github.com/joho/godotenv" + "os" + "strings" ) func loadConfig() error { // Default value for each environment variable. env := map[string]string{ - "COMMENTO_PORT": "8080", + "COMMENTO_DATABASE_FILE": "sqlite3.db", + "COMMENTO_PORT": "8080", } // Load configuration from the environment. Final value is governed by the diff --git a/main.go b/main.go index 9770d479..ea790e19 100644 --- a/main.go +++ b/main.go @@ -11,12 +11,13 @@ import ( func main() { var err error - err = LoadDatabase("sqlite:file=sqlite3.db") + err = loadConfig() if err != nil { Die(err) } - err = loadConfig() + fp := os.Getenv("COMMENTO_DATABASE_FILE") + err = LoadDatabase("sqlite:file=" + fp) if err != nil { Die(err) } @@ -29,7 +30,6 @@ func main() { http.HandleFunc("/get", GetCommentsHandler) port := os.Getenv("COMMENTO_PORT") - svr := &http.Server{ Addr: ":" + port, ReadTimeout: 5 * time.Second, From bfb79211d23665d99bfd32c124b2d8c6abdb7f9b Mon Sep 17 00:00:00 2001 From: Ian Lai Date: Fri, 27 Oct 2017 16:23:46 +0100 Subject: [PATCH 2/2] Refactor `Dockerfile` for better caching, and to minimise final image size The main goal here is to optimise for caching, build speed and final image size. It takes advantage of a multi-stage Docker build. - Build backend, and frontend in separate images - Copy artifacts from build images to final, execution image - Add `.dockerignore` to speed up build times (and improve caching) - Copy, and install npm packages before building (again, to improve caching) - Build Go binary statically The final size is under 20MB compared to the hundreds it was before. --- .dockerignore | 7 +++++++ Dockerfile | 39 +++++++++++++++++++++++++-------------- 2 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..45a52784 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,7 @@ +*.db +*.md +.dockerfile +.editorconfig +.gitignore +Dockerfile +example/ diff --git a/Dockerfile b/Dockerfile index 9af9f520..b9f29587 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,22 +1,33 @@ -# golang1.8.1 base image -FROM golang:1.8.1 +# Build backend +FROM golang:1.8.1 as backend-build -# copy source COPY . /go/src/commento WORKDIR /go/src/commento -# build backend RUN go get -v . -RUN go install . +RUN go build -ldflags '-linkmode external -extldflags -static -w' -# build frontend -RUN git clone https://github.com/creationix/nvm.git /tmp/nvm -RUN /bin/bash -c "source /tmp/nvm/nvm.sh && nvm install node" -RUN /bin/bash -c "source /tmp/nvm/nvm.sh && npm install" -RUN /bin/bash -c "source /tmp/nvm/nvm.sh && npm run-script build" -RUN cp /go/src/commento/assets /go/bin/assets -vr -RUN mkdir /data/ +# Build frontend +FROM node:8.8-alpine as frontend-build -# set entrypoint -ENTRYPOINT /go/bin/commento +COPY ./package.json /commento/package.json +WORKDIR /commento/ + +RUN npm install + +COPY ./assets/ /commento/assets/ + +RUN npm build + + +# Build final image +FROM alpine:3.6 + +COPY --from=backend-build /go/src/commento/commento /commento/ +COPY --from=frontend-build /commento/assets/ /commento/assets/ + +RUN mkdir /commento-data/ +ENV COMMENTO_DATABASE_FILE /commento-data/sqlite3.db + +ENTRYPOINT /commento/commento