diff --git a/docs/serving/samples/hello-world/helloworld-go/Dockerfile b/docs/serving/samples/hello-world/helloworld-go/Dockerfile index 5d4a0584a8f..06531f63163 100644 --- a/docs/serving/samples/hello-world/helloworld-go/Dockerfile +++ b/docs/serving/samples/hello-world/helloworld-go/Dockerfile @@ -1,24 +1,30 @@ # 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 + +# 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. -WORKDIR /go/src/github.com/knative/docs/helloworld -COPY . . +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 +# Build the binary. +RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server -# 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 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/server /server # Run the web service on container startup. -CMD ["/helloworld"] +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 79d921575ac..79eccf38459 100644 --- a/docs/serving/samples/hello-world/helloworld-go/README.md +++ b/docs/serving/samples/hello-world/helloworld-go/README.md @@ -72,27 +72,33 @@ 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 + + # 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. - WORKDIR /go/src/github.com/knative/docs/helloworld - COPY . . + 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 + # Build the binary. + RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server - # 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 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/server /server # Run the web service on container startup. - CMD ["/helloworld"] + CMD ["/server"] ``` 1. Create a new file, `service.yaml` and copy the following service definition @@ -115,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 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 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"