From a16e5ce886d4de492bf9055cd4eb7af931d32f8b Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 22 Jan 2023 10:47:22 +0100 Subject: [PATCH] Use class specific buildx instances So we don't have to muck with the machine default, and can swap between configurations without tearing down the old builder. --- README.md | 4 ---- lib/mrsk/commands/builder/multiarch.rb | 5 +++-- lib/mrsk/commands/builder/multiarch/remote.rb | 15 +++++++++------ lib/mrsk/commands/builder/native/remote.rb | 6 +++--- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 2de6817d..a0162f1f 100644 --- a/README.md +++ b/README.md @@ -163,8 +163,6 @@ builder: Note: You must have Docker running on the remote host being used as a builder. -With that configuration in place, you can setup the local/remote configuration using `mrsk build create`. If you wish to remove the contexts and buildx instances again, you can run `mrsk build remove`. If you had already built using the standard emulation setup, run `mrsk build remove` before doing `mrsk build remote`. - ### Configuring remote builder for single-arch If you're developing on ARM64 (like Apple Silicon), want to deploy on AMD64 (x86 64-bit), but don't need to run the image locally (or on other ARM64 hosts), you can configure a remote builder that just targets AMD64. This is a bit faster than building with multi-arch, as there's nothing to build locally. @@ -178,8 +176,6 @@ builder: Note: You must have Docker running on the remote host being used as a builder. -With that configuration in place, you can setup the remote configuration using `mrsk build create`. If you wish to remove the contexts and buildx instances again, you can run `mrsk build remove`. If you had already built using the standard emulation setup, run `mrsk build remove` before doing `mrsk build remote`. - ### Configuring native builder when multi-arch isn't needed If you're developing on the same architecture as the one you're deploying on, you can speed up the build a lot by forgoing a multi-arch image. This can be done by configuring the builder like so: diff --git a/lib/mrsk/commands/builder/multiarch.rb b/lib/mrsk/commands/builder/multiarch.rb index 82ef4ee5..bc87a86f 100644 --- a/lib/mrsk/commands/builder/multiarch.rb +++ b/lib/mrsk/commands/builder/multiarch.rb @@ -12,7 +12,8 @@ class Mrsk::Commands::Builder::Multiarch < Mrsk::Commands::Builder::Base def push docker :buildx, :build, "--push", - "--platform linux/amd64,linux/arm64", + "--platform", "linux/amd64,linux/arm64", + "--builder", builder_name, "-t", config.absolute_image, *build_args, *build_secrets, @@ -27,6 +28,6 @@ class Mrsk::Commands::Builder::Multiarch < Mrsk::Commands::Builder::Base private def builder_name - "mrsk-#{config.service}" + "mrsk-#{config.service}-multiarch" end end diff --git a/lib/mrsk/commands/builder/multiarch/remote.rb b/lib/mrsk/commands/builder/multiarch/remote.rb index 2bbd3c3a..b49de328 100644 --- a/lib/mrsk/commands/builder/multiarch/remote.rb +++ b/lib/mrsk/commands/builder/multiarch/remote.rb @@ -15,8 +15,16 @@ class Mrsk::Commands::Builder::Multiarch::Remote < Mrsk::Commands::Builder::Mult end private + def builder_name + super + "-remote" + end + + def builder_name_with_arch(arch) + "#{builder_name}-#{arch}" + end + def create_local_buildx - docker :buildx, :create, "--use", "--name", builder_name, builder_name_with_arch(local["arch"]), "--platform", "linux/#{local["arch"]}" + docker :buildx, :create, "--name", builder_name, builder_name_with_arch(local["arch"]), "--platform", "linux/#{local["arch"]}" end def append_remote_buildx @@ -50,9 +58,4 @@ class Mrsk::Commands::Builder::Multiarch::Remote < Mrsk::Commands::Builder::Mult def remote config.builder["remote"] end - - private - def builder_name_with_arch(arch) - "#{builder_name}-#{arch}" - end end diff --git a/lib/mrsk/commands/builder/native/remote.rb b/lib/mrsk/commands/builder/native/remote.rb index c7cecf89..6d2aeeea 100644 --- a/lib/mrsk/commands/builder/native/remote.rb +++ b/lib/mrsk/commands/builder/native/remote.rb @@ -17,6 +17,7 @@ class Mrsk::Commands::Builder::Native::Remote < Mrsk::Commands::Builder::Native docker :buildx, :build, "--push", "--platform", platform, + "--builder", builder_name, "-t", config.absolute_image, *build_args, *build_secrets, @@ -40,7 +41,7 @@ class Mrsk::Commands::Builder::Native::Remote < Mrsk::Commands::Builder::Native end def builder_name - "mrsk-#{config.service}" + "mrsk-#{config.service}-native-remote" end def builder_name_with_arch @@ -61,8 +62,7 @@ class Mrsk::Commands::Builder::Native::Remote < Mrsk::Commands::Builder::Native end def create_buildx - docker :buildx, :create, - "--use", "--name", builder_name, builder_name_with_arch, "--platform", platform + docker :buildx, :create, "--name", builder_name, builder_name_with_arch, "--platform", platform end def remove_buildx