0

I am unable to dockerize and use a utility written in c in go.

I have run this program locally without docker and it works

I tried using gccgo like so go build -compiler gccgo -gccgoflags -static-libgo but I get the same error

The preamble that calls the C functions looks like so:

/*
#cgo amd64 x86 LDFLAGS: -L. -lsomelib -lsomeotherlib
#include <stdio.h>
#include <stdlib.h>
#include "someheader.h"
*/

My docker file looks like so:

FROM golang:1.12 AS build

WORKDIR /go/src/app
COPY . .

ENV GOOS=linux
ENV GOARCH=amd64
ENV CGO_LDFLAGS_ALLOW='-linkmode external -extldflags -static-libgcc'

COPY packageFolder $GOPATH/src/packageFolder
COPY mainPackage $GOPATH/src/mainPackage

RUN cd packageFolder
RUN go get -d -v ./...
RUN CGO_ENABLED=1 go build --ldflags '-linkmode external -extldflags -static-libgcc' -o $GOPATH/pkg/linux_amd64/packageFolder.a -x
RUN cd ../packageFolder
RUN go get -d -v ./...
RUN CGO_ENABLED=1 go build --ldflags '-linkmode external -extldflags -static-libgcc' -o $GOPATH/pkg/linux_amd64/mainPackage.a -x
RUN cd ..
RUN go get -d -v ./...
RUN go build -a -x

FROM ourPackager:latest AS packager
WORKDIR /
COPY ./resources ./resources/
RUN appman-packager create-package "package.tar.gz" ./resources

FROM scratch AS runtime
COPY --from=build /go/src/app/app /
COPY --from=packager "/package.tar.gz" ./resources/
EXPOSE 8080/tcp
ENTRYPOINT ["/app"]

I keep running into standard_init_linux.go:207: exec user process caused "no such file or directory" when I do a docker run

What am I missing?

dirtyqwerty
  • 185
  • 2
  • 16
  • Maybe [convert line endings](https://stackoverflow.com/a/52665687/3915)? – Mark May 20 '19 at 21:42
  • I saw some posts about changing line endings but I am not running any shell/ bash scripts in my code. What do I need to change line endings of? – dirtyqwerty May 20 '19 at 22:29
  • 1
    Suspect the ENTRYPOINT is trying to run your app, but your app can't find some required libraries (or libraries required by those libraries). This might be due to the 'scratch' image not having standard c libraries, for example. Try replacing 'scratch' with 'golang:1.12'. – Mark May 20 '19 at 23:52
  • Mark I just tried that. Now I am getting /app: error while loading shared libraries: myExternalTool.so: cannot open shared object file: No such file or directory so it looks like it is still not statically linking in my 'build' stage if I understand correctly – dirtyqwerty May 21 '19 at 00:17

1 Answers1

1

I was able to fix it with Mark's suggestion. Using a Golang image for runtime exposed the actual problem of the shared object file not being packaged. So I copy it to /usr/lib/x86_64-linux-gnu in my runtime. I ended up using ubuntu:18.04 instead of the Golang image at runtime

FROM golang:1.12 AS build

WORKDIR /go/src/app
COPY . .

ENV GOOS=linux
ENV GOARCH=amd64
ENV CGO_ENABLED=1
COPY acrcloud $GOPATH/src/packageFolder
COPY musicrec $GOPATH/src/mainPackage

RUN cd packageFolder
RUN go get -d -v ./...
RUN go build -o $GOPATH/pkg/linux_amd64/packageFolder -x
RUN cd ../mainPackage
RUN go get -d -v ./...
RUN go build -o $GOPATH/pkg/linux_amd64/mainPackage -x
RUN cd ..
RUN go get -d -v ./...
RUN go build -a -x

FROM ourPackager:latest AS packager
WORKDIR /
COPY ./resources ./resources/
RUN appman-packager create-package "package.tar.gz" ./resources

FROM ubuntu:18.04 AS runtime
COPY --from=build /go/src/app/app /
COPY --from=build /go/src/app/myExternalTool.so /usr/lib/x86_64-linux-gnu
COPY --from=packager "/package.tar.gz" ./resources/
EXPOSE 8080/tcp
ENTRYPOINT ["/app"]
dirtyqwerty
  • 185
  • 2
  • 16