Skip to content

Commit

Permalink
Merge pull request moby#16285 from calavera/cleanup_volumes_when_crea…
Browse files Browse the repository at this point in the history
…te_fails

Remove volume references when container creation fails.
  • Loading branch information
cpuguy83 committed Sep 15, 2015
2 parents e91f2c2 + 2c6c077 commit 59311fa
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
13 changes: 9 additions & 4 deletions daemon/container_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -1188,14 +1188,19 @@ func (container *Container) removeMountPoints(rm bool) error {
}
container.daemon.volumes.Decrement(m.Volume)
if rm {
if err := container.daemon.volumes.Remove(m.Volume); err != nil {
rmErrors = append(rmErrors, fmt.Sprintf("%v\n", err))
continue
err := container.daemon.volumes.Remove(m.Volume)
// ErrVolumeInUse is ignored because having this
// volume being referenced by othe container is
// not an error, but an implementation detail.
// This prevents docker from logging "ERROR: Volume in use"
// where there is another container using the volume.
if err != nil && err != ErrVolumeInUse {
rmErrors = append(rmErrors, err.Error())
}
}
}
if len(rmErrors) > 0 {
return fmt.Errorf("Error removing volumes:\n%v", rmErrors)
return fmt.Errorf("Error removing volumes:\n%v", strings.Join(rmErrors, "\n"))
}
return nil
}
7 changes: 7 additions & 0 deletions daemon/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ func (daemon *Daemon) Create(config *runconfig.Config, hostConfig *runconfig.Hos
if err := daemon.setHostConfig(container, hostConfig); err != nil {
return nil, nil, err
}
defer func() {
if retErr != nil {
if err := container.removeMountPoints(true); err != nil {
logrus.Error(err)
}
}
}()
if err := container.Mount(); err != nil {
return nil, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion daemon/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (daemon *Daemon) ContainerRm(name string, config *ContainerRmConfig) error
}

if err := container.removeMountPoints(config.RemoveVolume); err != nil {
logrus.Errorf("%v", err)
logrus.Error(err)
}

return nil
Expand Down
5 changes: 5 additions & 0 deletions daemon/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"sync"

"github.com/Sirupsen/logrus"
"github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/chrootarchive"
"github.com/docker/docker/pkg/system"
Expand Down Expand Up @@ -139,6 +140,7 @@ func (s *volumeStore) Create(name, driverName string, opts map[string]string) (v
return v, nil
}
s.mu.Unlock()
logrus.Debugf("Registering new volume reference: driver %s, name %s", driverName, name)

vd, err := getVolumeDriver(driverName)
if err != nil {
Expand Down Expand Up @@ -173,6 +175,7 @@ func (s *volumeStore) Remove(v volume.Volume) error {
s.mu.Lock()
defer s.mu.Unlock()
name := v.Name()
logrus.Debugf("Removing volume reference: driver %s, name %s", v.DriverName(), name)
vc, exists := s.vols[name]
if !exists {
return ErrNoSuchVolume
Expand All @@ -197,6 +200,7 @@ func (s *volumeStore) Remove(v volume.Volume) error {
func (s *volumeStore) Increment(v volume.Volume) {
s.mu.Lock()
defer s.mu.Unlock()
logrus.Debugf("Incrementing volume reference: driver %s, name %s", v.DriverName(), v.Name())

vc, exists := s.vols[v.Name()]
if !exists {
Expand All @@ -211,6 +215,7 @@ func (s *volumeStore) Increment(v volume.Volume) {
func (s *volumeStore) Decrement(v volume.Volume) {
s.mu.Lock()
defer s.mu.Unlock()
logrus.Debugf("Decrementing volume reference: driver %s, name %s", v.DriverName(), v.Name())

vc, exists := s.vols[v.Name()]
if !exists {
Expand Down

0 comments on commit 59311fa

Please sign in to comment.