mounts.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "github.com/containerd/containerd/log"
  7. mounttypes "github.com/docker/docker/api/types/mount"
  8. "github.com/docker/docker/container"
  9. volumesservice "github.com/docker/docker/volume/service"
  10. "github.com/sirupsen/logrus"
  11. )
  12. func (daemon *Daemon) prepareMountPoints(container *container.Container) error {
  13. alive := container.IsRunning()
  14. for _, config := range container.MountPoints {
  15. if err := daemon.lazyInitializeVolume(container.ID, config); err != nil {
  16. return err
  17. }
  18. if config.Volume == nil {
  19. // FIXME(thaJeztah): should we check for config.Type here as well? (i.e., skip bind-mounts etc)
  20. continue
  21. }
  22. if alive {
  23. log.G(context.TODO()).WithFields(logrus.Fields{
  24. "container": container.ID,
  25. "volume": config.Volume.Name(),
  26. }).Debug("Live-restoring volume for alive container")
  27. if err := config.LiveRestore(context.TODO()); err != nil {
  28. return err
  29. }
  30. }
  31. }
  32. return nil
  33. }
  34. func (daemon *Daemon) removeMountPoints(container *container.Container, rm bool) error {
  35. var rmErrors []string
  36. ctx := context.TODO()
  37. for _, m := range container.MountPoints {
  38. if m.Type != mounttypes.TypeVolume || m.Volume == nil {
  39. continue
  40. }
  41. daemon.volumes.Release(ctx, m.Volume.Name(), container.ID)
  42. if !rm {
  43. continue
  44. }
  45. // Do not remove named mountpoints
  46. // these are mountpoints specified like `docker run -v <name>:/foo`
  47. if m.Spec.Source != "" {
  48. continue
  49. }
  50. err := daemon.volumes.Remove(ctx, m.Volume.Name())
  51. // Ignore volume in use errors because having this
  52. // volume being referenced by other container is
  53. // not an error, but an implementation detail.
  54. // This prevents docker from logging "ERROR: Volume in use"
  55. // where there is another container using the volume.
  56. if err != nil && !volumesservice.IsInUse(err) {
  57. rmErrors = append(rmErrors, err.Error())
  58. }
  59. }
  60. if len(rmErrors) > 0 {
  61. return fmt.Errorf("Error removing volumes:\n%v", strings.Join(rmErrors, "\n"))
  62. }
  63. return nil
  64. }