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 RUN plenv/rbenv in Dockerfile?
  • Ansible how to push local image to private registry
  • Errors with alpine linux 3.5 and ruby 2.0?
  • Runtime constraints on CPU and memory with docker containers
  • Using docker-compose to start web and backend. How can I get the IP of backend from web?
  • Trying to install Mezzanine via 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?

  • MongoDB in Docker container runs out of space for journal
  • How to run tests on CentOS 7 with Travis-CI?
  • Docker warning on cgroup swap limit, memory.use_hierarchy
  • Docker for Windows error: “Hardware assisted virtualization and data execution protection must be enabled in the BIOS”
  • How to copy and rename a Docker container?
  • stdout being buffered in 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.