|
| 1 | +# syntax=docker/dockerfile:1 |
| 2 | + |
| 3 | +# Comments are provided throughout this file to help you get started. |
| 4 | +# If you need more help, visit the Dockerfile reference guide at |
| 5 | +# https://docs.docker.com/go/dockerfile-reference/ |
| 6 | + |
| 7 | +# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7 |
| 8 | + |
| 9 | +################################################################################ |
| 10 | +# Create a stage for building the application. |
| 11 | +ARG GO_VERSION=1.22.4 |
| 12 | +FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} AS build |
| 13 | +WORKDIR /src |
| 14 | + |
| 15 | +# Download dependencies as a separate step to take advantage of Docker's caching. |
| 16 | +# Leverage a cache mount to /go/pkg/mod/ to speed up subsequent builds. |
| 17 | +# Leverage bind mounts to go.sum and go.mod to avoid having to copy them into |
| 18 | +# the container. |
| 19 | +RUN --mount=type=cache,target=/go/pkg/mod/ \ |
| 20 | + --mount=type=bind,source=go.sum,target=go.sum \ |
| 21 | + --mount=type=bind,source=go.mod,target=go.mod \ |
| 22 | + go mod download -x |
| 23 | + |
| 24 | +# This is the architecture you’re building for, which is passed in by the builder. |
| 25 | +# Placing it here allows the previous steps to be cached across architectures. |
| 26 | +ARG TARGETARCH |
| 27 | + |
| 28 | +# Build the application. |
| 29 | +# Leverage a cache mount to /go/pkg/mod/ to speed up subsequent builds. |
| 30 | +# Leverage a bind mount to the current directory to avoid having to copy the |
| 31 | +# source code into the container. |
| 32 | +RUN --mount=type=cache,target=/go/pkg/mod/ \ |
| 33 | + --mount=type=bind,target=. \ |
| 34 | + CGO_ENABLED=0 GOARCH=$TARGETARCH go build -o /bin/server . |
| 35 | + |
| 36 | +################################################################################ |
| 37 | +# Create a new stage for running the application that contains the minimal |
| 38 | +# runtime dependencies for the application. This often uses a different base |
| 39 | +# image from the build stage where the necessary files are copied from the build |
| 40 | +# stage. |
| 41 | +# |
| 42 | +# The example below uses the alpine image as the foundation for running the app. |
| 43 | +# By specifying the "latest" tag, it will also use whatever happens to be the |
| 44 | +# most recent version of that image when you build your Dockerfile. If |
| 45 | +# reproducability is important, consider using a versioned tag |
| 46 | +# (e.g., alpine:3.17.2) or SHA (e.g., alpine@sha256:c41ab5c992deb4fe7e5da09f67a8804a46bd0592bfdf0b1847dde0e0889d2bff). |
| 47 | +FROM alpine:latest AS final |
| 48 | + |
| 49 | +# Install any runtime dependencies that are needed to run your application. |
| 50 | +# Leverage a cache mount to /var/cache/apk/ to speed up subsequent builds. |
| 51 | +RUN --mount=type=cache,target=/var/cache/apk \ |
| 52 | + apk --update add \ |
| 53 | + ca-certificates \ |
| 54 | + tzdata \ |
| 55 | + && \ |
| 56 | + update-ca-certificates |
| 57 | + |
| 58 | +# Create a non-privileged user that the app will run under. |
| 59 | +# See https://docs.docker.com/go/dockerfile-user-best-practices/ |
| 60 | +ARG UID=10001 |
| 61 | +RUN adduser \ |
| 62 | + --disabled-password \ |
| 63 | + --gecos "" \ |
| 64 | + --home "/nonexistent" \ |
| 65 | + --shell "/sbin/nologin" \ |
| 66 | + --no-create-home \ |
| 67 | + --uid "${UID}" \ |
| 68 | + appuser |
| 69 | +USER appuser |
| 70 | + |
| 71 | +# Copy the executable from the "build" stage. |
| 72 | +COPY --from=build /bin/server /bin/ |
| 73 | + |
| 74 | +# Expose the port that the application listens on. |
| 75 | +EXPOSE 8080 |
| 76 | + |
| 77 | +# What the container should run when it is started. |
| 78 | +ENTRYPOINT [ "/bin/server" ] |
0 commit comments