Browse code

Optimize the restart function code

Signed-off-by: zhouhao <zhouhao@cn.fujitsu.com>

zhouhao authored on 2016/08/25 16:54:49
Showing 1 changed files
... ...
@@ -36,25 +36,27 @@ func (daemon *Daemon) containerRestart(container *container.Container, seconds i
36 36
 		defer daemon.Unmount(container)
37 37
 	}
38 38
 
39
-	// set AutoRemove flag to false before stop so the container won't be
40
-	// removed during restart process
41
-	autoRemove := container.HostConfig.AutoRemove
42
-
43
-	container.HostConfig.AutoRemove = false
44
-	err := daemon.containerStop(container, seconds)
45
-	// restore AutoRemove irrespective of whether the stop worked or not
46
-	container.HostConfig.AutoRemove = autoRemove
47
-	// containerStop will write HostConfig to disk, we shall restore AutoRemove
48
-	// in disk too
49
-	if toDiskErr := container.ToDiskLocking(); toDiskErr != nil {
50
-		logrus.Errorf("Write container to disk error: %v", toDiskErr)
51
-	}
39
+	if container.IsRunning() {
40
+		// set AutoRemove flag to false before stop so the container won't be
41
+		// removed during restart process
42
+		autoRemove := container.HostConfig.AutoRemove
52 43
 
53
-	if err != nil {
54
-		return err
44
+		container.HostConfig.AutoRemove = false
45
+		err := daemon.containerStop(container, seconds)
46
+		// restore AutoRemove irrespective of whether the stop worked or not
47
+		container.HostConfig.AutoRemove = autoRemove
48
+		// containerStop will write HostConfig to disk, we shall restore AutoRemove
49
+		// in disk too
50
+		if toDiskErr := container.ToDiskLocking(); toDiskErr != nil {
51
+			logrus.Errorf("Write container to disk error: %v", toDiskErr)
52
+		}
53
+
54
+		if err != nil {
55
+			return err
56
+		}
55 57
 	}
56 58
 
57
-	if err = daemon.containerStart(container); err != nil {
59
+	if err := daemon.containerStart(container); err != nil {
58 60
 		return err
59 61
 	}
60 62