From 9f9e9be138911ce373afd48ba4faa41787489b71 Mon Sep 17 00:00:00 2001 From: Louis Parisot Date: Thu, 21 Nov 2019 11:47:26 +0100 Subject: [PATCH 1/4] Support volume mounts --- container.go | 1 + docker.go | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/container.go b/container.go index 15c8cadab0..e887816c72 100644 --- a/container.go +++ b/container.go @@ -67,6 +67,7 @@ type ContainerRequest struct { Cmd []string Labels map[string]string BindMounts map[string]string + VolumeMounts map[string]string RegistryCred string WaitingFor wait.Strategy Name string // for specifying container name diff --git a/docker.go b/docker.go index 1cc5890de8..81901ee4e9 100644 --- a/docker.go +++ b/docker.go @@ -424,18 +424,25 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque } // prepare mounts - bindMounts := []mount.Mount{} + mounts := []mount.Mount{} for hostPath, innerPath := range req.BindMounts { - bindMounts = append(bindMounts, mount.Mount{ + mounts = append(mounts, mount.Mount{ Type: mount.TypeBind, Source: hostPath, Target: innerPath, }) } + for volumeName, innerPath := range req.VolumeMounts { + mounts = append(mounts, mount.Mount{ + Type: mount.TypeVolume, + Source: volumeName, + Target: innerPath, + }) + } hostConfig := &container.HostConfig{ PortBindings: exposedPortMap, - Mounts: bindMounts, + Mounts: mounts, AutoRemove: true, Privileged: req.Privileged, } From d257f320fa22db62b9b0e6a93d797099550c2ba0 Mon Sep 17 00:00:00 2001 From: Louis Parisot Date: Thu, 21 Nov 2019 13:54:40 +0100 Subject: [PATCH 2/4] Fix code formatting --- container.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/container.go b/container.go index e887816c72..4555cc2508 100644 --- a/container.go +++ b/container.go @@ -67,7 +67,7 @@ type ContainerRequest struct { Cmd []string Labels map[string]string BindMounts map[string]string - VolumeMounts map[string]string + VolumeMounts map[string]string RegistryCred string WaitingFor wait.Strategy Name string // for specifying container name From fa8f044846cb1654898e1077bb5891f61540a1ed Mon Sep 17 00:00:00 2001 From: Louis Parisot Date: Thu, 21 Nov 2019 15:12:42 +0100 Subject: [PATCH 3/4] Support volume mounts - test case --- docker_test.go | 53 ++++++++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 4 ++++ testresources/hello.sh | 3 +++ 4 files changed, 61 insertions(+) create mode 100644 testresources/hello.sh diff --git a/docker_test.go b/docker_test.go index af601fd7b1..9040551281 100644 --- a/docker_test.go +++ b/docker_test.go @@ -886,3 +886,56 @@ func ExampleContainer_MappedPort() { port, _ := nginxC.MappedPort(ctx, "80") http.Get(fmt.Sprintf("http://%s:%s", ip, port.Port())) } + +func TestContainerCreationWithBindAndVolume(t *testing.T) { + absPath, err := filepath.Abs("./testresources/hello.sh") + if err != nil { + t.Fatal(err) + } + ctx, cnl := context.WithTimeout(context.Background(), 30*time.Second) + defer cnl() + // Create a Docker client. + dockerCli, err := client.NewClientWithOpts(client.FromEnv) + if err != nil { + t.Fatal(err) + } + dockerCli.NegotiateAPIVersion(ctx) + // Create the volume. + vol, err := dockerCli.VolumeCreate(ctx, volume.VolumeCreateBody{ + Driver: "local", + }) + if err != nil { + t.Fatal(err) + } + volumeName := vol.Name + defer func() { + ctx, cnl := context.WithTimeout(context.Background(), 5*time.Second) + defer cnl() + err := dockerCli.VolumeRemove(ctx, volumeName, true) + if err != nil { + t.Fatal(err) + } + }() + // Create the container that writes into the mounted volume. + bashC, err := GenericContainer(ctx, GenericContainerRequest{ + ContainerRequest: ContainerRequest{ + Image: "bash", + BindMounts: map[string]string{absPath: "/hello.sh"}, + VolumeMounts: map[string]string{volumeName: "/data"}, + Cmd: []string{"bash", "/hello.sh"}, + WaitingFor: wait.ForLog("done"), + }, + Started: true, + }) + if err != nil { + t.Fatal(err) + } + defer func() { + ctx, cnl := context.WithTimeout(context.Background(), 5*time.Second) + defer cnl() + err := bashC.Terminate(ctx) + if err != nil { + t.Fatal(err) + } + }() +} diff --git a/go.mod b/go.mod index e80ce251e5..8a3fc75cc8 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ replace github.com/docker/docker => github.com/docker/engine v0.0.0-201907171610 require ( github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect github.com/Microsoft/go-winio v0.4.11 // indirect + github.com/Microsoft/hcsshim v0.8.6 // indirect github.com/cenkalti/backoff v2.2.1+incompatible github.com/containerd/continuity v0.0.0-20190426062206-aaeac12a7ffc // indirect github.com/docker/distribution v2.7.1-0.20190205005809-0d3efadf0154+incompatible // indirect diff --git a/go.sum b/go.sum index 9a5fabe51f..bd4703e0b8 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,9 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8= +github.com/Microsoft/go-winio v0.4.11 h1:zoIOcVf0xPN1tnMVbTtEdI+P8OofVk3NObnwOQ6nK2Q= github.com/Microsoft/go-winio v0.4.11/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA= +github.com/Microsoft/hcsshim v0.8.6 h1:ZfF0+zZeYdzMIVMZHKtDKJvLHj76XCuVae/jNkjj0IA= +github.com/Microsoft/hcsshim v0.8.6/go.mod h1:Op3hHsoHPAvb6lceZHDtd9OkTew38wNoXnJs8iY7rUg= github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= @@ -39,6 +42,7 @@ github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2z github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= diff --git a/testresources/hello.sh b/testresources/hello.sh new file mode 100644 index 0000000000..cc6381d33a --- /dev/null +++ b/testresources/hello.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +echo "hello world" > /data/hello.txt +echo "done" From 166ffaf9fcc35bd57d7d6f28f2f7f5e8c0b7dee6 Mon Sep 17 00:00:00 2001 From: Louis Parisot Date: Thu, 21 Nov 2019 15:16:53 +0100 Subject: [PATCH 4/4] Support volume mounts - missing imports --- docker_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docker_test.go b/docker_test.go index 9040551281..cbd58dad46 100644 --- a/docker_test.go +++ b/docker_test.go @@ -3,7 +3,9 @@ package testcontainers import ( "context" "fmt" + "github.com/docker/docker/api/types/volume" "net/http" + "path/filepath" "testing" "time"