dongquanlin1885 2019-01-21 03:35
浏览 84
已采纳

在golang中导入存档/ tar时docker run失败

I'm trying to get a golang program that runs on the cmd line to run in a docker container, but importing archive/tar causes errors.

This works:


    package main
    import (
            "fmt"
    )
    func main() {
            fmt.Println("success!")
    }

producing the output:

    liam gotest $ docker run -it gotest
    success!

This does not:


    package main
    import (
            "archive/tar"
            "fmt"
    )
    var _ = tar.TypeReg
    func main() {
            fmt.Println("success!")
    }

producing the output:


    liam gotest $ docker run -it gotest
    standard_init_linux.go:207: exec user process caused "no such file or directory"

This is my Dockerfile:


    FROM scratch
    WORKDIR /app
    COPY . /app
    CMD ["./test"]

I'm running:

go version go1.11.4 linux/amd64
Docker version 18.09.1, build 4c52b90
ubuntu 18.04LTS

I'd appreciate any suggestions.

Solved by vishnu narayanan:

I used

`CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo test.go`

to build a static executable, and it worked.

  • 写回答

1条回答 默认 最新

  • doutou7549 2019-01-21 05:21
    关注

    Why ?

    This is because of the default go build behavior. The compiled binary is still looking for libraries in the system path due to dynamic linking.

    Since scratch is empty, the binary is not able to find the system libraries and throws an error.

    How to solve this ?

    Modify the build script to produce a static compiled build with all libraries built in.

    CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .

    Use the output binary from above build for the docker container.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗