Container is not able to find executable binary in working directory

I created a Golang backend and use a Makefile for common scripts. A snippet of the build command:

BINARY_NAME=myapp
MODULE_PATH=github.com/me/${BINARY_NAME}

BUILD_DIR=./build

build:
	@go build -o $(BUILD_DIR)/$(BINARY_NAME)

I started with the following Dockerfile

# Build stage
FROM golang:1.21.0-bullseye AS build
WORKDIR /app

ADD . .

RUN make build

# Run stage
FROM alpine:3.18
WORKDIR /app

COPY --from=build /app/build/myapp .

# CMD [ "./myapp" ]

Building the image works fine but when trying to execute the binary I get the error that it’s missing. I tried to inspect the container, this is the output

❯ docker container run -it theimage
/app # ls
myapp
/app # ./myapp
/bin/sh: ./myapp: not found

So I can see the binary exists. But I don’t know why the container is not able to find it when I want to execute it.

Do you know what’s wrong or missing here?

You built a binary in a debian container and want to run it in an Alpine container. The libraries are not compatible. Try to use an Alpine based golang image to build the binary or build all the required libraries into the binary. I never tried to build in a debian container and run in an alpine container, but that should work. I have a GitHub repo where I wrote about how you can make a binary that includes all the requirements so you could have an image from scratch that contains only the go binary so it should be able to run in an Alpine container as well