Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions docs/serving/samples/hello-world/helloworld-go/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason not to use golang:1?

We have to keep updating this every 6 months otherwise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that sometimes the minor version has significant change to best practices (e.g., modules) I think the clarity is worthwhile for the reader and motivation to update.


# 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 . ./
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should do the go mod download trick, since many people will copy this? I know it's been decided on in the past, but consider adding it.

(I'm not attached to adding it - definitely makes things more complex, but it might be worth it.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added go mod download and modified go build to use -mod=readonly for strictness of the build.


# 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"]
35 changes: 24 additions & 11 deletions docs/serving/samples/hello-world/helloworld-go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions docs/serving/samples/hello-world/helloworld-go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/knative/docs/docs/serving/samples/hello-world/helloworld-go

go 1.13
Copy link
Contributor

@chizhg chizhg Sep 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an empty line after this line. Otherwise go build will not work with -mod=readonly.

1 change: 1 addition & 0 deletions test/sampleapp/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ languages:
- language: "go"
expectedOutput: "Hello Go Sample v1!"
copies:
- "go.mod"
- "helloworld.go"
- "service.yaml"
- "Dockerfile"
Expand Down