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

Remove docker container on build failure #1531

Merged
merged 2 commits into from
Mar 8, 2024
Merged
Changes from 1 commit
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
31 changes: 24 additions & 7 deletions crates/build/src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ use bollard::{
CreateContainerOptions,
ListContainersOptions,
LogOutput,
RemoveContainerOptions,
},
errors::Error,
image::{
Expand Down Expand Up @@ -162,11 +163,27 @@ pub fn docker_build(args: ExecuteArgs) -> Result<BuildResult> {
)
.await?;

let mut build_result = run_build(&client, &container, &verbosity).await?;

update_build_result(&host_folder, &mut build_result)?;

update_metadata(&build_result, &verbosity, &image, &client).await?;
let build_result = async {
let mut build_result = run_build(&client, &container, &verbosity).await?;
update_build_result(&host_folder, &mut build_result)?;
update_metadata(&build_result, &verbosity, &image, &client).await?;
Ok::<BuildResult, anyhow::Error>(build_result)
}
.await;

let build_result = match build_result {
Ok(build_result) => build_result,
Err(e) => {
// Remove container to avoid leaving it in an incorrect state for
// subsequent calls
let options = Some(RemoveContainerOptions {
force: true,
..Default::default()
});
let _ = client.remove_container(&container, options).await;
return Err(e)
}
};

verbose_eprintln!(
verbosity,
Expand Down Expand Up @@ -305,7 +322,7 @@ async fn create_container(
let container_option = containers.first();

if container_option.is_some() {
return Ok(container_name);
return Ok(container_name)
}

let mount = Mount {
Expand Down Expand Up @@ -530,7 +547,7 @@ async fn show_pull_progress(
let status = info.status.unwrap_or_default();
if status.starts_with("Digest:") || status.starts_with("Status:") {
eprintln!("{}", status);
continue;
continue
}

if let Some(id) = info.id {
Expand Down
Loading