Skip to content

Commit 61d24a1

Browse files
graysideknative-prow-robot
authored andcommitted
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
1 parent 3a2dde0 commit 61d24a1

File tree

4 files changed

+45
-22
lines changed

4 files changed

+45
-22
lines changed
Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
# Use the offical Golang image to create a build artifact.
22
# This is based on Debian and sets the GOPATH to /go.
33
# https://hub.docker.com/_/golang
4-
FROM golang:1.12 as builder
4+
FROM golang:1.13 as builder
5+
6+
# Create and change to the app directory.
7+
WORKDIR /app
8+
9+
# Retrieve application dependencies.
10+
# This allows the container build to reuse cached dependencies.
11+
COPY go.* ./
12+
RUN go mod download
513

614
# Copy local code to the container image.
7-
WORKDIR /go/src/github.com/knative/docs/helloworld
8-
COPY . .
15+
COPY . ./
916

10-
# Build the command inside the container.
11-
# (You may fetch or manage dependencies here,
12-
# either manually or with a tool like "godep".)
13-
RUN CGO_ENABLED=0 GOOS=linux go build -v -o helloworld
17+
# Build the binary.
18+
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server
1419

15-
# Use a Docker multi-stage build to create a lean production image.
20+
# Use the official Alpine image for a lean production container.
21+
# https://hub.docker.com/_/alpine
1622
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
17-
FROM alpine
23+
FROM alpine:3
1824
RUN apk add --no-cache ca-certificates
1925

2026
# Copy the binary to the production image from the builder stage.
21-
COPY --from=builder /go/src/github.com/knative/docs/helloworld/helloworld /helloworld
27+
COPY --from=builder /app/server /server
2228

2329
# Run the web service on container startup.
24-
CMD ["/helloworld"]
30+
CMD ["/server"]

docs/serving/samples/hello-world/helloworld-go/README.md

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,27 +72,33 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-go
7272
# Use the offical Golang image to create a build artifact.
7373
# This is based on Debian and sets the GOPATH to /go.
7474
# https://hub.docker.com/_/golang
75-
FROM golang:1.12 as builder
75+
FROM golang:1.13 as builder
76+
77+
# Create and change to the app directory.
78+
WORKDIR /app
79+
80+
# Retrieve application dependencies.
81+
# This allows the container build to reuse cached dependencies.
82+
COPY go.* ./
83+
RUN go mod download
7684
7785
# Copy local code to the container image.
78-
WORKDIR /go/src/github.com/knative/docs/helloworld
79-
COPY . .
86+
COPY . ./
8087
81-
# Build the command inside the container.
82-
# (You may fetch or manage dependencies here,
83-
# either manually or with a tool like "godep".)
84-
RUN CGO_ENABLED=0 GOOS=linux go build -v -o helloworld
88+
# Build the binary.
89+
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server
8590
86-
# Use a Docker multi-stage build to create a lean production image.
91+
# Use the official Alpine image for a lean production container.
92+
# https://hub.docker.com/_/alpine
8793
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
88-
FROM alpine
94+
FROM alpine:3
8995
RUN apk add --no-cache ca-certificates
9096
9197
# Copy the binary to the production image from the builder stage.
92-
COPY --from=builder /go/src/github.com/knative/docs/helloworld/helloworld /helloworld
98+
COPY --from=builder /app/server /server
9399
94100
# Run the web service on container startup.
95-
CMD ["/helloworld"]
101+
CMD ["/server"]
96102
```
97103

98104
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
115121
value: "Go Sample v1"
116122
```
117123
124+
1. Use the go tool to create a
125+
[`go.mod`](https://github.com/golang/go/wiki/Modules#gomod) manifest.
126+
127+
```shell
128+
go mod init github.com/knative/docs/docs/serving/samples/hello-world/helloworld-go
129+
```
130+
118131
## Building and deploying the sample
119132

120133
Once you have recreated the sample code files (or used the files in the sample
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/knative/docs/docs/serving/samples/hello-world/helloworld-go
2+
3+
go 1.13

test/sampleapp/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ languages:
1111
- language: "go"
1212
expectedOutput: "Hello Go Sample v1!"
1313
copies:
14+
- "go.mod"
1415
- "helloworld.go"
1516
- "service.yaml"
1617
- "Dockerfile"

0 commit comments

Comments
 (0)