-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use multi-stage builds to create the image
- Loading branch information
Showing
3 changed files
with
37 additions
and
22 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,60 @@ | ||
package template | ||
|
||
const DockerfileDev = `FROM golang:{{.GoVersion}}-stretch | ||
const Dockerfile = `# --------------------------------------------------------------------- | ||
# The first stage container, for image base | ||
# --------------------------------------------------------------------- | ||
FROM golang:{{.GoVersion}}-stretch as base | ||
ENV GOLANG_CI_LINT_VERSION=v{{.CILintVersion}} | ||
RUN cd /usr && \ | ||
wget -O - -q https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s ${GOLANG_CI_LINT_VERSION} | ||
ARG USER | ||
ARG USER_ID | ||
ARG GROUP_ID | ||
RUN groupadd -f -g ${GROUP_ID} ${USER} && \ | ||
useradd -m -g ${GROUP_ID} -u ${USER_ID} ${USER} || echo "user already exists" | ||
RUN groupadd -f -g ${GROUP_ID} appuser && \ | ||
useradd -m -g ${GROUP_ID} -u ${USER_ID} appuser || echo "user already exists" | ||
USER ${USER_ID}:${GROUP_ID} | ||
WORKDIR /app | ||
` | ||
const Dockerfile = `FROM alpine:{{.AlpineVersion}} as base | ||
# --------------------------------------------------------------------- | ||
# The second stage container, for building the application | ||
# --------------------------------------------------------------------- | ||
FROM base AS builder | ||
RUN apt-get update && \ | ||
apt-get dist-upgrade -y && \ | ||
apt-get install -y --no-install-recommends ca-certificates tzdata && \ | ||
update-ca-certificates | ||
RUN adduser --disabled-password --gecos '' appuser | ||
WORKDIR $GOPATH/src/{{.Module}} | ||
COPY . . | ||
RUN apk --no-cache update && \ | ||
apk --no-cache add ca-certificates tzdata && \ | ||
rm -rf /var/cache/apk/* | ||
RUN go mod download | ||
RUN adduser -D -g '' appuser | ||
ARG VERSION | ||
COPY ./cmd/{{.Project}}/{{.Project}} /app/{{.Project}} | ||
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-w -s -X main.Version=${VERSION}" -o /go/bin/{{.Project}} ./cmd/{{.Project}} | ||
# --------------------------------------------------------------------- | ||
# The third stage container, for running the application | ||
# -------------------------------------------------------------------- | ||
FROM scratch | ||
COPY --from=base /usr/share/zoneinfo /usr/share/zoneinfo | ||
COPY --from=base /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ | ||
COPY --from=base /etc/passwd /etc/passwd | ||
COPY --from=base /etc/group /etc/group | ||
COPY --from=base /app/{{.Project}} /app/{{.Project}} | ||
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo | ||
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ | ||
COPY --from=builder /etc/passwd /etc/passwd | ||
COPY --from=builder /etc/group /etc/group | ||
COPY --from=builder /go/bin/{{.Project}} /bin/{{.Project}} | ||
# Use an unprivileged user. | ||
USER appuser | ||
ENTRYPOINT ["/app/{{.Project}}"] | ||
ENTRYPOINT ["/bin/{{.Project}}"] | ||
` |
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