Skip to content

Instantly share code, notes, and snippets.

@lcbm
Last active March 18, 2020 14:06
Show Gist options
  • Save lcbm/fd752e0b1579fb7078fdfe3341aa4f4f to your computer and use it in GitHub Desktop.
Save lcbm/fd752e0b1579fb7078fdfe3341aa4f4f to your computer and use it in GitHub Desktop.
Multi-stage Dockerfiles for Golang
# 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"]
# 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