alpine容器中运行go的二进制文件
发布于 2022-09-08 18:41:27阅读 1594
问题重现
/data # ls
app
/data # ./app
/bin/sh: ./app: not found
解决办法
方法一
查看下依赖库
/data # ldd app
/lib64/ld-linux-x86-64.so.2 (0x7ff4fc486000)
libpthread.so.0 => /lib64/ld-linux-x86-64.so.2 (0x7ff4fc486000)
libc.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7ff4fc486000)
/data # ls /lib64/ld-linux-x86-64.so.2
ls: /lib64/ld-linux-x86-64.so.2: No such file or directory
结果提示没找到
借鉴大神的方法
mkdir /lib64 && ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2
方法二
go build
添加参数--tags netgo
,如下
go build -mod=mod --tags netgo -o hello .
最后
看下Dockerfile
文件
FROM golang:1.19 AS build
WORKDIR /go/src/gin-demo
COPY . .
RUN go env -w GO111MODULE=on
RUN go env -w GOPROXY=https://goproxy.cn,direct
RUN go build -mod=mod --tags netgo -o hello .
FROM alpine:3.16
WORKDIR "/data"
EXPOSE 8097
# RUN mkdir /lib64 && ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2
COPY --from=build /go/src/gin-demo/hello app
CMD ["./app"]