Brian
Brian

Reputation: 976

precompiled golang on alpine

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.

Upvotes: 2

Views: 6971

Answers (3)

valiano
valiano

Reputation: 18611

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.

Upvotes: 2

Kirill
Kirill

Reputation: 8331

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

Upvotes: 3

larsks
larsks

Reputation: 312128

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).

Upvotes: 4

Related Questions