Skip to content

Commit

Permalink
Log error when starting a container fails (#431)
Browse files Browse the repository at this point in the history
When container fails to start all you get is `failed to start container`
message, and it's not always easy to guess why it failed. This PR
includes command output in panic message.

Example:

`dev`:
> ```thread '<unnamed>' panicked at 'failed to start container'```

PR:
> ```thread '<unnamed>' panicked at 'failed to start container, output =
Output { status: ExitStatus(unix_wait_status(32000)), stdout:
"e5866b9407496bc9a88b63528370de91ec43358a6a40e1262fb3cae9dd3a8ec1\n",
stderr: "docker: Error response from daemon: driver failed programming
external connectivity on endpoint dazzling_elbakyan
(85503e92065801ce10c86a95ba9af00c53e0e9a3ec4eb0ad78e55e306fbf8434): Bind
for 0.0.0.0:13306 failed: port is already allocated.\n" }'```

Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
  • Loading branch information
stepantubanov and thomaseizinger authored Sep 26, 2023
1 parent cab1c82 commit 407d924
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion testcontainers/src/clients/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ impl Cli {
log::debug!("Executing command: {:?}", command);

let output = command.output().expect("Failed to execute docker command");
if !output.status.success() {
let stdout = std::str::from_utf8(&output.stdout).unwrap_or("{not utf8}");
let stderr = std::str::from_utf8(&output.stderr).unwrap_or("{not utf8}");
log::error!("Failed to start container.\nContainer stdout: {stdout}\nContainer stderr: {stderr}");
panic!("Failed to start container, check log for details")
}

assert!(output.status.success(), "failed to start container");
let container_id = String::from_utf8(output.stdout)
.expect("output is not valid utf8")
.trim()
Expand Down

0 comments on commit 407d924

Please sign in to comment.