Skip to content

Commit

Permalink
Add multi platform support to contrib.docker through docker buildx (#…
Browse files Browse the repository at this point in the history
…3143)

A minor change to the contrib.docker plugin (and its docs).

Allows to build for other architectures - a use case that becomes more
and more useful with ARM architectures becoming more relevant.

Relevant docs:
[docker buildx (Buildkit)](https://docs.docker.com/reference/cli/docker/buildx/build/#platform)

Pull request: #3143
---------

Co-authored-by: Georg Ofenbeck - taaofge1 <georg.ofenbeck@swisscom.com>
  • Loading branch information
GeorgOfenbeck and Georg Ofenbeck - taaofge1 authored May 7, 2024
1 parent 867bb53 commit a6ea16b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
4 changes: 4 additions & 0 deletions contrib/docker/readme.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ object docker extends DockerConfig {
def user = "new-user"
// Optionally override the docker executable to use something else
def executable = "podman"
// If the executable is docker (which is also the default), you can optionally also pass a platform parameter
// docker buildx is then used to build multi-platform images
def platform = "linux/arm64"
}
----


Run mill in interactive mode to see the docker client output, like `mill -i foo.docker.build`.
31 changes: 27 additions & 4 deletions contrib/docker/src/mill/contrib/docker/DockerModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ trait DockerModule { outer: JavaModule =>
*/
def user: T[String] = ""

/**
* Optional platform parameter, if set uses buildkit to build for specified platform.
*
* See also the Docker docs on
* [[https://docs.docker.com/reference/cli/docker/buildx/build/#platform]]
* for more information.
*/
def platform: T[String] = ""

/**
* The name of the executable to use, the default is "docker".
*/
Expand Down Expand Up @@ -155,10 +164,24 @@ trait DockerModule { outer: JavaModule =>
val (pull, _) = pullAndHash()
val pullLatestBase = IterableShellable(if (pull) Some("--pull") else None)

val result = os
.proc(executable(), "build", tagArgs, pullLatestBase, dest)
.call(stdout = os.Inherit, stderr = os.Inherit)

val result = if (platform().isEmpty || executable() != "docker") {
if (platform().nonEmpty)
log.info("Platform parameter is ignored when using non-docker executable")
os.proc(executable(), "build", tagArgs, pullLatestBase, dest)
.call(stdout = os.Inherit, stderr = os.Inherit)
} else {
os.proc(
executable(),
"buildx",
"build",
tagArgs,
pullLatestBase,
"--platform",
platform(),
dest
)
.call(stdout = os.Inherit, stderr = os.Inherit)
}
log.info(s"Docker build completed ${if (result.exitCode == 0) "successfully"
else "unsuccessfully"} with ${result.exitCode}")
tags()
Expand Down

0 comments on commit a6ea16b

Please # to comment.