mounts.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. mounttypes "github.com/docker/docker/api/types/mount"
  7. "github.com/docker/docker/container"
  8. volumesservice "github.com/docker/docker/volume/service"
  9. )
  10. func (daemon *Daemon) prepareMountPoints(container *container.Container) error {
  11. for _, config := range container.MountPoints {
  12. if err := daemon.lazyInitializeVolume(container.ID, config); err != nil {
  13. return err
  14. }
  15. }
  16. return nil
  17. }
  18. func (daemon *Daemon) removeMountPoints(container *container.Container, rm bool) error {
  19. var rmErrors []string
  20. ctx := context.TODO()
  21. for _, m := range container.MountPoints {
  22. if m.Type != mounttypes.TypeVolume || m.Volume == nil {
  23. continue
  24. }
  25. daemon.volumes.Release(ctx, m.Volume.Name(), container.ID)
  26. if !rm {
  27. continue
  28. }
  29. // Do not remove named mountpoints
  30. // these are mountpoints specified like `docker run -v <name>:/foo`
  31. if m.Spec.Source != "" {
  32. continue
  33. }
  34. err := daemon.volumes.Remove(ctx, m.Volume.Name())
  35. // Ignore volume in use errors because having this
  36. // volume being referenced by other container is
  37. // not an error, but an implementation detail.
  38. // This prevents docker from logging "ERROR: Volume in use"
  39. // where there is another container using the volume.
  40. if err != nil && !volumesservice.IsInUse(err) {
  41. rmErrors = append(rmErrors, err.Error())
  42. }
  43. }
  44. if len(rmErrors) > 0 {
  45. return fmt.Errorf("Error removing volumes:\n%v", strings.Join(rmErrors, "\n"))
  46. }
  47. return nil
  48. }