gradle-docker icon indicating copy to clipboard operation
gradle-docker copied to clipboard

Is it possible to use the plugin with buildx?

Open lamba92 opened this issue 4 years ago • 3 comments

I'd be in need to automate a multiarch build similar to this example. Is there any task available?

lamba92 avatar Apr 13 '20 10:04 lamba92

Same here, this is becoming more important to use buildx as support for multi-architecture builds and pushing list (fat) manifests are increasingly being done.

ArvinB avatar Nov 11 '20 15:11 ArvinB

    project.tasks.docker.with {
        commandLine getDockerBuildCmd()
        doFirst { logger.lifecycle("${commandLine.join(' ')}") }
    }
def getDockerBuildCmd() {
    def buildCommandLine = ['docker', 'buildx', 'build', '--load', '--platform', 'linux/amd64']
    if (docker.noCache) {
        buildCommandLine.add '--no-cache'
    }
    if (docker.network != null) {
        buildCommandLine.addAll('--network', docker.network)
    }
    if (!docker.buildArgs.isEmpty()) {
        for (Map.Entry<String, String> buildArg : docker.buildArgs.entrySet()) {
            buildCommandLine.addAll('--build-arg', "${buildArg.getKey()}=${buildArg.getValue()}")
        }
    }
    if (!docker.labels.isEmpty()) {
        for (Map.Entry<String, String> label : docker.labels.entrySet()) {
            if (!label.getKey().matches(LABEL_KEY_PATTERN)) {
                throw new GradleException(String.format("Docker label '%s' contains illegal characters. " +
                        "Label keys must only contain lowercase alphanumberic, `.`, or `-` characters (must match %s).",
                        label.getKey(), LABEL_KEY_PATTERN.pattern()))
            }
            buildCommandLine.addAll('--label', "${label.getKey()}=${label.getValue()}")
        }
    }
    if (docker.pull) {
        buildCommandLine.add '--pull'
    }
    buildCommandLine.addAll('-t', docker.name, '.')
    return buildCommandLine
}

yuanjinyong avatar Jan 18 '22 15:01 yuanjinyong

This is great to see. I couldn't wait, so I ended up creating my own Gradle plugin with a focus on using docker buildx.

There were many many lessons learned along the way. Just a couple gotchas:

  • On a RHEL VM, often the buildkit needs to be reset otherwise you loose multi-platform capabilities. Especially when the VM reboots.
docker run --rm --privileged multiarch/qemu-user-static --reset --persistent yes
  • When you tag multi-platform images (which you should), then you have to revert to old-school for creating the list manifest. After the images are pushed to a registry of course.
docker manifest create --amend <your tagged image>
docker manifest push --purge <your tagged list manifest>

ArvinB avatar Jan 18 '22 17:01 ArvinB