serving/samples: update helloworld-go to go:1.13 and go modules (#1774)

* serving/samples: update helloworld-go to go:1.13 and go modules

* seving/samples: sync helloworld-go Dockerfile to README

* alpine:3.10 => alpine:3

* serving/samples: helloworld-go name binary helloworld-server

* serving/samples: simpler comment for go build in helloworld-go

* serving/samples: helloworld-go README sync

* serving/samples: fix up README

* serving/samples: add go.mod to test config

* serving/samples: Add instruction to create a go.mod
This commit is contained in:
Adam Ross 2019-09-17 11:32:46 -07:00 committed by Knative Prow Robot
parent 3a2dde05fc
commit 61d24a1196
4 changed files with 45 additions and 22 deletions

View File

@ -1,24 +1,30 @@
# Use the offical Golang image to create a build artifact. # Use the offical Golang image to create a build artifact.
# This is based on Debian and sets the GOPATH to /go. # This is based on Debian and sets the GOPATH to /go.
# https://hub.docker.com/_/golang # 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. # Copy local code to the container image.
WORKDIR /go/src/github.com/knative/docs/helloworld COPY . ./
COPY . .
# Build the command inside the container. # Build the binary.
# (You may fetch or manage dependencies here, RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server
# 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 # 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 RUN apk add --no-cache ca-certificates
# Copy the binary to the production image from the builder stage. # 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. # Run the web service on container startup.
CMD ["/helloworld"] CMD ["/server"]

View File

@ -72,27 +72,33 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-go
# Use the offical Golang image to create a build artifact. # Use the offical Golang image to create a build artifact.
# This is based on Debian and sets the GOPATH to /go. # This is based on Debian and sets the GOPATH to /go.
# https://hub.docker.com/_/golang # 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. # Copy local code to the container image.
WORKDIR /go/src/github.com/knative/docs/helloworld COPY . ./
COPY . .
# Build the command inside the container. # Build the binary.
# (You may fetch or manage dependencies here, RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server
# 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 # 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 RUN apk add --no-cache ca-certificates
# Copy the binary to the production image from the builder stage. # 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. # Run the web service on container startup.
CMD ["/helloworld"] CMD ["/server"]
``` ```
1. Create a new file, `service.yaml` and copy the following service definition 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" 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 ## Building and deploying the sample
Once you have recreated the sample code files (or used the files in the sample Once you have recreated the sample code files (or used the files in the sample

View File

@ -0,0 +1,3 @@
module github.com/knative/docs/docs/serving/samples/hello-world/helloworld-go
go 1.13

View File

@ -11,6 +11,7 @@ languages:
- language: "go" - language: "go"
expectedOutput: "Hello Go Sample v1!" expectedOutput: "Hello Go Sample v1!"
copies: copies:
- "go.mod"
- "helloworld.go" - "helloworld.go"
- "service.yaml" - "service.yaml"
- "Dockerfile" - "Dockerfile"