From 40f51f9ee3d8bcd150a6e9e99425aa78bb3b2f88 Mon Sep 17 00:00:00 2001 From: Michael Bianco Date: Tue, 7 Jan 2025 07:59:31 -0700 Subject: [PATCH] feat: add debug option to Docker build commands This would enable an interative shell to be opened up if a build fails. This is still experimental and I'm running into some issues using this with orbstack, so this PR is still very much a WIP. https://github.com/orbstack/orbstack/issues/1700 Without this option, the only way to debug a failing build is to `--out` the build, insert multi-stage build commands in the dockerfile and then use `--target` to get in-progress container ID to play around with, which is super lame. We need a better way. --- src/main.rs | 4 ++++ src/nixpacks/builder/docker/docker_image_builder.rs | 11 +++++++++++ src/nixpacks/builder/docker/mod.rs | 1 + 3 files changed, 16 insertions(+) diff --git a/src/main.rs b/src/main.rs index b66dc2750..90749188f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -163,6 +163,10 @@ enum Commands { #[arg(long)] docker_cert_path: Option, + /// Open an interactive shell if the build fails + #[arg(long)] + debug: bool, + /// Enable writing cache metadata into the output image #[arg(long)] inline_cache: bool, diff --git a/src/nixpacks/builder/docker/docker_image_builder.rs b/src/nixpacks/builder/docker/docker_image_builder.rs index 39cf8bed1..00f1c9229 100644 --- a/src/nixpacks/builder/docker/docker_image_builder.rs +++ b/src/nixpacks/builder/docker/docker_image_builder.rs @@ -36,11 +36,14 @@ fn get_output_dir(app_src: &str, options: &DockerBuilderOptions) -> Result String { let args = command .get_args() .map(|arg| arg.to_string_lossy()) .collect::>(); + format!( "{} {}", command.get_program().to_string_lossy(), @@ -145,8 +148,16 @@ impl DockerImageBuilder { // Enable BuildKit for all builds docker_build_cmd.env("DOCKER_BUILDKIT", "1"); + + if self.options.debug { + docker_build_cmd.env("BUILDX_EXPERIMENTAL", "1"); + } docker_build_cmd + .arg("buildx") + .arg("debug") + .arg("--invoke") + .arg("bash") .arg("build") .arg(&output.root) .arg("-f") diff --git a/src/nixpacks/builder/docker/mod.rs b/src/nixpacks/builder/docker/mod.rs index f12d05be8..7357e4c93 100644 --- a/src/nixpacks/builder/docker/mod.rs +++ b/src/nixpacks/builder/docker/mod.rs @@ -21,6 +21,7 @@ pub struct DockerBuilderOptions { pub cpu_quota: Option, pub memory: Option, pub verbose: bool, + pub debug: bool, pub docker_host: Option, pub docker_tls_verify: Option, pub docker_output: Option,