From f68efcf2b9990509703d826d36abb69ad0486eb6 Mon Sep 17 00:00:00 2001 From: Adam Ross Date: Thu, 5 Sep 2019 15:45:06 -0700 Subject: [PATCH 1/9] serving/samples: update helloworld-go to go:1.13 and go modules --- .../samples/hello-world/helloworld-go/Dockerfile | 13 +++++++------ .../samples/hello-world/helloworld-go/go.mod | 3 +++ 2 files changed, 10 insertions(+), 6 deletions(-) create mode 100644 docs/serving/samples/hello-world/helloworld-go/go.mod diff --git a/docs/serving/samples/hello-world/helloworld-go/Dockerfile b/docs/serving/samples/hello-world/helloworld-go/Dockerfile index 5d4a0584a8f..7ac5d1d53dc 100644 --- a/docs/serving/samples/hello-world/helloworld-go/Dockerfile +++ b/docs/serving/samples/hello-world/helloworld-go/Dockerfile @@ -1,24 +1,25 @@ # Use the offical Golang image to create a build artifact. # This is based on Debian and sets the GOPATH to /go. # https://hub.docker.com/_/golang -FROM golang:1.12 as builder +FROM golang:1.13 as builder # Copy local code to the container image. -WORKDIR /go/src/github.com/knative/docs/helloworld -COPY . . +WORKDIR /app +COPY . ./ # Build the command inside the container. # (You may fetch or manage dependencies here, # either manually or with a tool like "godep".) RUN CGO_ENABLED=0 GOOS=linux go build -v -o helloworld -# Use a Docker multi-stage build to create a lean production image. +# Use the official Alpine image for a lean production container. +# https://hub.docker.com/_/alpine # https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds -FROM alpine +FROM alpine:3.10 RUN apk add --no-cache ca-certificates # Copy the binary to the production image from the builder stage. -COPY --from=builder /go/src/github.com/knative/docs/helloworld/helloworld /helloworld +COPY --from=builder /app/helloworld /helloworld # Run the web service on container startup. CMD ["/helloworld"] diff --git a/docs/serving/samples/hello-world/helloworld-go/go.mod b/docs/serving/samples/hello-world/helloworld-go/go.mod new file mode 100644 index 00000000000..1def53d8d38 --- /dev/null +++ b/docs/serving/samples/hello-world/helloworld-go/go.mod @@ -0,0 +1,3 @@ +module github.com/knative/docs/docs/serving/samples/hello-world/helloworld-go + +go 1.13 From 66b92cf80c4d08f3eb73a5cf4758f581572bfd18 Mon Sep 17 00:00:00 2001 From: Adam Ross Date: Thu, 5 Sep 2019 15:48:17 -0700 Subject: [PATCH 2/9] seving/samples: sync helloworld-go Dockerfile to README --- .../samples/hello-world/helloworld-go/README.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/docs/serving/samples/hello-world/helloworld-go/README.md b/docs/serving/samples/hello-world/helloworld-go/README.md index 79d921575ac..97bd3680bea 100644 --- a/docs/serving/samples/hello-world/helloworld-go/README.md +++ b/docs/serving/samples/hello-world/helloworld-go/README.md @@ -72,24 +72,25 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-go # Use the offical Golang image to create a build artifact. # This is based on Debian and sets the GOPATH to /go. # https://hub.docker.com/_/golang - FROM golang:1.12 as builder + FROM golang:1.13 as builder # Copy local code to the container image. - WORKDIR /go/src/github.com/knative/docs/helloworld - COPY . . + WORKDIR /app + COPY . ./ # Build the command inside the container. # (You may fetch or manage dependencies here, # either manually or with a tool like "godep".) RUN CGO_ENABLED=0 GOOS=linux go build -v -o helloworld - # Use a Docker multi-stage build to create a lean production image. + # Use the official Alpine image for a lean production container. + # https://hub.docker.com/_/alpine # https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds - FROM alpine + FROM alpine:3.10 RUN apk add --no-cache ca-certificates # Copy the binary to the production image from the builder stage. - COPY --from=builder /go/src/github.com/knative/docs/helloworld/helloworld /helloworld + COPY --from=builder /app/helloworld /helloworld # Run the web service on container startup. CMD ["/helloworld"] From b0b4a3f6a54cde9bc99492eea130e64eb02799ff Mon Sep 17 00:00:00 2001 From: Adam Ross Date: Fri, 6 Sep 2019 08:53:31 -0700 Subject: [PATCH 3/9] alpine:3.10 => alpine:3 --- docs/serving/samples/hello-world/helloworld-go/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/serving/samples/hello-world/helloworld-go/Dockerfile b/docs/serving/samples/hello-world/helloworld-go/Dockerfile index 7ac5d1d53dc..fbc5dcbff94 100644 --- a/docs/serving/samples/hello-world/helloworld-go/Dockerfile +++ b/docs/serving/samples/hello-world/helloworld-go/Dockerfile @@ -15,7 +15,7 @@ RUN CGO_ENABLED=0 GOOS=linux go build -v -o helloworld # Use the official Alpine image for a lean production container. # https://hub.docker.com/_/alpine # https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds -FROM alpine:3.10 +FROM alpine:3 RUN apk add --no-cache ca-certificates # Copy the binary to the production image from the builder stage. From 812a8ea9b7039fdef9d1ac0d1850d3b0e9e2bb05 Mon Sep 17 00:00:00 2001 From: Adam Ross Date: Fri, 6 Sep 2019 08:58:53 -0700 Subject: [PATCH 4/9] serving/samples: helloworld-go name binary helloworld-server --- .../hello-world/helloworld-go/Dockerfile | 6 +++--- .../hello-world/helloworld-go/README.md | 18 +++++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/docs/serving/samples/hello-world/helloworld-go/Dockerfile b/docs/serving/samples/hello-world/helloworld-go/Dockerfile index fbc5dcbff94..11bc3331501 100644 --- a/docs/serving/samples/hello-world/helloworld-go/Dockerfile +++ b/docs/serving/samples/hello-world/helloworld-go/Dockerfile @@ -10,7 +10,7 @@ COPY . ./ # Build the command inside the container. # (You may fetch or manage dependencies here, # either manually or with a tool like "godep".) -RUN CGO_ENABLED=0 GOOS=linux go build -v -o helloworld +RUN CGO_ENABLED=0 GOOS=linux go build -v -o helloworld-server # Use the official Alpine image for a lean production container. # https://hub.docker.com/_/alpine @@ -19,7 +19,7 @@ FROM alpine:3 RUN apk add --no-cache ca-certificates # Copy the binary to the production image from the builder stage. -COPY --from=builder /app/helloworld /helloworld +COPY --from=builder /app/helloworld-server /helloworld-server # Run the web service on container startup. -CMD ["/helloworld"] +CMD ["/helloworld-server"] diff --git a/docs/serving/samples/hello-world/helloworld-go/README.md b/docs/serving/samples/hello-world/helloworld-go/README.md index 97bd3680bea..1fb0b5734a4 100644 --- a/docs/serving/samples/hello-world/helloworld-go/README.md +++ b/docs/serving/samples/hello-world/helloworld-go/README.md @@ -73,27 +73,27 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-go # This is based on Debian and sets the GOPATH to /go. # https://hub.docker.com/_/golang FROM golang:1.13 as builder - + # Copy local code to the container image. WORKDIR /app COPY . ./ - + # Build the command inside the container. # (You may fetch or manage dependencies here, # either manually or with a tool like "godep".) - RUN CGO_ENABLED=0 GOOS=linux go build -v -o helloworld - + RUN CGO_ENABLED=0 GOOS=linux go build -v -o helloworld-server + # Use the official Alpine image for a lean production container. # https://hub.docker.com/_/alpine # https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds - FROM alpine:3.10 + FROM alpine:3 RUN apk add --no-cache ca-certificates - + # Copy the binary to the production image from the builder stage. - COPY --from=builder /app/helloworld /helloworld - + COPY --from=builder /app/helloworld-server /helloworld-server + # Run the web service on container startup. - CMD ["/helloworld"] + CMD ["/helloworld-server"] ``` 1. Create a new file, `service.yaml` and copy the following service definition From a7e68f669d181c5c6b18524501857a9fd223476c Mon Sep 17 00:00:00 2001 From: Adam Ross Date: Wed, 11 Sep 2019 10:27:41 -0700 Subject: [PATCH 5/9] serving/samples: simpler comment for go build in helloworld-go --- docs/serving/samples/hello-world/helloworld-go/Dockerfile | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docs/serving/samples/hello-world/helloworld-go/Dockerfile b/docs/serving/samples/hello-world/helloworld-go/Dockerfile index 11bc3331501..7645711a363 100644 --- a/docs/serving/samples/hello-world/helloworld-go/Dockerfile +++ b/docs/serving/samples/hello-world/helloworld-go/Dockerfile @@ -7,9 +7,7 @@ FROM golang:1.13 as builder WORKDIR /app COPY . ./ -# Build the command inside the container. -# (You may fetch or manage dependencies here, -# either manually or with a tool like "godep".) +# Build the binary. RUN CGO_ENABLED=0 GOOS=linux go build -v -o helloworld-server # Use the official Alpine image for a lean production container. From e19ebe1627e6f016f608ff546f545147bbc09b42 Mon Sep 17 00:00:00 2001 From: Adam Ross Date: Wed, 11 Sep 2019 10:33:18 -0700 Subject: [PATCH 6/9] serving/samples: helloworld-go README sync --- .../samples/hello-world/helloworld-go/README.md | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/docs/serving/samples/hello-world/helloworld-go/README.md b/docs/serving/samples/hello-world/helloworld-go/README.md index 1fb0b5734a4..81252f7f779 100644 --- a/docs/serving/samples/hello-world/helloworld-go/README.md +++ b/docs/serving/samples/hello-world/helloworld-go/README.md @@ -73,25 +73,23 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-go # This is based on Debian and sets the GOPATH to /go. # https://hub.docker.com/_/golang FROM golang:1.13 as builder - + # Copy local code to the container image. WORKDIR /app COPY . ./ - - # Build the command inside the container. - # (You may fetch or manage dependencies here, - # either manually or with a tool like "godep".) + + # Build the binary. RUN CGO_ENABLED=0 GOOS=linux go build -v -o helloworld-server - + # Use the official Alpine image for a lean production container. # https://hub.docker.com/_/alpine # https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds FROM alpine:3 RUN apk add --no-cache ca-certificates - + # Copy the binary to the production image from the builder stage. COPY --from=builder /app/helloworld-server /helloworld-server - + # Run the web service on container startup. CMD ["/helloworld-server"] ``` From 4484ccc970efcec08d0f7744d9108619baac6cb3 Mon Sep 17 00:00:00 2001 From: Adam Ross Date: Wed, 11 Sep 2019 11:25:47 -0700 Subject: [PATCH 7/9] serving/samples: fix up README --- .../samples/hello-world/helloworld-go/Dockerfile | 15 +++++++++++---- .../samples/hello-world/helloworld-go/README.md | 15 +++++++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/docs/serving/samples/hello-world/helloworld-go/Dockerfile b/docs/serving/samples/hello-world/helloworld-go/Dockerfile index 7645711a363..06531f63163 100644 --- a/docs/serving/samples/hello-world/helloworld-go/Dockerfile +++ b/docs/serving/samples/hello-world/helloworld-go/Dockerfile @@ -3,12 +3,19 @@ # https://hub.docker.com/_/golang FROM golang:1.13 as builder -# Copy local code to the container image. +# Create and change to the app directory. WORKDIR /app + +# Retrieve application dependencies. +# This allows the container build to reuse cached dependencies. +COPY go.* ./ +RUN go mod download + +# Copy local code to the container image. COPY . ./ # Build the binary. -RUN CGO_ENABLED=0 GOOS=linux go build -v -o helloworld-server +RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server # Use the official Alpine image for a lean production container. # https://hub.docker.com/_/alpine @@ -17,7 +24,7 @@ FROM alpine:3 RUN apk add --no-cache ca-certificates # Copy the binary to the production image from the builder stage. -COPY --from=builder /app/helloworld-server /helloworld-server +COPY --from=builder /app/server /server # Run the web service on container startup. -CMD ["/helloworld-server"] +CMD ["/server"] diff --git a/docs/serving/samples/hello-world/helloworld-go/README.md b/docs/serving/samples/hello-world/helloworld-go/README.md index 81252f7f779..9d44c4cc8e6 100644 --- a/docs/serving/samples/hello-world/helloworld-go/README.md +++ b/docs/serving/samples/hello-world/helloworld-go/README.md @@ -74,12 +74,19 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-go # https://hub.docker.com/_/golang FROM golang:1.13 as builder - # Copy local code to the container image. + # Create and change to the app directory. WORKDIR /app + + # Retrieve application dependencies. + # This allows the container build to reuse cached dependencies. + COPY go.* ./ + RUN go mod download + + # Copy local code to the container image. COPY . ./ # Build the binary. - RUN CGO_ENABLED=0 GOOS=linux go build -v -o helloworld-server + RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server # Use the official Alpine image for a lean production container. # https://hub.docker.com/_/alpine @@ -88,10 +95,10 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-go RUN apk add --no-cache ca-certificates # Copy the binary to the production image from the builder stage. - COPY --from=builder /app/helloworld-server /helloworld-server + COPY --from=builder /app/server /server # Run the web service on container startup. - CMD ["/helloworld-server"] + CMD ["/server"] ``` 1. Create a new file, `service.yaml` and copy the following service definition From 007e214ec14c3636914f942aa9cc5e025b229a71 Mon Sep 17 00:00:00 2001 From: Adam Ross Date: Mon, 16 Sep 2019 09:48:42 -0700 Subject: [PATCH 8/9] serving/samples: add go.mod to test config --- test/sampleapp/config.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/test/sampleapp/config.yaml b/test/sampleapp/config.yaml index 049f2a7c8cf..5c7ed314c84 100644 --- a/test/sampleapp/config.yaml +++ b/test/sampleapp/config.yaml @@ -11,6 +11,7 @@ languages: - language: "go" expectedOutput: "Hello Go Sample v1!" copies: + - "go.mod" - "helloworld.go" - "service.yaml" - "Dockerfile" From 1930ac6d028cb9910677689f3d1d7f6a58ec3431 Mon Sep 17 00:00:00 2001 From: Adam Ross Date: Mon, 16 Sep 2019 09:49:39 -0700 Subject: [PATCH 9/9] serving/samples: Add instruction to create a go.mod --- docs/serving/samples/hello-world/helloworld-go/README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/serving/samples/hello-world/helloworld-go/README.md b/docs/serving/samples/hello-world/helloworld-go/README.md index 9d44c4cc8e6..79eccf38459 100644 --- a/docs/serving/samples/hello-world/helloworld-go/README.md +++ b/docs/serving/samples/hello-world/helloworld-go/README.md @@ -121,6 +121,13 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-go value: "Go Sample v1" ``` +1. Use the go tool to create a + [`go.mod`](https://github.com/golang/go/wiki/Modules#gomod) manifest. + + ```shell + go mod init github.com/knative/docs/docs/serving/samples/hello-world/helloworld-go + ``` + ## Building and deploying the sample Once you have recreated the sample code files (or used the files in the sample