use smaller container as base

This commit is contained in:
Josh Deprez 2021-01-05 20:47:41 +11:00
parent 4396ec6592
commit 1ecb5ecdba
2 changed files with 12 additions and 16 deletions

View file

@ -1,9 +1,10 @@
FROM golang:latest
FROM golang:latest as builder
WORKDIR /go/src/sungrow
COPY . .
RUN go get -d -v ./...
RUN go install -v ./...
RUN CGO_ENABLED=0 go build -v -o sungrow .
CMD ["sungrow"]
FROM gcr.io/distroless/static:latest
COPY --from=builder /go/src/sungrow/sungrow /usr/bin/
CMD ["/usr/bin/sungrow"]

17
main.go
View file

@ -6,9 +6,9 @@ import (
"flag"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"time"
"github.com/goburrow/modbus"
@ -64,11 +64,6 @@ func init() {
}
}
func die(f string, args ...interface{}) {
fmt.Fprintf(os.Stderr, f+"\n", args...)
os.Exit(1)
}
func statusHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "not implemented", http.StatusNotFound)
}
@ -76,7 +71,7 @@ func statusHandler(w http.ResponseWriter, r *http.Request) {
func readRegs(client modbus.Client, start, qty uint16) {
data, err := client.ReadInputRegisters(start, qty)
if err != nil {
die("Couldn't read input registers %d-%d: %v", start+1, start+qty, err)
log.Fatalf("Couldn't read input registers %d-%d: %v", start+1, start+qty, err)
}
for addr, reg := range sungrowInputRegs {
if addr <= start || addr > start+qty {
@ -105,7 +100,7 @@ func main() {
// Is the inverter reachable?
sgc, err := dialSungrow(*inverterAddr)
if err != nil {
die("Couldn't dial inverter: %v", err)
log.Fatalf("Couldn't dial inverter: %v", err)
}
defer sgc.Close()
@ -114,14 +109,14 @@ func main() {
// to connect to, and connect to it immediately.
ln, err := net.Listen("tcp", "localhost:0")
if err != nil {
die("Couldn't open listener: %v", err)
log.Fatalf("Couldn't open listener: %v", err)
}
defer ln.Close()
go func() {
// Only one connection needed!
conn, err := ln.Accept()
if err != nil {
die("Couldn't accept connection: %v", err)
log.Fatalf("Couldn't accept connection: %v", err)
}
go io.Copy(sgc, conn)
io.Copy(conn, sgc)
@ -140,7 +135,7 @@ func main() {
scrape(client)
// Start http interface only after first successful scrape
go func() {
die("http.ListenAndServe: %v", http.ListenAndServe(*httpAddr, nil))
log.Fatalf("http.ListenAndServe: %v", http.ListenAndServe(*httpAddr, nil))
}()
for range time.Tick(*scrapeInterval) {
scrape(client)