Golang Mac OSX build for Docker machine

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.

  • Docker compose wants engine > 1.10.0 but it already has engine 1.12.3
  • Deploy a .war into a Docker image by fabric8io docker-maven-plugin
  • Selecting different code branches when using a shared base image in Docker
  • Setting build args for dockerfile agent using a Jenkins declarative pipeline
  • docker nginx stream balancer 404
  • Can close properly a websocket with docker
  • I see two ways here:

    1. cross-compile binaries on Mac for linux OS
    2. 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?

  • Firefox hangs after opening Karma Server URL in Docker
  • Docker build process for javascript frontends with tons of dependencies
  • No access to a linked container with docker run (mapping in /etc/hosts not exist)
  • What is the difference between exposing docker port and creating bridge(or overlay)?
  • Cannot ping server after docker run
  • How do I get my dreamweaver site to use my nginx docker container as a web test server?
  • 3 Solutions collect form web for “Golang Mac OSX build for Docker machine”

    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!

    I use the first approach. Here its a gulp task the build go code. If the production flag is set, it runs GOOS=linux CGO_ENABLED=0 go build instead go build. So the binary will work inside a 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;
    });
    

    You could create a Docker container from the distinct OS you need for your executable, and map a volume to your src directory. Run the container and make the executable from within the container. You end up with a binary that you can run on the distinct OS.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.