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.

  • How to make docker application use port 80 (http) instead of 443 (https)
  • Docker stats for docker containers in kubernetes returning empty values
  • Docker: Why port forwarding is not working with go tour?
  • No suitable driver found (jdbc:pgsql)
  • Get elastic beanstalk environment variables in docker container
  • Docker running inside vagrant + remote python debugging in Pycharm
  • 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?

  • Kubernetes UI: error in kubectl proxy
  • Record local audio in a docker container
  • docker restart container failed: “already in use”, but there's no more docker image
  • How to run sql script for postgres in docker
  • Guidance needed - how to track nginx-docker-rails performance issue
  • Cannot reduce ulimits inside docker container
  • 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:


    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)
                    'Error (go install): ' + lines[l]
                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.