1

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

patabhu
  • 51
  • 3
  • 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. – Zac Anger Dec 16 '22 at 04:39

1 Answers1

0

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.

Campbell
  • 1
  • 1
  • 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. – patabhu Dec 16 '22 at 05:09
  • **main.go** just contains a http listerner on **8081** – patabhu Dec 16 '22 at 05:14
  • My bad can you try `ENTRYPOINT ["./handlerBuild"]` instead? I forgot to include the leading `.` – Campbell Dec 16 '22 at 05:31
  • Actually I just tried with an example go program locally and `ENTRYPOINT ["./app/handlerBuild"]` should do the trick – Campbell Dec 16 '22 at 05:38