From f2518ca45b7fc2b5e28e37480b5818ab22c9c4b5 Mon Sep 17 00:00:00 2001 From: Adam Ross Date: Mon, 19 Nov 2018 16:45:22 -0800 Subject: [PATCH] serving/samples/helloworld-go: simplify and standardize Dockerfile (#508) --- serving/samples/helloworld-go/Dockerfile | 26 ++++++++++++++-------- serving/samples/helloworld-go/README.md | 28 +++++++++++++++--------- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/serving/samples/helloworld-go/Dockerfile b/serving/samples/helloworld-go/Dockerfile index 2a9b4183f..84e77cc3f 100644 --- a/serving/samples/helloworld-go/Dockerfile +++ b/serving/samples/helloworld-go/Dockerfile @@ -1,17 +1,25 @@ -# Start from a Debian image with the latest version of Go installed -# and a workspace (GOPATH) configured at /go. -FROM golang +# Use the offical Golang image to create a build artifact. +# This is based on Debian and sets the GOPATH to /go. +FROM golang as builder -# Copy the local package files to the container's workspace. -ADD . /go/src/github.com/knative/docs/helloworld +# Copy local code to the container image. +COPY . /go/src/github.com/knative/docs/helloworld # Build the outyet command inside the container. # (You may fetch or manage dependencies here, # either manually or with a tool like "godep".) RUN go install github.com/knative/docs/helloworld -# Run the outyet command by default when the container starts. -ENTRYPOINT ["/go/bin/helloworld"] +# Use a Docker multi-stage build to create a lean production image. +# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds +FROM alpine -# Document that the service listens on port 8080. -EXPOSE 8080 +# Copy the binary to the production image from the builder stage. +COPY --from=builder /go/bin/helloworld /helloworld + +# Configure and document the service HTTP port. +ENV PORT 8080 +EXPOSE $PORT + +# Run the web service on container startup. +CMD ["/helloworld"] diff --git a/serving/samples/helloworld-go/README.md b/serving/samples/helloworld-go/README.md index a9716dafe..26ce91a78 100644 --- a/serving/samples/helloworld-go/README.md +++ b/serving/samples/helloworld-go/README.md @@ -59,23 +59,31 @@ following instructions recreate the source files from this folder. [Deploying Go servers with Docker](https://blog.golang.org/docker). ```docker - # Start from a Debian image with the latest version of Go installed - # and a workspace (GOPATH) configured at /go. - FROM golang + # Use the offical Golang image to create a build artifact. + # This is based on Debian and sets the GOPATH to /go. + FROM golang as builder - # Copy the local package files to the container's workspace. - ADD . /go/src/github.com/knative/docs/helloworld + # Copy local code to the container image. + COPY . /go/src/github.com/knative/docs/helloworld - # Build the helloworld command inside the container. + # Build the outyet command inside the container. # (You may fetch or manage dependencies here, # either manually or with a tool like "godep".) RUN go install github.com/knative/docs/helloworld - # Run the helloworld command by default when the container starts. - ENTRYPOINT /go/bin/helloworld + # Use a Docker multi-stage build to create a lean production image. + # https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds + FROM alpine - # Document that the service listens on port 8080. - EXPOSE 8080 + # Copy the binary to the production image from the builder stage. + COPY --from=builder /go/bin/helloworld /helloworld + + # Configure and document the service HTTP port. + ENV PORT 8080 + EXPOSE $PORT + + # Run the web service on container startup. + CMD ["/helloworld"] ``` 1. Create a new file, `service.yaml` and copy the following service definition