diff --git a/dockerclient.go b/dockerclient.go index 51a00b2..2b3a6ee 100644 --- a/dockerclient.go +++ b/dockerclient.go @@ -10,6 +10,7 @@ import ( "io/ioutil" "net/http" "net/url" + "path/filepath" "strconv" "strings" "sync/atomic" @@ -251,6 +252,34 @@ func (client *DockerClient) ContainerLogs(id string, options *LogOptions) (io.Re return resp.Body, nil } +func (client *DockerClient) CopyToContainer(id, destPath string, rdr io.Reader) error { + p := filepath.ToSlash(destPath) + + uri := fmt.Sprintf("/%s/containers/%s/archive?path=%s", APIVersion, id, p) + + req, err := http.NewRequest("PUT", client.URL.String()+uri, rdr) + if err != nil { + return err + } + + req.Header.Add("Content-Type", "application/x-tar") + resp, err := client.HTTPClient.Do(req) + if err != nil { + return err + } + + if resp.StatusCode > 200 { + defer resp.Body.Close() + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + return Error{StatusCode: resp.StatusCode, Status: resp.Status, msg: string(data)} + } + + return nil +} + func (client *DockerClient) ContainerChanges(id string) ([]*ContainerChanges, error) { uri := fmt.Sprintf("/%s/containers/%s/changes", APIVersion, id) data, err := client.doRequest("GET", uri, nil, nil) diff --git a/interface.go b/interface.go index b373055..6650930 100644 --- a/interface.go +++ b/interface.go @@ -27,6 +27,7 @@ type Client interface { ExecResize(id string, width, height int) error StartContainer(id string, config *HostConfig) error AttachContainer(id string, options *AttachOptions) (io.ReadCloser, error) + CopyToContainer(id, destPath string, rdr io.Reader) error StopContainer(id string, timeout int) error RestartContainer(id string, timeout int) error KillContainer(id, signal string) error diff --git a/mockclient/mock.go b/mockclient/mock.go index 9da36b8..1b2e719 100644 --- a/mockclient/mock.go +++ b/mockclient/mock.go @@ -60,6 +60,11 @@ func (client *MockClient) AttachContainer(id string, options *dockerclient.Attac return args.Get(0).(io.ReadCloser), args.Error(1) } +func (client *MockClient) CopyToContainer(id string, destPath string, rdr io.Reader) error { + args := client.Mock.Called(id, destPath, rdr) + return args.Error(0) +} + func (client *MockClient) StartContainer(id string, config *dockerclient.HostConfig) error { args := client.Mock.Called(id, config) return args.Error(0)