Answer the question
In order to leave comments, you need to log in
Why does a Go program behave differently in an alpine:latest container than in golang:1.15?
The program uses output from tail
.
package main
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
)
func main() {
if len(os.Args) != 2 {
os.Exit(1)
}
logFilePath := os.Args[1]
// fi, err := os.Stat(logFilePath)
// if err != nil {
// log.Fatal(err)
// }
// fmt.Printf("Stat: %s, %v\n", fi.Mode().String(), fi)
cmd := exec.Command("tail", "-f", "-n 20", logFilePath)
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
if err := cmd.Start(); err != nil {
log.Fatal("cmd.Start error: " + err.Error())
}
go func() {
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
line := scanner.Text()
fmt.Printf("SCAN: %q\n", line)
if line == "~~END~~" {
break
}
}
log.Println("scanner exit")
}()
if err := cmd.Wait(); err != nil {
log.Fatal("cmd.Wait error: " + err.Error())
}
log.Println("DONE")
}
FROM golang:1.15
ADD . /app
WORKDIR /app
ENV GOOS=linux
ENV GOARCH=amd64
ENV CGO_ENABLED=0
RUN go build -ldflags="-w -s" -o ttt
# FROM alpine:latest
# RUN apk --no-cache add ca-certificates tzdata
# WORKDIR /app
# COPY --from=0 /app/ttt .
# /tmp/logs/bla-bla.log - доступен через примонтированный к Docker том
CMD ["./ttt", "/tmp/logs/bla-bla.log"]
2021/01/22 17:12:55 scanner exit
2021/01/22 17:12:55 cmd.Wait error: exit status 1
Answer the question
In order to leave comments, you need to log in
It seems that a flag needs to be specified -a
, i.e. go build -a ...
.
As far as I understand, together with the flag CGO_ENABLED=0
, this causes something like "compiling" some of the used std libs into a binary. Without this, runtime relies on OS components that are either not in alpine or they are different.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question