Skip to content

Commit

Permalink
Merge pull request #16 from shipwright-io/sascha-add-docker-build-wit…
Browse files Browse the repository at this point in the history
…h-broken-final-stage

Add Docker build with broken final stage
  • Loading branch information
openshift-merge-bot[bot] authored Nov 25, 2024
2 parents 96afb41 + 4b8ca1f commit b491399
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
19 changes: 19 additions & 0 deletions docker-build-with-broken-final-stage/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions docker-build-with-broken-final-stage/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit b491399

Please sign in to comment.