1

According to this post: What does go build build?

go build builds the binary and leaves it in the current directory, while go install moves the binary into $GOPATH/bin.

This works on my machine like that. However, I have a Dockerfile:

FROM golang:latest
ADD . /go/src/myapp
WORKDIR /go/src/myapp
RUN go get .
RUN go build 
RUN ls /go/bin # shows myapp!!
ENTRYPOINT ["/go/bin/myapp"] #works!!

There should be no difference here. The expected result was that /go/bin was empty and the binary myapp would be /go/src/myapp/myapp Instead, I see that build is working like install, and the binary shows up in /go/bin. Why??

Tommy
  • 12,588
  • 14
  • 59
  • 110
  • The `go get` command may do that. What happens if you remove the build command? Is /go/bin/myapp still there? – Peter Feb 13 '19 at 18:38

1 Answers1

1

The go get command is installing your package into /go/bin. If you dont want to install your current package then pass -d to stop after downloading your dependencies.

The -d flag instructs get to stop after downloading the packages; that is, it instructs get not to install the packages.

https://golang.org/cmd/go/#hdr-Download_and_install_packages_and_dependencies

codestation
  • 2,938
  • 1
  • 22
  • 22
  • why is the behavior different on my local machine? Also, is go build overwriting it? – Tommy Feb 14 '19 at 13:52
  • I have confirmed that in Docker, passing -d breaks the dockerfile as expected, so this answer is correct. However it is still a mystery why go get doesn't function the same locally. Doing an ls on bin after go get locally does NOT find myapp. – Tommy Feb 14 '19 at 14:05
  • @Tommy do you have your software on the GOPATH on your local machine? Go tools have different behaviour depending on the GOPATH. I did my test on both local and docker and go get installed the binary on GOPATH/bin. – codestation Feb 14 '19 at 14:30