boyter / scc

Sloc, Cloc and Code: scc is a very fast accurate code counter with complexity calculations and COCOMO estimates written in pure Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add documentation for CGO_ENABLED to fix user issues with scc

MikkelHJuul opened this issue · comments

A user of scc in containers will run into issues with running the application in alpine if they do not use compiler flag CGO_ENABLED=0.

So I was trying to run scc in a pipeline, and saw iRyanBell's implementation from your README, but I couldn't find the image; I just built my own (mjuul/scc). (I did find the image later: https://github.com/marketplace/actions/scc-docker-action -- I thought it was on docker hub).

But I ran into several issues, firstly with go get where it seems impossible to install v2.13.0 (see my description here: golang/go#35732) well... so I decided to compile it myself and I wrote the Dockerfile below

FROM golang as scc-get

ENV GOOS=linux \
    GOARCH=amd64 

ARG VERSION
RUN git clone --branch $VERSION --depth 1 https://github.com/boyter/scc
WORKDIR /go/scc
RUN go build -ldflags="-s -w"

FROM alpine
COPY --from=scc-get /go/scc/scc /bin/
ENTRYPOINT ["scc"]

then

$ docker build -t scc-image --build-arg VERSION=v2.13.0 .
$ docker run -v /some/local/path/:/root/something -w /root/something scc-image 

You get an error: standard_init_linux.go:211: exec user process caused "no such file or directory"
So I noted that iRyanBell is running the i386 (32 bit) vers. succesfully (which he didn't have to anyway). And that also works. (setting ENV GOARCH=386).

But that is very unsatisfactory; I certainly expect the program to run with the expected behavior on the same architecture.

Searching around I find: https://stackoverflow.com/questions/36279253/go-compiled-binary-wont-run-in-an-alpine-docker-container-on-ubuntu-host
and that fixes the issue.

i.e. this works:

FROM golang as scc-get

ENV GOOS=linux \
    GOARCH=amd64 \
    CGO_ENABLED=0

ARG VERSION
RUN git clone --branch $VERSION --depth 1 https://github.com/boyter/scc
WORKDIR /go/scc
RUN go build -ldflags="-s -w"

FROM alpine
COPY --from=scc-get /go/scc/scc /bin/
ENTRYPOINT ["scc"]

So the documentation should mention compiler flag CGO_ENABLED=0 for better compatibility with less dynamic linking.

Alpine images... the bane of my existence when doing anything in Go. The amount of times I have had the same issue... well its not a huge amount but enough that its etched into my brain.

If you want to write the documentation into the README ill merge it in and you get the credit :)

Otherwise ill add this in in a week or two. Cheers.

Added to README.