This repository was archived by the owner on Sep 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
125 lines (105 loc) · 2.86 KB
/
main.go
File metadata and controls
125 lines (105 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"archive/tar"
"fmt"
"io"
"os"
"path/filepath"
"github.com/fsouza/go-dockerclient"
)
func mountedPath(path string) string {
return filepath.Join("/host", path)
}
func main() {
dockerCli, err := docker.NewClient("unix:///host/var/run/docker.sock")
if err != nil {
panic(err)
}
fileToDownload := "/etc/hosts"
tarStream := getTarStream(dockerCli, fileToDownload)
defer tarStream.Close()
fmt.Println("Downloading", fileToDownload, "as tar")
tarReader := tar.NewReader(tarStream)
for {
header, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
panic(err)
}
fileInfo := header.FileInfo()
// Should also check if file is a link in real life
if fileInfo.IsDir() {
fmt.Println("Got dir", header.Name)
continue
}
fmt.Println("Got file", header.Name, "with size", header.Size)
fmt.Println("=========Contents=========")
if _, err = io.CopyN(os.Stdout, tarReader, header.Size); err != nil {
panic(err)
}
fmt.Println("==========================")
}
fmt.Println("Download complete")
}
func getTarStream(cli *docker.Client, filename string) io.ReadCloser {
container := createContainer(cli, filename)
fmt.Println("Created container", container.ID)
defer removeContainer(cli, container)
startContainer(cli, container)
fmt.Println("Started container")
preader, pwriter := io.Pipe()
opts := docker.DownloadFromContainerOptions{
Path: mountedPath(filename),
OutputStream: pwriter,
}
// Let docker asynchronously write into the pipe while we are reading it on the other end
go func() {
defer pwriter.Close()
fmt.Println("Requesting file", opts.Path)
if err := cli.DownloadFromContainer(container.ID, opts); err != nil {
panic(err)
}
}()
return preader
}
func createContainer(cli *docker.Client, filename string) *docker.Container {
createOpts := docker.CreateContainerOptions{
Config: &docker.Config{
// any image that has the command specified below can be used
Image: "dockercp",
// "cat" with no arguments will simply block indefinitely ensuring that the container does not terminate.
Cmd: []string{"cat"},
Entrypoint: []string{},
OpenStdin: true,
},
HostConfig: &docker.HostConfig{
Binds: []string{fmt.Sprintf("%s:%s", filename, mountedPath(filename))},
},
}
container, err := cli.CreateContainer(createOpts)
if err != nil {
panic(err)
}
return container
}
func startContainer(cli *docker.Client, container *docker.Container) {
if err := cli.StartContainer(container.ID, nil); err != nil {
panic(err)
}
}
func removeContainer(cli *docker.Client, container *docker.Container) {
removeOpts := docker.RemoveContainerOptions{
ID: container.ID,
RemoveVolumes: true,
Force: true,
}
err := cli.RemoveContainer(removeOpts)
if err != nil {
if _, ok := err.(*docker.NoSuchContainer); ok {
return
}
panic(err)
}
}