Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancements: configurable database file path, default port, and optimised Docker image #109

Merged
merged 2 commits into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*.db
*.md
.dockerfile
.editorconfig
.gitignore
Dockerfile
example/
39 changes: 26 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +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'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a review comment, just curious: what does this do? I haven't been writing Go long enough ^^

Searching tells me -linkmode external can be used to load go variables at compile time. Is that the reasoning?

-static is static linking, I can tell. -w is some kind of executable size reduction technique from what I read (add -s as well?).

Copy link
Author

@MrSaints MrSaints Oct 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I went with this versus a standard go build is because I wanted to build a static binary from the golang Docker image, and still be able to use it in alpine. There are a couple of problems if we rely on the former.

One of which is your project's use of mattn/go-sqlite3. It is a cgo package, not a Go package. Your project however, is in Go. So we have situation where we need a combination of Go, and non-Go code to be in the final executable without dynamic libraries. So, -linkmode external simply tells cmd/linker to invoke the host linker (often gcc) to combine your Go code (compiled by Go to an object file go.o, but not processed any further) with your non-Go code.

It is also possibly using cgo from the net library (see https://golang.org/pkg/net/#hdr-Name_Resolution).

Everything after -extldflags are flags for gcc (-w to suppress warnings IIRC).

TL;DR: I just want a static binary. If I relied on the standard go build, we'd get an 'output' that relies on dynamic linking, and it will not be executable on alpine.

(see mattn/go-sqlite3#384 (comment) for more information)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, thanks for the explanation.


# 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

# set entrypoint
ENTRYPOINT /go/bin/commento
# Build frontend
FROM node:8.8-alpine as frontend-build

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
7 changes: 4 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -29,7 +30,6 @@ func main() {
http.HandleFunc("/get", GetCommentsHandler)

port := os.Getenv("COMMENTO_PORT")

svr := &http.Server{
Addr: ":" + port,
ReadTimeout: 5 * time.Second,
Expand Down