Skip to content

Commit

Permalink
Merge pull request #116 from SkySoft-ATM/feature/volume-mount
Browse files Browse the repository at this point in the history
Support volume mounts
  • Loading branch information
gianarb authored Dec 20, 2019
2 parents 8830459 + 166ffaf commit 0c6fcb4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
1 change: 1 addition & 0 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
55 changes: 55 additions & 0 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package testcontainers
import (
"context"
"fmt"
"github.com/docker/docker/api/types/volume"
"net/http"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -886,3 +888,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)
}
}()
}
3 changes: 3 additions & 0 deletions testresources/hello.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
echo "hello world" > /data/hello.txt
echo "done"

0 comments on commit 0c6fcb4

Please sign in to comment.