Docker alpine部署go项目失败分析

https://yryz.net/post/golang-docker-alpine-start-panic.html

用docker基于alpine微型镜像部署go的项目,启动时报错 panic: standard_init_linux.go:175: exec user process caused "no such file or directory",去年测试docker时遇到过,没去深入研究,这次项目遇又到了,深入分析了一下。

详细错误记录:

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

 

➜ xxx git:(master) ✗ docker run --rm -it xxxx.com/xxx_webapp:prod

panic: standard_init_linux.go:175: exec user process caused "no such file or directory" [recovered]

panic: standard_init_linux.go:175: exec user process caused "no such file or directory"

goroutine 1 [running, locked to thread]:

panic(0x88f8a0, 0xc82011bb20)

/usr/local/go/src/runtime/panic.go:481 +0x3e6

github.com/urfave/cli.HandleAction.func1(0xc8200e72e8)

/tmp/tmp.UkUQ4KXBPZ/src/github.com/opencontainers/runc/Godeps/_workspace/src/github.com/urfave/cli/app.go:478 +0x38e

panic(0x88f8a0, 0xc82011bb20)

/usr/local/go/src/runtime/panic.go:443 +0x4e9

github.com/opencontainers/runc/libcontainer.(*LinuxFactory).StartInitialization.func1(0xc8200e6bf8, 0xc82001a0c8, 0xc8200e6d08)

/tmp/tmp.UkUQ4KXBPZ/src/github.com/opencontainers/runc/Godeps/_workspace/src/github.com/opencontainers/runc/libcontainer/factory_linux.go:259 +0x136

github.com/opencontainers/runc/libcontainer.(*LinuxFactory).StartInitialization(0xc820051630, 0x7fc3c9efc728, 0xc82011bb20)

/tmp/tmp.UkUQ4KXBPZ/src/github.com/opencontainers/runc/Godeps/_workspace/src/github.com/opencontainers/runc/libcontainer/factory_linux.go:277 +0x5b1

main.glob.func8(0xc82006ea00, 0x0, 0x0)

/tmp/tmp.UkUQ4KXBPZ/src/github.com/opencontainers/runc/main_unix.go:26 +0x68

reflect.Value.call(0x7f45a0, 0x9a4d88, 0x13, 0x8ebac8, 0x4, 0xc8200e7268, 0x1, 0x1, 0x0, 0x0, ...)

/usr/local/go/src/reflect/value.go:435 +0x120d

reflect.Value.Call(0x7f45a0, 0x9a4d88, 0x13, 0xc8200e7268, 0x1, 0x1, 0x0, 0x0, 0x0)

/usr/local/go/src/reflect/value.go:303 +0xb1

github.com/urfave/cli.HandleAction(0x7f45a0, 0x9a4d88, 0xc82006ea00, 0x0, 0x0)

/tmp/tmp.UkUQ4KXBPZ/src/github.com/opencontainers/runc/Godeps/_workspace/src/github.com/urfave/cli/app.go:487 +0x2ee

github.com/urfave/cli.Command.Run(0x8ee970, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x984240, 0x51, 0x0, ...)

/tmp/tmp.UkUQ4KXBPZ/src/github.com/opencontainers/runc/Godeps/_workspace/src/github.com/urfave/cli/command.go:191 +0xfec

github.com/urfave/cli.(*App).Run(0xc820001500, 0xc82000a100, 0x2, 0x2, 0x0, 0x0)

/tmp/tmp.UkUQ4KXBPZ/src/github.com/opencontainers/runc/Godeps/_workspace/src/github.com/urfave/cli/app.go:240 +0xaa4

main.main()

/tmp/tmp.UkUQ4KXBPZ/src/github.com/opencontainers/runc/main.go:137 +0xe24

首先确认被执行的程序都是存在的,经过一番搜索测试也没解决。

之前用GOOS=linux GOARCH=amd64 go build是可以的,这次是通过golang的docker镜像编译的确不行,对比编译后的文件发现有猫腻。

 

1

2

3

 

> GOOS=linux GOARCH=amd64 go build

> file xxx

xxx: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped

vs docker build:

 

1

2

3

 

> docker run --rm -it -v $GOPATH:/go golang:1.7 bash -c 'cd $GOPATH/src/xxx && go build

> file xxx

xxx: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, not stripped

问题找到了,一个是静态链接,一个是动态链接,动态链接的在微型镜像alpine上不支持。

总结

  1. 默认go使用静态链接,在docker的golang环境中默认是使用动态编译。
  2. 如果想使用docker编译+alpine部署,可以通过禁用cgoCGO_ENABLED=0来解决。
  3. 如果要使用cgo可以通过go build --ldflags "-extldflags -static" 来让gcc使用静态编译。

参考:Linking golang statically

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Jenkins是一个开源的持续集成工具,而Docker是一个容器化平台,我们可以利用Jenkins和Docker部署Golang项目。 首先,我们需要在Jenkins中安装Docker插件,以便在构建过程中使用Docker容器。Docker插件允许我们在构建任务中创建、启动和停止Docker容器。 接下来,我们可以配置Jenkins的构建任务。在构建任务中,我们可以指定源代码仓库的地址,以及一些其他的构建步骤,例如代码编译、单元测试等。 在构建步骤中,我们可以使用Docker插件创建一个Docker容器来运行我们的Golang项目。可以使用Dockerfile来定义容器的环境和依赖项。Dockerfile是一个包含一系列命令的文件,用于构建Docker镜像,其中包括构建、安装Golang项目所需的环境和依赖项。 一旦Docker容器创建完成,我们可以在容器中执行一些命令来编译和运行我们的Golang项目。可以使用Docker命令来执行这些命令,例如docker exec命令可以在容器中执行某个命令。 最后,我们可以配置Jenkins任务的后续步骤,例如将构建结果发布到某个服务器或云平台上。 通过Jenkins和Docker的集成,我们可以轻松地实现Golang项目的持续集成和部署。Jenkins可以自动触发构建任务,使用Docker容器来创建项目的运行环境,从而简化了部署的流程。这种方式还可以确保每次构建的环境完全一致,提高了应用程序的可移植性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值