Visitar URL original
file content from CopyFromContainer · Issue #308 · docker-archive-public/docker.engine-api · GitHub
Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

file content from CopyFromContainer #308

Open

Description

@SDkie

If I write the content to a file its adding some header and footer to the file.
What is the issue?

Sample code

reader, _, err = CopyFromContainer(context.TODO(), containerId, path)
f, err = os.Create("tempFile")
n, err = io.Copy(f, reader)

Activity

  1. prayas-stha commented on Feb 28, 2017

    @prayas-stha

    It is because its the tar file in the reader.

    import("archive/tar")
    ....
    reader, _, err = CopyFromContainer(context.TODO(), containerId, path)
    if err != nil{
                    log.Println(err.Error())
    }
    tr := tar.NewReader(reader)
    for {
                    // hdr gives you the header of the tar file
                    hdr, err := tr.Next()
                    if err == io.EOF {
                            // end of tar archive
                            break
                    }
                    if err != nil {
                            log.Fatalln(err)
                    }
                    buf := new(bytes.Buffer)
                    buf.ReadFrom(tr)
                    
                    // You can use this wholeContent to create new file
                    wholeContent := buf.String()
                    
                    fmt.Println("Whole of the string of ", hdr.Name ," is ",wholeContent)
                    
    }
    

    This gives you the content, you can use to create files on your own. Look into https://golang.org/pkg/archive/tar/#pkg-examples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions