precompiled golang on alpine

六月ゝ 毕业季﹏ 提交于 2019-12-11 17:04:52

问题


I'm trying to write a dockerfile that uses alpine and takes advantage of a precompiled golang.

docker run -it  alpine:latest
wget https://dl.google.com/go/go1.12.9.linux-amd64.tar.gz --no-check-certificate
tar -C /usr/local/ -xzf go1*.tar.gz

I'm getting /bin/sh/: ./go: not found

cd /usr/local/go/bin/
./go

It works fine on my ubuntu laptop so I'm unsure what the difference is here. I did a quick google and I could not find anything clear that points to something missing.


回答1:


Alpine is built using the MUSL C library. You cannot run binaries that have been compiled for glibc in this environment. You would need to find a go binary built explicitly for the Alpine platform (e.g. by running apk add go).




回答2:


You need to use golang:alpine base image for your Dockerfile (see available tags: https://hub.docker.com/_/golang):

FROM golang:alpine

RUN go version

or:

docker run -it golang:alpine /bin/sh



回答3:


As answered by larsks, Alpine will usually not run glibc built binaries, at least not out of the box.

However, there are cases in which the needed binary is not available, for example, the exact needed go build is not available for Alpine. Or, the case of Java (AdoptOpenJDK), where you need certified binaries, that are only available for glibc builds.

In these cases, you could create a glibc-enabled Alpine container:

# Based on: https://github.com/anapsix/docker-alpine-java
FROM alpine:latest

ENV GLIBC_REPO=https://github.com/sgerrand/alpine-pkg-glibc
ENV GLIBC_VERSION=2.28-r0

RUN set -ex && \
    apk --update add libstdc++ curl ca-certificates && \
    for pkg in glibc-${GLIBC_VERSION} glibc-bin-${GLIBC_VERSION}; \
        do curl -sSL ${GLIBC_REPO}/releases/download/${GLIBC_VERSION}/${pkg}.apk -o /tmp/${pkg}.apk; done && \
    apk add --allow-untrusted /tmp/*.apk && \
    rm -v /tmp/*.apk && \
    /usr/glibc-compat/sbin/ldconfig /lib /usr/glibc-compat/lib

alpine-pkg-glibc is a glibc custom built for Alpine (musl libc).
This procedures enables running glibc programs on your Alpine container.

Note, it does increase Alpine base image size - from 5.6MB to 16.5MB - but this seems a small price to pay for the desired compatibility in these cases, especially if the installed programs are themselves quite large.



来源:https://stackoverflow.com/questions/57527537/precompiled-golang-on-alpine

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!