Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix copying up initial contents of the mount point directory
Signed-off-by: fahed dorgaa <fahed.dorgaa@gmail.com>
  • Loading branch information
fahedouch committed Jun 25, 2021
commit e9863554055f5b7a509f94256593fdf3f25ba0e7
19 changes: 8 additions & 11 deletions cmd/nerdctl/run_mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,15 @@ func generateMountOpts(clicontext *cli.Context, ctx context.Context, client *con
mounted[filepath.Clean(x.Mount.Destination)] = struct{}{}

//copying up initial contents of the mount point directory
for imgVolRaw := range imageVolumes {
imgVol := filepath.Clean(imgVolRaw)
target, err := securejoin.SecureJoin(tempDir, imgVol)
if err != nil {
return nil, nil, err
}
target, err := securejoin.SecureJoin(tempDir, x.Mount.Destination)
if err != nil {
return nil, nil, err
}

//Coyping content in AnonymousVolume and namedVolume
if x.Mount.Destination == imgVol && x.Type == "volume" {
if err := copyExistingContents(target, x.Mount.Source); err != nil {
return nil, nil, err
}
//Coyping content in AnonymousVolume and namedVolume
if x.Type == "volume" {
if err := copyExistingContents(target, x.Mount.Source); err != nil {
return nil, nil, err
}
}
if x.AnonymousVolume != "" {
Expand Down
27 changes: 27 additions & 0 deletions cmd/nerdctl/run_mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,33 @@ VOLUME /foo
}

func TestCopyingUpInitialContentsOnVolume(t *testing.T) {
t.Parallel()
testutil.RequiresBuild(t)
base := testutil.NewBase(t)
const imageName = "nerdctl-test-copying-on-volume"
defer base.Cmd("rmi", imageName).Run()
defer base.Cmd("volume", "rm", "copying-initial-content-on-volume").Run()

dockerfile := fmt.Sprintf(`FROM %s
RUN mkdir -p /mnt && echo hi > /mnt/initial_file
CMD ["cat", "/mnt/initial_file"]
`, testutil.AlpineImage)

buildCtx, err := createBuildContext(dockerfile)
assert.NilError(t, err)
defer os.RemoveAll(buildCtx)

base.Cmd("build", "-t", imageName, buildCtx).AssertOK()

//AnonymousVolume
base.Cmd("run", "--rm", imageName).AssertOutContains("hi")
base.Cmd("run", "-v", "/mnt", "--rm", imageName).AssertOutContains("hi")

//NamedVolume should be automatically created
base.Cmd("run", "-v", "copying-initial-content-on-volume:/mnt", "--rm", imageName).AssertOutContains("hi")
}

func TestCopyingUpInitialContentsOnDockerfileVolume(t *testing.T) {
t.Parallel()
testutil.RequiresBuild(t)
base := testutil.NewBase(t)
Expand Down
10 changes: 9 additions & 1 deletion pkg/mountutil/mountutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"path/filepath"
"strings"

"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/oci"
"github.com/containerd/containerd/sys"
"github.com/containerd/nerdctl/pkg/idgen"
Expand Down Expand Up @@ -67,7 +68,14 @@ func ProcessFlagV(s string, volStore volumestore.VolumeStore) (*Processed, error
// assume src is a volume name
vol, err := volStore.Get(src)
if err != nil {
return nil, err
if errors.Is(err, errdefs.ErrNotFound) {
vol, err = volStore.Create(src)
if err != nil {
return nil, err
}
} else {
return nil, err
}
}
// src is now full path
src = vol.Mountpoint
Expand Down