dsqbh42082 2018-08-14 21:23
浏览 94
已采纳

以编程方式检查Docker容器进程是否以非零状态结束

I'm working on a Go application which starts some Docker containers using Go Docker SDK. I need to check if containers' processes exit with zero (success) status code.

Here's the minimal working example:

package main

import (
    "context"
    "io"
    "log"
    "os"

    "github.com/docker/docker/api/types"
    "github.com/docker/docker/api/types/container"
    "github.com/docker/docker/client"
)

func main() {
    ctx := context.Background()

    cli, err := client.NewEnvClient()
    if err != nil {
        log.Fatal(err)
    }

    reader, err := cli.ImagePull(
        ctx,
        "docker.io/library/alpine",
        types.ImagePullOptions{},
    )
    if err != nil {
        log.Fatal(err)
    }
    io.Copy(os.Stdout, reader)

    resp, err := cli.ContainerCreate(ctx, &container.Config{
        Image: "alpine",
        Cmd:   []string{"sh", "-c", "echo hello world; return 1"},
        Tty:   true,
    }, nil, nil, "")
    if err != nil {
        log.Fatal(err)
    }

    err = cli.ContainerStart(
        ctx,
        resp.ID,
        types.ContainerStartOptions{},
    )
    if err != nil {
        log.Fatal(err)
    }

    statusCh, errCh := cli.ContainerWait(
        ctx,
        resp.ID,
        container.WaitConditionNotRunning,
    )
    select {
    case err := <-errCh:
        if err != nil {
            log.Fatal(err)
        }
    case <-statusCh:
    }

    out, err := cli.ContainerLogs(
        ctx,
        resp.ID,
        types.ContainerLogsOptions{ShowStdout: true},
    )
    if err != nil {
        log.Fatal(err)
    }

    io.Copy(os.Stdout, out)
}

As you can see, the process in the container ends with non-zero status (sh -c "echo hello world; return 1"). However, it doesn't log any fatal errors and simply displays hello world when built and executed:

{"status":"Pulling from library/alpine","id":"latest"}
{"status":"Digest: sha256:7043076348bf5040220df6ad703798fd8593a0918d06d3ce30c6c93be117e430"}
{"status":"Status: Image is up to date for alpine:latest"}
hello world

How can I check that container process exited with non-zero status using Docker Go SDK?

  • 写回答

1条回答 默认 最新

  • dtwncxs3547 2018-08-14 21:48
    关注

    I think you should use the status channel to get the exit code. The error channel seems to be used to signal if there was an error while talking to the docker daemon, see https://godoc.org/github.com/docker/docker/client#Client.ContainerWait.

    This works for me:

    select {
    case err := <-errCh:
        if err != nil {
            log.Fatal(err)
        }
    case status := <-statusCh:
        log.Printf("status.StatusCode: %#+v
    ", status.StatusCode)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料