doutong6814 2018-03-14 20:23
浏览 34
已采纳

静态链接上没有可访问的服务器转为二进制

Depending on where my binary is being executed, I get different results on mgo Dial.

Right now, I'm building on my machine (Fedora: uname -a: Linux localhost.localdomain 4.15.6-300.fc27.x86_64 #1 SMP Mon Feb 26 18:43:03 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux) using the following command:

$ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -ldflags '-s' -o myProgram

So, if I build my docker image using:

FROM centos
COPY myProgram "/usr/local/bin/myProgram"
ENTRYPOINT ["/usr/local/bin/myProgram"]

It works perfectly. That means I'm connected to the database. But, if I change to:

FROM debian
COPY myProgram "/usr/local/bin/myProgram"
ENTRYPOINT ["/usr/local/bin/myProgram"]

I'm getting no reachable servers. My goal is to compile the application on gitlab-ci using the golang image, and run it on a alpine container.

The question is: Why the same executable get different results on different base images?

Does mgo (or go) use something related to the OS? I mean, it seems that my binary will run only on red hat based distribution (just a guess, it doesn't make much sense to me right now.)

Dial source code:

dialInfo := &mgo.DialInfo{
    Addrs:          config.Addr,
    Database:       config.Auth,
    Username:       config.User,
    Password:       config.Pass,
    ReplicaSetName: config.ReplicaSet,
    Timeout:        time.Second * 10,
}
dialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
    return tls.Dial("tcp", addr.String(), &tls.Config{})
}

session, err := mgo.DialWithInfo(dialInfo)
if err != nil {
    log.Fatal(err.Error())
}
  • 写回答

1条回答 默认 最新

  • dousi1994 2018-03-15 16:37
    关注

    Just solved the mystery. It's indeed related to the docker base image, and not to the build step.

    It'll work perfectly if I do:

    FROM debian
    RUN apt-get update
    RUN apt-get install -y ca-certificates
    

    As my goal is to use the alpine image, I'm using the following right now:

    FROM alpine
    RUN apk --no-cache add ca-certificates
    

    Hope that helps someone with the same problem. For more information, see: http://blog.cloud66.com/x509-error-when-using-https-inside-a-docker-container/

    PS.: mgo (no reachable servers) error message was pointing me out in the wrong direction.

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

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线