scene reproduction
multi-stage
A 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 golang
and alpine
the operating system are linux
the same, the underlying architecture is different, so golang
the executable file generated by the image cannot run on the alpine
operating 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
- Try to build with an image with a golang environment
alpine
, and then copy the executable file generated by the build to a purealpine
image, so that the above problem does not exist.- Try to use the image
golang:1.9.4-alpine3.7
and find thatgo get -v .
it is saved when executed, the image does not containgit
tools... git
Install the tools in the build imageapk add --no-cache git
...- Finally, the image is generated and the container runs successfully. The image size is only 13MB...
- Try to use the image
- try cross compilation of golang
- Don't know much about cross-compilation...I'll try again when I get a chance