本文整理匯總了Golang中github.com/docker/engine-api/client.Client.CopyFromContainer方法的典型用法代碼示例。如果您正苦於以下問題:Golang Client.CopyFromContainer方法的具體用法?Golang Client.CopyFromContainer怎麽用?Golang Client.CopyFromContainer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/docker/engine-api/client.Client
的用法示例。
在下文中一共展示了Client.CopyFromContainer方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: GetOnionHostname
func GetOnionHostname(cli *client.Client, containerID string) (string, error) {
content, stat, err := cli.CopyFromContainer(containerID, HostnamePath)
// XXX: This isn't very pretty. But we need to wait until Tor generates
// an .onion address, and there's not really any better way of
// doing it.
for err != nil && strings.Contains(err.Error(), "no such file or directory") {
// Make sure the container hasn't died.
if inspect, err := cli.ContainerInspect(containerID); err != nil {
return "", fmt.Errorf("error inspecting container: %s", err)
} else if !isRunning(inspect.State) {
return "", fmt.Errorf("container died before the hostname was computed")
}
log.Warnf("tor onion hostname not found in container, retrying after a short nap...")
time.Sleep(500 * time.Millisecond)
content, stat, err = cli.CopyFromContainer(containerID, HostnamePath)
}
if err != nil {
return "", err
}
defer content.Close()
if stat.Mode.IsDir() {
return "", fmt.Errorf("hostname file is a directory")
}
tr := tar.NewReader(content)
hdr, err := tr.Next()
for err != io.EOF {
if err != nil {
break
}
// XXX: Maybe do filepath.Base()?
if hdr.Name != "hostname" {
continue
}
data, err := ioutil.ReadAll(tr)
if err != nil {
return "", err
}
hostname := string(data)
return strings.TrimSpace(hostname), nil
}
return "", fmt.Errorf("hostname file not in copied archive")
}