Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multi platform support to contrib.docker through docker buildx #3143

Merged
merged 7 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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