我尝试使用从头开始构建一个小图像。我关闭了 CGO,但程序运行时仍然无法读取文件。我收到错误消息:“打开 ./app/a.txt:没有这样的文件或目录”。程序无法读取文件还有其他原因吗?
FROM golang:alpine AS builder
RUN apk update && apk add --no-cache git
WORKDIR $GOPATH/src/scratch
ADD . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -tags netgo -ldflags '-w -extldflags "-static"' -o /go/bin/scratch
FROM scratch
# Copy static executable.
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /go/bin/scratch /go/bin/scratch
# Run the binary.
ENTRYPOINT ["/go/bin/scratch"]
func main() {
resp, err := http.Get("https://google.com")
check(err)
body, err := ioutil.ReadAll(resp.Body)
check(err)
fmt.Println(len(body))
LocalFile := "./app/a.txt"
fmt.Println(LocalFile)
dat, err := ioutil.ReadFile(LocalFile)
check(err)
fmt.Print(string(dat))
f, err := os.Open(LocalFile)
check(err)
b1 := make([]byte, 5)
n1, err := f.Read(b1)
check(err)
fmt.Printf("%d bytes: %s\n", n1, string(b1[:n1]))
}
func check(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
绝地无双
Cats萌萌
相关分类