Procházet zdrojové kódy

Optimize the restart function code

Signed-off-by: zhouhao <zhouhao@cn.fujitsu.com>
zhouhao před 9 roky
rodič
revize
870cf1096f
1 změnil soubory, kde provedl 18 přidání a 16 odebrání
  1. 18 16
      daemon/restart.go

+ 18 - 16
daemon/restart.go

@@ -36,25 +36,27 @@ func (daemon *Daemon) containerRestart(container *container.Container, seconds i
 		defer daemon.Unmount(container)
 	}
 
-	// set AutoRemove flag to false before stop so the container won't be
-	// removed during restart process
-	autoRemove := container.HostConfig.AutoRemove
-
-	container.HostConfig.AutoRemove = false
-	err := daemon.containerStop(container, seconds)
-	// restore AutoRemove irrespective of whether the stop worked or not
-	container.HostConfig.AutoRemove = autoRemove
-	// containerStop will write HostConfig to disk, we shall restore AutoRemove
-	// in disk too
-	if toDiskErr := container.ToDiskLocking(); toDiskErr != nil {
-		logrus.Errorf("Write container to disk error: %v", toDiskErr)
-	}
+	if container.IsRunning() {
+		// set AutoRemove flag to false before stop so the container won't be
+		// removed during restart process
+		autoRemove := container.HostConfig.AutoRemove
 
-	if err != nil {
-		return err
+		container.HostConfig.AutoRemove = false
+		err := daemon.containerStop(container, seconds)
+		// restore AutoRemove irrespective of whether the stop worked or not
+		container.HostConfig.AutoRemove = autoRemove
+		// containerStop will write HostConfig to disk, we shall restore AutoRemove
+		// in disk too
+		if toDiskErr := container.ToDiskLocking(); toDiskErr != nil {
+			logrus.Errorf("Write container to disk error: %v", toDiskErr)
+		}
+
+		if err != nil {
+			return err
+		}
 	}
 
-	if err = daemon.containerStart(container); err != nil {
+	if err := daemon.containerStart(container); err != nil {
 		return err
 	}