Getting 'executable file not found in $PATH' error when running 'go run' command with exec.Command in golang container

186 Views Asked by At

I have a golang docker container. My Dockerfile contains:

FROM docker.io/golang:1.21.7-alpine3.19 AS builder

In my local everthing works fine. I can run this command in my golang project:

cmd := exec.Command("go", "run", "anotherProgram.go", param1, param2)

But when I run this command in my docker container, I get this error:

exec: 'go': executable file not found in $PATH

I am sure 'anotherProgram.go' file exist in my container. What is the problem? How can I run another go file in my golang docker container?

1

There are 1 best solutions below

0
Sebastian Yilmaz On

I added these lines to my Dockerfile:

COPY --from=builder /usr/local/go/ /go
ENV GOROOT=/go
ENV PATH=$PATH:$GOROOT/bin

It works like a charm.