fixes #2671, add support for bind mounting individual files in to containers, rebases of #1757 #2301

This commit is contained in:
daniel-garcia 2013-11-19 17:27:01 -06:00 коммит произвёл Daniel Garcia
Родитель a93e40a158
Коммит 0198f8a879
2 изменённых файлов: 42 добавлений и 2 удалений

Просмотреть файл

@ -610,6 +610,7 @@ func (container *Container) Start() (err error) {
// Create the requested volumes if they don't exist // Create the requested volumes if they don't exist
for volPath := range container.Config.Volumes { for volPath := range container.Config.Volumes {
volPath = path.Clean(volPath) volPath = path.Clean(volPath)
volIsDir := true
// Skip existing volumes // Skip existing volumes
if _, exists := container.Volumes[volPath]; exists { if _, exists := container.Volumes[volPath]; exists {
continue continue
@ -624,6 +625,16 @@ func (container *Container) Start() (err error) {
if strings.ToLower(bindMap.Mode) == "rw" { if strings.ToLower(bindMap.Mode) == "rw" {
srcRW = true srcRW = true
} }
if file, err := os.Open(bindMap.SrcPath); err != nil {
return err
} else {
defer file.Close()
if stat, err := file.Stat(); err != nil {
return err
} else {
volIsDir = stat.IsDir()
}
}
// Otherwise create an directory in $ROOT/volumes/ and use that // Otherwise create an directory in $ROOT/volumes/ and use that
} else { } else {
c, err := container.runtime.volumes.Create(nil, container, "", "", nil) c, err := container.runtime.volumes.Create(nil, container, "", "", nil)
@ -640,9 +651,31 @@ func (container *Container) Start() (err error) {
container.VolumesRW[volPath] = srcRW container.VolumesRW[volPath] = srcRW
// Create the mountpoint // Create the mountpoint
rootVolPath := path.Join(container.RootfsPath(), volPath) rootVolPath := path.Join(container.RootfsPath(), volPath)
if volIsDir {
if err := os.MkdirAll(rootVolPath, 0755); err != nil { if err := os.MkdirAll(rootVolPath, 0755); err != nil {
return err return err
} }
}
volPath = path.Join(container.RootfsPath(), volPath)
if _, err := os.Stat(volPath); err != nil {
if os.IsNotExist(err) {
if volIsDir {
if err := os.MkdirAll(volPath, 0755); err != nil {
return err
}
} else {
if err := os.MkdirAll(path.Dir(volPath), 0755); err != nil {
return err
}
if f, err := os.OpenFile(volPath, 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 {

Просмотреть файл

@ -1257,6 +1257,13 @@ func TestBindMounts(t *testing.T) {
if _, err := runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:.", tmpDir), "_", "ls", "."}, nil); err == nil { if _, err := runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:.", tmpDir), "_", "ls", "."}, nil); err == nil {
t.Fatal("Container bind mounted illegal directory") t.Fatal("Container bind mounted illegal directory")
} }
// test mount a file
runContainer(eng, r, []string{"-v", fmt.Sprintf("%s/holla:/tmp/holla:rw", tmpDir), "_", "sh", "-c", "echo -n 'yotta' > /tmp/holla"}, t)
content := readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
if content != "yotta" {
t.Fatal("Container failed to write to bind mount file")
}
} }
// Test that -volumes-from supports both read-only mounts // Test that -volumes-from supports both read-only mounts