Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

standard_init_linux.go:190: exec user process caused "exec format error" when running Go binary

Tags:

docker

go

I am attempting to create a container with my Go binary in for use as a database migrator. If I run the binary it works perfectly, however, I am struggling to put it into a container and run it in my docker-compose stack.

Below is my Dockerfile.

FROM golang:1.11 AS build_base

WORKDIR /app

ENV GO111MODULE=on
# We want to populate the module cache based on the go.{mod,sum} files.
COPY go.mod .
COPY go.sum .
RUN go mod download

FROM build_base AS binary_builder
# Here we copy the rest of the source code
COPY . .

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build

#In this last stage, we start from a fresh Alpine image, to reduce the image size and not ship the Go compiler in our production artifacts.
FROM alpine AS database-migrator
# We add the certificates to be able to verify remote instances
RUN apk add ca-certificates
COPY --from=binary_builder /app /app

ENTRYPOINT ["/app/binary-name"]

When I run my docker-compose stack the MySQL database gets setup correctly but I receive this error in the logs for my database migrator container.

data-migrator_1 | standard_init_linux.go:190: exec user process caused "exec format error"

like image 953
pocockn Avatar asked Apr 21 '19 21:04

pocockn


2 Answers

I had the same error message. For me the fix was to cross build the for the right architecture. In my case amd64. Like this:

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o [OUTPUT] .
like image 80
perelin Avatar answered Nov 11 '22 07:11

perelin


Check if this is similar to containers/buildah issue 475 :

I think it is because the system does not know how to execute the file.
FYI: What's the appropriate Go shebang line?

Also be aware of the difference between the shell form and exec form of CMD/ENTRYPOINT in Dockerfile.

just adding #!/bin/bash to my entry point file fixed the issue.

Or:

Turns out the #!/bin/bash was in my entry point file, but since I did a copy and paste into that file, the first line was a newline, not the #!/bin/bash, effectively ignoring it.
If this helps anyone as well: Deleted the empty line and all worked fine.

Or:

In case anyone finds this useful, you can get this issue if your shell script uses CRLF for line endings and/or UTF-8 with BOM (e.g. if you created a shell script file in Visual Studio).
Changing to LF only and straight UTF-8 fixed it for me.

Or (probably not your case, but to be complete):

For anyone who got a standard_init_linux.go:190: exec user process caused "no such file or directory" error after applying this fix, you're probably on an alpine base image which does not come with bash.

Replacing #!/bin/bash with #!/bin/sh will do the trick!

like image 33
VonC Avatar answered Nov 11 '22 07:11

VonC