R
R
Romses Panagiotis2021-01-22 20:24:33
linux
Romses Panagiotis, 2021-01-22 20:24:33

Why does a Go program behave differently in an alpine:latest container than in golang:1.15?

The program uses output from tail.

Go program code

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")
}

Dockerfile

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"]

If you uncomment the lines with alpine, the image will no longer work correctly.
Outputs lines
2021/01/22 17:12:55 scanner exit
2021/01/22 17:12:55 cmd.Wait error: exit status 1


Why is this happening? Is the Alpine environment to blame? Busybox?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
raiboon, 2021-01-22
@raiboon

Either run in the same OS or compile with CGO_ENABLED=0

B
basrach, 2021-01-23
@basrach

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 question

Ask a Question

731 491 924 answers to any question