Browse code

Merge pull request #2614 from dotcloud/prevent_delete_image_running_container

Runtime: prevent deletion if image is used by a running container

Victor Vieux authored on 2013/11/13 04:01:51
Showing 1 changed files
... ...
@@ -1257,6 +1257,26 @@ func (srv *Server) ImageDelete(name string, autoPrune bool) ([]APIRmi, error) {
1257 1257
 		}
1258 1258
 		return nil, nil
1259 1259
 	}
1260
+
1261
+	// Prevent deletion if image is used by a running container
1262
+	for _, container := range srv.runtime.List() {
1263
+		if container.State.Running {
1264
+			parent, err := srv.runtime.repositories.LookupImage(container.Image)
1265
+			if err != nil {
1266
+				return nil, err
1267
+			}
1268
+
1269
+			if err := parent.WalkHistory(func(p *Image) error {
1270
+				if img.ID == p.ID {
1271
+					return fmt.Errorf("Conflict, cannot delete %s because the running container %s is using it", name, container.ID)
1272
+				}
1273
+				return nil
1274
+			}); err != nil {
1275
+				return nil, err
1276
+			}
1277
+		}
1278
+	}
1279
+
1260 1280
 	if strings.Contains(img.ID, name) {
1261 1281
 		//delete via ID
1262 1282
 		return srv.deleteImage(img, "", "")