diff --git a/docker-build-with-broken-final-stage/Dockerfile b/docker-build-with-broken-final-stage/Dockerfile new file mode 100644 index 0000000..f087885 --- /dev/null +++ b/docker-build-with-broken-final-stage/Dockerfile @@ -0,0 +1,19 @@ +# From https://github.com/homeport/gonut/tree/master/assets/sample-apps/golang + +FROM ghcr.io/shipwright-io/shipwright-samples/golang:1.18 AS build + +COPY main.go . +ENV CGO_ENABLED=0 +RUN go build \ + -ldflags "-s -w -extldflags '-static'" \ + -o /tmp/helloworld \ + main.go + +FROM scratch AS working-final +COPY --from=build /tmp/helloworld ./helloworld +ENTRYPOINT [ "./helloworld" ] +EXPOSE 8080 + +# the following stage is INTENTIONALLY broken, one can only have a successful run when specifying working-final as target stage +FROM scratch +RUN non-existing-command diff --git a/docker-build-with-broken-final-stage/main.go b/docker-build-with-broken-final-stage/main.go new file mode 100644 index 0000000..6fee211 --- /dev/null +++ b/docker-build-with-broken-final-stage/main.go @@ -0,0 +1,47 @@ +// Copyright The Shipwright Contributors +// +// SPDX-License-Identifier: Apache-2.0 + +package main + +import ( + "context" + "fmt" + "log" + "net/http" + "os" + "os/signal" + "runtime" + "strconv" + "syscall" +) + +func main() { + ctx := context.Background() + signals := make(chan os.Signal, 1) + signal.Notify(signals, os.Interrupt, syscall.SIGTERM) + + port := 8080 + if strValue, ok := os.LookupEnv("PORT"); ok { + if intValue, err := strconv.Atoi(strValue); err == nil { + port = intValue + } + } + + srv := &http.Server{Addr: fmt.Sprintf(":%d", port)} + go func() { + http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) { + fmt.Fprintf(w, "Hello, World! I am using %s by the way.", runtime.Version()) + }) + + if err := srv.ListenAndServe(); err != http.ErrServerClosed { + log.Fatalf("failed to start server: %v", err) + } + }() + + <-signals + log.Printf("shutting down server") + if err := srv.Shutdown(ctx); err != nil { + log.Fatalf("failed to shutdown server: %v", err) + } +}