mirror of https://github.com/docker/docs.git
Merge pull request #4752 from crosbymichael/fix-volumes-from-files
Allow volumes from to be individual files
This commit is contained in:
commit
b936f4f5e1
|
@ -253,3 +253,21 @@ func TestDockerRunWithoutNetworking(t *testing.T) {
|
||||||
logDone("run - disable networking with --networking=false")
|
logDone("run - disable networking with --networking=false")
|
||||||
logDone("run - disable networking with -n=false")
|
logDone("run - disable networking with -n=false")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Regression test for #4741
|
||||||
|
func TestDockerRunWithVolumesAsFiles(t *testing.T) {
|
||||||
|
runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", "/etc/hosts:/target-file", "busybox", "true")
|
||||||
|
out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
|
||||||
|
if err != nil && exitCode != 0 {
|
||||||
|
t.Fatal("1", out, stderr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
runCmd = exec.Command(dockerBinary, "run", "--volumes-from", "test-data", "busybox", "cat", "/target-file")
|
||||||
|
out, stderr, exitCode, err = runCommandWithStdoutStderr(runCmd)
|
||||||
|
if err != nil && exitCode != 0 {
|
||||||
|
t.Fatal("2", out, stderr, err)
|
||||||
|
}
|
||||||
|
deleteAllContainers()
|
||||||
|
|
||||||
|
logDone("run - regression test for #4741 - volumes from as files")
|
||||||
|
}
|
||||||
|
|
|
@ -88,7 +88,11 @@ func applyVolumesFrom(container *Container) error {
|
||||||
if _, exists := container.Volumes[volPath]; exists {
|
if _, exists := container.Volumes[volPath]; exists {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err := os.MkdirAll(filepath.Join(container.basefs, volPath), 0755); err != nil {
|
stat, err := os.Stat(filepath.Join(c.basefs, volPath))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := createIfNotExists(filepath.Join(container.basefs, volPath), stat.IsDir()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
container.Volumes[volPath] = id
|
container.Volumes[volPath] = id
|
||||||
|
@ -208,25 +212,9 @@ func createVolumes(container *Container) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if err := createIfNotExists(rootVolPath, volIsDir); err != nil {
|
||||||
if _, err := os.Stat(rootVolPath); err != nil {
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
if volIsDir {
|
|
||||||
if err := os.MkdirAll(rootVolPath, 0755); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
if err := os.MkdirAll(filepath.Dir(rootVolPath), 0755); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if f, err := os.OpenFile(rootVolPath, os.O_CREATE, 0755); err != nil {
|
|
||||||
return err
|
|
||||||
} else {
|
|
||||||
f.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do not copy or change permissions if we are mounting from the host
|
// Do not copy or change permissions if we are mounting from the host
|
||||||
if srcRW && !isBindMount {
|
if srcRW && !isBindMount {
|
||||||
|
@ -266,3 +254,25 @@ func createVolumes(container *Container) error {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func createIfNotExists(path string, isDir bool) error {
|
||||||
|
if _, err := os.Stat(path); err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
if isDir {
|
||||||
|
if err := os.MkdirAll(path, 0755); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
f, err := os.OpenFile(path, os.O_CREATE, 0755)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue