Problems with dockerfile multi-stage for Go application

scene reproduction

multi-stageA simple go application, ready to be deployed via docker, uses the build to reduce the size of images and containers at runtime :

# dockerfile 大致如下

# 一级构建使用带golang环境的镜像
FROM golang:1.8.3 AS app-build
WORKDIR /go/src/app/
RUN go install
# 生成可执行文件 

# 二级构建使用alpine:3.7,体积小
FROM alpine:3.7
# 拷贝上面一级构建中生成的可执行文件
COPY --from=golb-build ./go/bin/app .
ENTRYPOINT ["/go/bin/app"]

The dockefile build is normal, but an error will be reported when running the container:

docker run --name app-1 --publish 80:80 -it app:latest
standard_init_linux.go:195: exec user process caused "no such file or directory"

problem causes

It is said that although the image golangand alpinethe operating system are linuxthe same, the underlying architecture is different, so golangthe executable file generated by the image cannot run on the alpineoperating system (Where do you see the English explanation, and if you see it again, add the link)

  • glibc is commonly used under Linux such as debian/ubuntu;
  • But the musl libc used under alpine.

solution

  1. Try to build with an image with a golang environment alpine, and then copy the executable file generated by the build to a pure alpineimage, so that the above problem does not exist.
    • Try to use the image golang:1.9.4-alpine3.7and find that go get -v .it is saved when executed, the image does not contain gittools...
    • gitInstall the tools in the build image apk add --no-cache git...
    • Finally, the image is generated and the container runs successfully. The image size is only 13MB...
  2. try cross compilation of golang
    • Don't know much about cross-compilation...I'll try again when I get a chance

Reference documentation

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324934246&siteId=291194637