I need to run Golang application on Docker machine.
I’m working on Mac OSX and Docker is working on top of Linux virtual machine, so binaries builded on Mac are not runnable on Docker.
I see two ways here:
- cross-compile binaries on Mac for linux OS
- copy project sources to docker, run ‘go get’ and ‘go build’ on it
First one is hard because of CGO (it is used in some imported libraries).
Second is very slow because of ‘go get’ operation.
Can you please tell me, which way is the most common in that situation? Or maybe I’m doing something wrong?
Advertisement
Answer
Here a solution to make cross-compile super easy even with CGO.
I stumbled upon it recently after wasting a lot of time getting a new windows build server to build my Go app. Now I just compile it on my Mac and will create a Linux build server with it:
https://github.com/karalabe/xgo
Many thanks to Péter Szilágyi alias karalabe for this really great package!
How to use:
- have Docker running
- go get github.com/karalabe/xgo
- xgo –targets=windows/amd64 ./
There are lots more options!
— edit —
Almost 3 Years later I’m not using this any more, but my docker image to build my application in a linux based CD pipeline is still based on the docker images used in xgo
.