douqiao5440
2017-11-20 21:14
浏览 91

使用Go Docker API从装入的文件中获取文件上下文

I'm trying to get mounted file content using Go docker API:

File secret.txt stores a line TOKEN=MY_TOKEN

Code:

cli, err := client.NewEnvClient()
if err != nil {
    panic(err)
}
defer cli.Close()

ctx := context.Background()

_, err = cli.ImagePull(ctx, "alpine", types.ImagePullOptions{})
if err != nil {
    panic(err)

}

containerConfig := &container.Config{
    Image: "alpine",
    Cmd:   []string{"echo", "hello world"},
}

// mounted file
h := container.HostConfig{
    Binds: []string{"/etc/secret.txt"},
}

resp, err := cli.ContainerCreate(ctx, containerConfig, &h, nil, "")
if err != nil {
    panic(err)
}

rc, _, err := cli.CopyFromContainer(context.Background(), resp.ID, "/etc/secret.txt")
if err != nil {
    fmt.Println(err.Error())
}
b, err := ioutil.ReadAll(rc)
if err != nil {
    fmt.Println(err.Error())
}
rc.Close()
fmt.Println(string(b), "  len=", len(string(b)))

I'm getting secret.txt file with additional information:

secret.txt/0040755000000000000000000000000013204637420011354 5ustar0000000000000000 len= 153

How to get actual data from text file? Thank you

1条回答 默认 最新

相关推荐 更多相似问题