Alessandro Argentieri
Alessandro Argentieri

Reputation: 3215

Docker golang busybox "no such file or directory" error

I'm building a multistage Docker image of a golang microservice and I want to make it very thin using busybox as base image to run the final executable. The image is correctly built, but when I run it I get this error:

standard_init_linux.go:211: exec user process caused "no such file or directory"

I'm working on my Ubuntu laptop, so this error has nothing to do with the Windows OS as many other questions report.

This is my image.

# build stage
FROM golang:1.15.3 AS build-stage

RUN mkdir /build
ADD . /build/
WORKDIR /build

RUN go mod download
RUN go test ./...
RUN go build -o goapp .

# final stage
FROM busybox
WORKDIR /app
COPY --from=build-stage /build/goapp /app/

CMD ["./goapp"]

A very simplified version of my project folder could be:

project
├── Dockerfile
├── go.mod
├── go.sum
├── main.go
└── other-packages

Upvotes: 0

Views: 1883

Answers (2)

Flying onion
Flying onion

Reputation: 136

I guess there's a difference between the libc of the two images. As introduced in description of busybox image, there are several libc variants, which is distinguished by image tags.

The libc of golang:1.15.3 is glibc (it is FROM the corresponding version of debian:buster), so you should use busybox:glibc on your final stage.

The default busybox:latest is using uclibc. They are incompatible. Check the sha256 digest of busybox:latest and busybox:uclibc.

Upvotes: 0

colm.anseo
colm.anseo

Reputation: 22117

You are building your app with CGO_ENABLED=1 (this is Go's default setting for builds) - and transplanting that executable to a docker image where there is no compatible library support (glibc, dns resolver etc.).

To ensure your build does not rely on any external libraries - statically binding all dependencies - and can then be deployed to a SCRATCH docker image or busybox - disable CGO during your build phase:

RUN CGO_ENABLED=0 go build -o goapp .

Upvotes: 1

Related Questions