Building in Docker on Raspberry Pi

The issue

A while ago whilst building Docker images for the Raspberry Pi I was hitting the following issue in the containers logs:

api_1  | standard_init_linux.go:219: exec user process caused: no such file or directory
api_1  | standard_init_linux.go:219: exec user process caused: no such file or directory
api_1  | standard_init_linux.go:219: exec user process caused: no such file or directory

I left this for a few days, confused as to why for the life of me I could not get this to work. I was having to push all my containers to my x86 system which I didn't want to be doing as I use a Pi as a Go dev server.

Then as I was testing the lounge for a web based irc client I joined #go-nuts on Freenode and noticed some discussion of the Raspberry Pi builds. Thought it may be a long-shot for some to know what this exact issue was in Docker but I asked anyway. Some kind fellow spotted my message in the chat, replied me and said wait 2 minutes. 2 minutes later he appeared back with the exact line I needed. I wish I knew who sent me this line as it has saved me many hours searching... I have never found this online since unless searching for the exact line, of which it rarely mentions to Docker and the Pi.

The solution

One ENV line to add to the Dockerfile for building:

ENV CGO_ENABLED=0

Just this single line. I do not understand the full depths of how this works but there is a useful article here that explains how the complilation works here.

Below is a full example of how I build for Raspberry Pi. This exact sample can be found in my utils-api project.

FROM golang:latest as builder
RUN mkdir /build
ADD . /build/
WORKDIR /build
ENV CGO_ENABLED=0
RUN go build -ldflags="-s -w" -o main .

FROM alpine:latest
WORKDIR /app
COPY --from=builder /build/main /app/
ENTRYPOINT [ "./main" ]