Last active
March 18, 2020 14:06
-
-
Save lcbm/fd752e0b1579fb7078fdfe3341aa4f4f to your computer and use it in GitHub Desktop.
Multi-stage Dockerfiles for Golang
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
# Using "go proxy" | |
# TODO: how do I separate the build from download | |
# using goproxy? | |
######################################## | |
## FIRST STAGE: build the application ## | |
######################################## | |
FROM golang:1.14-alpine AS builder | |
# Install certificates | |
RUN apk --no-cache add \ | |
ca-certificates \ | |
&& update-ca-certificates | |
# Set the Current Working Directory inside the container | |
WORKDIR /go/src/github.com/CESARBR/knot-babeltower | |
# Copy the source code from the current directory to $WORKDIR (inside the container) | |
COPY . . | |
# Build the app for linux target (cross compile disabled) | |
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \ | |
GO111MODULE=on GOPROXY=https://proxy.golang.org \ | |
go build -a -installsuffix cgo -o app cmd/main.go | |
########################################### | |
## SECOND STAGE: create production image ## | |
########################################### | |
# FROM alpine:3.11 | |
FROM scratch | |
WORKDIR /root/ | |
# Copy the configuration files from the build stage | |
COPY --from=builder /go/src/github.com/CESARBR/knot-babeltower/internal/ ./internal | |
# Copy the binary file from the build stage | |
COPY --from=builder /go/src/github.com/CESARBR/knot-babeltower/app app | |
# Copy SSL CA certificates | |
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ | |
ENTRYPOINT ["./app"] |
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
# Using "go mod download" | |
######################################## | |
## FIRST STAGE: build the application ## | |
######################################## | |
FROM golang:1.14-alpine AS builder | |
# Install build dependencies | |
RUN apk --no-cache add --virtual .build-deps \ | |
make \ | |
git | |
# Install certificates | |
RUN apk --no-cache add \ | |
ca-certificates \ | |
&& update-ca-certificates | |
# Set the Current Working Directory inside the container | |
WORKDIR /go/src/github.com/CESARBR/knot-babeltower | |
# Populate the module cache based on the go.{mod,sum} files | |
COPY go.mod . | |
COPY go.sum . | |
# Download dependancies (cached if the go.{mod,sum} files aren't changed) | |
RUN go mod download | |
RUN go mod verify | |
# Copy the source code from the current directory to $WORKDIR (inside the container) | |
COPY . . | |
# Build the app for linux target (cross compile disabled) | |
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \ | |
go build -a -installsuffix cgo -o app cmd/main.go | |
# # Remove build dependencies | |
RUN apk del .build-deps | |
########################################### | |
## SECOND STAGE: create production image ## | |
########################################### | |
# FROM alpine:3.11 | |
FROM scratch | |
WORKDIR /root/ | |
# Copy the configuration files from the build stage | |
COPY --from=builder /go/src/github.com/CESARBR/knot-babeltower/internal/ ./internal | |
# Copy the binary file from the build stage | |
COPY --from=builder /go/src/github.com/CESARBR/knot-babeltower/app app | |
# Copy SSL CA certificates | |
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ | |
ENTRYPOINT ["./app"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment