From a57900e35f2c30026a070fdfdbdb0ce99b35e1ff Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Tue, 18 Mar 2014 15:28:40 -0700 Subject: [PATCH] Allow volumes from to be individual files Fixes #4741 Right now volumes from expected a dir and not a file so when the drivers tried to do the bind mount, the destination was a dir, not a file so it fails to run. Docker-DCO-1.1-Signed-off-by: Michael Crosby (github: crosbymichael) --- runtime/volumes.go | 48 ++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/runtime/volumes.go b/runtime/volumes.go index 1bbb14a369..5ac82ef089 100644 --- a/runtime/volumes.go +++ b/runtime/volumes.go @@ -88,7 +88,11 @@ func applyVolumesFrom(container *Container) error { if _, exists := container.Volumes[volPath]; exists { 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 } container.Volumes[volPath] = id @@ -208,24 +212,8 @@ func createVolumes(container *Container) error { if err != nil { return err } - - if _, err := os.Stat(rootVolPath); err != nil { - if os.IsNotExist(err) { - if volIsDir { - if err := os.MkdirAll(rootVolPath, 0755); err != nil { - 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() - } - } - } + if err := createIfNotExists(rootVolPath, volIsDir); err != nil { + return err } // Do not copy or change permissions if we are mounting from the host @@ -266,3 +254,25 @@ func createVolumes(container *Container) error { } 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 +}