This example also contains a two-stage Dockerfile Dockerfile-multistage and a corresponding wercker-multistage.yml.
The use of multi-stage Dockerfiles requires Docker 17.05, which is why this is provided as a separate example.
# Step #1 Run unit tests and build an executable that doesn't require the go librraies
FROM golang as firststage
WORKDIR /work
ADD . .
RUN go test ./...
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o myapp .
#
# Step #2: Copy the executable into a minimal image (less than 5MB) which doesn't contain the build tools and artifacts
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=firststage /work/myapp .
CMD ["./myapp"]
This is a simple two-stage docker build. The first stage, which uses a golang base image, runs the tests and then builds the executable. The second stage, which uses an alpine linux image, simply copies the executable from the previous stage into the new image. The result of the second stage is a very small image that does not contain any source code or golang build tools.
To build the image using the docker command:
docker build . -f Dockerfile-multistage -t my-image
See the main README for the subsequent steps to tag, push, run and test this image.
This example also contains a file wercker-multistage.yml which uses Dockerfile-multistage to build the image.
To build and push the image using Wercker:
wercker build --wercker-yml wercker-multistage.yml
See the main README for the environment variables you need to set first, and for the subsequent steps to run and test this image.
Sign up for Wercker: http://www.wercker.com
Learn more at: http://devcenter.wercker.com