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; });
CESCO source share