2

using cmd docker compose up inside testdocker folder.

getting below error enter image description here

here is my dockerfile

# golang base image
FROM golang:1.19-alpine as golangBase
RUN mkdir /app
ADD . /app
WORKDIR /app
RUN go mod tidy && go build -o handlerBuild main.go

FROM ubuntu:latest
RUN mkdir /app
WORKDIR /app
COPY --from=golangBase /app .
ENTRYPOINT [ "/app/handlerBuild" ]

here is my docker-compose.yml

enter image description here

having file structure like below /testdocker

/testdocker/main.go

/testdocker/go.mod

/testdocker/dockerfild

/testdocker/docker-compose.yml

Edit: found the solution here, needed flag CGO_ENABLED=0, while building. Go-compiled binary won't run in an alpine docker container on Ubuntu host

1
  • 1
    For starters, you're building on Alpine but running on Ubuntu; this is a problem because binaries built on Alpine don't work in most other Linuxes. Also the usual reason for a multistage build is to have a smaller final image, both in size and attack surface. Additionally, your COPY in the ubuntu image is incorrect as pointed out in Campbell's answer. Commented Dec 16, 2022 at 4:39

1 Answer 1

1

I think that when you COPY --from=golangBase /app . from golangBase, because golangBase already has WORKDIR app, Docker tries to copy from /app/app on golangBase (resulting in a file not found error).

Can you try COPY --from=golangBase . .? Additionally, because you set WORKDIR app on the ubuntu image, I think your entrypoint should just be ENTRYPOINT ["/handlerBuild"]. In other words, you can think of WORKDIR appas telling the Docker imagecd appso all commands thereafter should be run relative to the/app` path.

Unrelated note: I'm pretty sure WORKDIR app creates the directory app and also adds it. So instead of

RUN mkdir /app
ADD . /app
WORKDIR /app

I'm pretty sure you could just put

WORKDIR /app

and have the same effect. Correct me if I'm wrong though — I couldn't actually run the Docker build commands because I don't have your source code.

Sign up to request clarification or add additional context in comments.

5 Comments

did everything you said, but still its not working. now i am getting error like Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "handlerBuild": executable file not found in $PATH: unknown. if i just change ubuntu:latest to alpine:latest It works, but we need ubuntu image only.
main.go just contains a http listerner on 8081
My bad can you try ENTRYPOINT ["./handlerBuild"] instead? I forgot to include the leading .
Actually I just tried with an example go program locally and ENTRYPOINT ["./app/handlerBuild"] should do the trick
thanks for this! you helped me a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.