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

使用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条回答 默认 最新

  • duanguangwang5829 2017-11-21 16:57
    已采纳

    First, the Binds parameter is of the format "source:target[:ro]", so you should have it "/etc/secret.txt:/etc/secret.txt", or "/etc/secret.txt:/etc/secret.txt:ro" if you want it to be read-only.

    Second, the format of the data from the reader returned by CopyFromContainer() is a tar archive. Here is a small modification to your code that fixes Binds, and uses tar from the stdlib to extract the bytes from /etc/secret.

    func main() {
            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{"sleep", "1h"},
            }
    
            // mounted file
            h := container.HostConfig{
                    Binds: []string{"/etc/secret.txt:/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())
            }
    
            tr := tar.NewReader(rc)
            var b []byte
            for {
                    hdr, err := tr.Next()
                    if err == io.EOF {
                            break
                    }
                    if err != nil {
                            break
                    }
                    if hdr.Name == "secret.txt" {
                            b, err = ioutil.ReadAll(tr)
                            break
                    }
                    fmt.Println("Name:", hdr.Name)
                    if err != nil {
                            break
                    }
            }
            if err != nil {
                    fmt.Println(err.Error())
            }
            rc.Close()
            fmt.Printf("%q (len=%d)
    ", b, len(string(b)))
    }
    
    点赞 评论

相关推荐 更多相似问题