package daemon

import (
	"fmt"

	"github.com/docker/docker/container"
	"github.com/docker/docker/libcontainerd"
)

// platformConstructExitStatus returns a platform specific exit status structure
func platformConstructExitStatus(e libcontainerd.StateInfo) *container.ExitStatus {
	return &container.ExitStatus{
		ExitCode: int(e.ExitCode),
	}
}

// postRunProcessing perfoms any processing needed on the container after it has stopped.
func (daemon *Daemon) postRunProcessing(container *container.Container, e libcontainerd.StateInfo) error {
	if e.ExitCode == 0 && e.UpdatePending {
		spec, err := daemon.createSpec(container)
		if err != nil {
			return err
		}

		servicingOption := &libcontainerd.ServicingOption{
			IsServicing: true,
		}

		// Create a new servicing container, which will start, complete the update, and merge back the
		// results if it succeeded, all as part of the below function call.
		if err := daemon.containerd.Create((container.ID + "_servicing"), *spec, servicingOption); err != nil {
			container.ExitCode = -1
			return fmt.Errorf("Post-run update servicing failed: %s", err)
		}
	}
	return nil
}

package daemon

import (
	"fmt"

	"github.com/docker/docker/container"
	"github.com/docker/docker/libcontainerd"
)

// platformConstructExitStatus returns a platform specific exit status structure
func platformConstructExitStatus(e libcontainerd.StateInfo) *container.ExitStatus {
	return &container.ExitStatus{
		ExitCode: int(e.ExitCode),
	}
}

// postRunProcessing perfoms any processing needed on the container after it has stopped.
func (daemon *Daemon) postRunProcessing(container *container.Container, e libcontainerd.StateInfo) error {
	if e.UpdatePending {
		spec, err := daemon.createSpec(container)
		if err != nil {
			return err
		}

		servicingOption := &libcontainerd.ServicingOption{
			IsServicing: true,
		}

		// Create a new servicing container, which will start, complete the update, and merge back the
		// results if it succeeded, all as part of the below function call.
		if err := daemon.containerd.Create((container.ID + "_servicing"), *spec, servicingOption); err != nil {
			return fmt.Errorf("Post-run update servicing failed: %s", err)
		}
	}
	return nil
}

--- 
+++ 
@@ -16,7 +16,7 @@
 
 // postRunProcessing perfoms any processing needed on the container after it has stopped.
 func (daemon *Daemon) postRunProcessing(container *container.Container, e libcontainerd.StateInfo) error {
-	if e.UpdatePending {
+	if e.ExitCode == 0 && e.UpdatePending {
 		spec, err := daemon.createSpec(container)
 		if err != nil {
 			return err
@@ -29,6 +29,7 @@
 		// Create a new servicing container, which will start, complete the update, and merge back the
 		// results if it succeeded, all as part of the below function call.
 		if err := daemon.containerd.Create((container.ID + "_servicing"), *spec, servicingOption); err != nil {
+			container.ExitCode = -1
 			return fmt.Errorf("Post-run update servicing failed: %s", err)
 		}
 	}
Fix postRunProcessing behavior during docker build

If Windows updates are being applied via a RUN command in a docker build, the build will not stop if there was a failure in postRunProcessing.  To enable this behavior, we explicitly set the exit code of the container to a failure if postRunProcessing fails during servicing container step.  For completeness, also avoid running servicing operation if the original exit code of the container is non-zero so that original failure exit code does not get overwritten.

Signed-off-by: Stefan J. Wernli <83146b3f843e2cabf52cc6abaa32c20ae61a0a7e@microsoft.com>
