Created
February 22, 2021 21:56
-
-
Save phrozen/ae36c0086bc5f3eb1c8e630d3b619c37 to your computer and use it in GitHub Desktop.
Golang Dockerfile template for binary only images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1. Build a statically linked binary | |
FROM golang:alpine as builder | |
# Install git to install dependencies (skip if vendored) | |
RUN apk add --update --no-cache git | |
# Copy source code to working directory | |
COPY . /src | |
WORKDIR /src | |
# Add some env vars for the build process | |
ENV CGO_ENABLED=0 | |
ENV GOOS=linux | |
ENV GOARCH=amd64 | |
# Build the statically linked binary | |
RUN go build -ldflags="-w -s" -o /app | |
# 2. Build a new image from scratch with just the app binary | |
FROM scratch | |
COPY --from=builder /app /app | |
# 3. Run the app binary. | |
ENTRYPOINT ["/app"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment