palantir / gradle-docker

a Gradle plugin for orchestrating docker builds and pushes.
Apache License 2.0
747 stars 158 forks source link

Is it possible to use the plugin with buildx? #353

Open lamba92 opened 4 years ago

lamba92 commented 4 years ago

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

ArvinB commented 3 years ago

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.

yuanjinyong commented 2 years ago
    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
}
ArvinB commented 2 years ago

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:

docker run --rm --privileged multiarch/qemu-user-static --reset --persistent yes
docker manifest create --amend <your tagged image>
docker manifest push --purge <your tagged list manifest>