Skip to content

Instantly share code, notes, and snippets.

@boseji
Last active September 27, 2024 05:36
Show Gist options
  • Save boseji/19945209b840d9ac70f29d33c9481643 to your computer and use it in GitHub Desktop.
Save boseji/19945209b840d9ac70f29d33c9481643 to your computer and use it in GitHub Desktop.
Boseji's Docker Build File for Golang Applications
#
# ॐ भूर्भुवः स्वः
# तत्स॑वि॒तुर्वरे॑ण्यं॒
# भर्गो॑ दे॒वस्य॑ धीमहि।
# धियो॒ यो नः॑ प्रचो॒दया॑त्॥
#
#
# बोसजी के द्वारा रचित गोलङ्ग् कि डोकर कार्यविधि।
# ===================================
#
# एक सरल संचार सहायक और संलग्न तंत्र।
#
# ~~~~~~~~~~~~~~~~~~~~~~~
# एक रचनात्मक भारतीय उत्पाद।
# ~~~~~~~~~~~~~~~~~~~~~~~
#
# ___________________________________________________
#
# Boseji's Docker Build File for Golang Applications
# ___________________________________________________
#
# Sources
# --------
#
# https://gist.github.com/boseji/
#
# License
# ----------
#
# Boseji's Docker Build File for Golang Applications
#
# Copyright (C) 2024 Abhijit Bose (aka. Boseji). All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# <http://www.apache.org/licenses/LICENSE-2.0>
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX short identifier: Apache-2.0
#
FROM golang:1.23.1-alpine AS builder
WORKDIR /builder
# UPX For squizing
RUN apk --no-cache add upx tzdata ca-certificates
# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
COPY go.* ./
RUN go mod download && go mod verify
# Build the App
COPY . .
RUN GOOS=linux go build -v -o goapp ./...
RUN upx -9 /builder/goapp
# scratch is an empty image
# FROM gcr.io/distroless/static:latest
# FROM alpine:latest
FROM scratch
## Open Container Spec
# https://github.com/opencontainers/image-spec/blob/main/annotations.md
LABEL org.opencontainers.image.authors="Abhijit Bose (aka. Boseji)"
LABEL org.opencontainers.image.version="1.0"
LABEL org.opencontainers.image.description=" \
Small Container for a Golang application."
LABEL org.opencontainers.image.licenses="Apache-2.0"
# If you need /bin/sh and a few utilities, uncomment
# the following line. It increases the image by 5.5 MB
# FROM alpine:latest
COPY --from=builder /builder/goapp /usr/bin/
# copy other files if needed
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
ENTRYPOINT ["goapp"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment