Golang Mac OSX for Docking Machine

I need to run the application on Golang Docker car.

I am working on Mac OSX, and Docker runs on top of the Linux virtual machine, so binaries created on Mac do not run on Docker.

Here I see two ways:

  • cross compiling binaries on Mac for Linux
  • copy project sources to docker, run 'go get' and 'go build' on it

The first one is complicated due to CGO (it is used in some imported libraries).

The second is very slow due to the go get operation.

Could you tell me which path is the most common in this situation? Or maybe I'm doing something wrong?

+6
source share
3 answers

Here's a solution to make cross-compiling super easy even with CGO.

I recently came across this, spending a lot of time getting a new Windows build server to build my Go application. Now I just compile it on my Mac and create a Linux build server:

https://github.com/karalabe/xgo

Thanks a lot to Péter Szilágyi alias karalabe for this really great package!

How to use:

  • Docker works
  • go get github.com/karalabe/xgo
  • xgo --targets = windows / amd64. /

There are many more options!

- edit -

Almost 3 years later, I do not use it anymore, but my way of dockers to build my application in the pipeline CD based on Linux is still based on the images xgo used xgo .

+3
source

I use the first approach. Here his gulp task is the build code. If the flag is set production, it starts GOOS=linux CGO_ENABLED=0 go build instead go build . This way the binary will work inside the docker container

 gulp.task('server:build', function () { var build; let options = { env: { 'PATH': process.env.PATH, 'GOPATH': process.env.GOPATH } } if (argv.prod) { options.env['GOOS'] = 'linux' options.env['CGO_ENABLED'] = '0' console.log("Compiling go binarie to run inside Docker container") } var output = argv.prod ? conf.paths.build + '/prod/bin' : conf.paths.build + '/dev/bin'; build = child.spawnSync('go', ['build', '-o', output, "src/backend/main.go"], options); if (build.stderr.length) { var lines = build.stderr.toString() .split('\n').filter(function(line) { return line.length }); for (var l in lines) util.log(util.colors.red( 'Error (go install): ' + lines[l] )); notifier.notify({ title: 'Error (go install)', message: lines }); } return build; }); 
0
source

You can create a Docker container from the separate operating system that you need for your executable file and map the volume in the src directory. Launch the container and make the executable from the container. You get a binary code that can be run on a single OS.

0
source

All Articles