我需要获取处于非运行状态的容器的退出代码。 我知道容器没有运行,我从不同的来源获得这些信息。
Docker的go SDK有没有办法获取退出码,而不必等待容器处于某种状态? 比如
ContainerWait
的WaitResponse
提供?
在我已经没有容器的状态下简单地调用
ContainerWait
是否是一个好的解决方案?或者有更好的解决方案吗?
我特别想避免
ContainerWait
因为我可以看到这个电话非常昂贵。
如果容器处于停止状态,则每个容器调用 const 约 10 毫秒,如果容器处于重新启动状态,则调用 const 在 20 到 50 毫秒之间。
ContainerState
结构中。这嵌入在来自 State
的响应中的
(*Client).ContainerInspect()
字段中。
例如:
func checkExitStatus(ctx context.Context, client *client.Client, containerID string) error {
inspection, err := client.ContainerInspect(ctx, containerID)
if err != nil {
return err
}
// Possible values are listed in the `ContainerState` docs; there do not
// seem to be named constants for these values.
if inspection.State.Status != "exited" {
return errors.New("container is not exited")
}
if inspection.State.ExitCode != 0 {
return fmt.Errorf("container exited with status %d", inspection.State.ExitCode)
}
return nil
}