From 22656fb4dc8500a9effcc36bed5403e45004cfa0 Mon Sep 17 00:00:00 2001 From: Sascha Schwarze Date: Thu, 21 Nov 2024 21:28:38 +0100 Subject: [PATCH 1/2] Add Docker build with broken final stage Signed-off-by: Sascha Schwarze --- .../Dockerfile | 19 ++++++++ docker-build-with-broken-final-stage/main.go | 47 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 docker-build-with-broken-final-stage/Dockerfile create mode 100644 docker-build-with-broken-final-stage/main.go diff --git a/docker-build-with-broken-final-stage/Dockerfile b/docker-build-with-broken-final-stage/Dockerfile new file mode 100644 index 0000000..f20831c --- /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 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) + } +} From 4b8ca1f02dbbfbf59e0cad1edbccc85b4f2497b0 Mon Sep 17 00:00:00 2001 From: Sascha Schwarze Date: Mon, 25 Nov 2024 09:27:10 +0100 Subject: [PATCH 2/2] Correct stage name in comment Co-authored-by: Enrique Encalada Signed-off-by: Sascha Schwarze --- docker-build-with-broken-final-stage/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-build-with-broken-final-stage/Dockerfile b/docker-build-with-broken-final-stage/Dockerfile index f20831c..f087885 100644 --- a/docker-build-with-broken-final-stage/Dockerfile +++ b/docker-build-with-broken-final-stage/Dockerfile @@ -14,6 +14,6 @@ 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 final as target stage +# 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