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.

  • Connecting to percona docker from a java docker container
  • Import-Module posh-docker is not working
  • Can I push application updates to docker image without rebuilding?
  • Docker Hub Private Repo Login. More secure way?
  • Docker: disable pulling from remote registry
  • Enter into docker container after shell script execution is complete
  • 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?

  • NFS Volumes in Docker/Docker-Compose
  • Docker container can't resolve DNS to reach another AWS Ec2 Machine
  • 404 on Nginx running on docker (uwsgi)
  • Can docker use the existing host system to run a command in isolation like lxc-execute
  • Not able to push docker images to docker private registry
  • Docker/virtualisation and HDFS
  • 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.