Skip to content

Instantly share code, notes, and snippets.

@jeffotoni
Last active February 16, 2022 17:07
Show Gist options
  • Save jeffotoni/59c0490f3c8ae4007170f9e89a0d082c to your computer and use it in GitHub Desktop.
Save jeffotoni/59c0490f3c8ae4007170f9e89a0d082c to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"net/http"
)
func Hello(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Hello, welcome to the world, Go!"))
}
func main() {
mux := http.NewServeMux()
mux.Handle("/api/hello", http.HandlerFunc(Hello))
server :=
&http.Server{
Addr: ":8080",
Handler: mux,
}
fmt.Printf("Server Run port: 8080\n")
if err := server.ListenAndServe(); err != nil {
log.Printf("Eror while serving metrics: %s", err)
}
}
/// curl localhost:8080/api/hello
# compilar
$ CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo
#################################################
# DockerFile scratch
#################################################
FROM scratch
ADD main /
CMD ["/main"]
-------------------------------------------------------------------------
#################################################
# Dockerfile distroless
#################################################
FROM golang:1.12.0 as builder
WORKDIR /go/src/main
ENV GO111MODULE=on
COPY . .
RUN go install -v ./...
############################
# STEP 2 build a small image
############################
FROM gcr.io/distroless/base
COPY --from=builder /go/bin/main /
CMD ["/main"]
-------------------------------------------------------------------------
#################################################
# Strach + Muiltistage
#################################################
FROM golang:1.12.0 as builder
WORKDIR /go/src/main
ENV GO111MODULE=on
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /app .
############################
# STEP 2 build a small image
############################
FROM scratch
COPY --from=0 /app /app
CMD ["/app"]
-------------------------------------------------------------------------
#################################################
# Alpine + Strach + Muiltistage
#################################################
FROM golang:alpine AS builder
# Install git.
# Git is required for fetching the dependencies.
RUN apk update && apk add --no-cache git
WORKDIR /go/src/main
ENV GO111MODULE=on
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /app .
############################
# STEP 2 build a small image
############################
FROM scratch
# Copy our static executable.
COPY --from=builder /app /app
# Run the hello binary.
ENTRYPOINT ["/app"]
@brbarmex
Copy link

brbarmex commented Feb 16, 2022

The disabling DWARF generation will greatly reduce the size of the binary because it will delete debug information without losing any functionality

GCO_ENABLED=0 GOOS=linux go build -ldflags "-s -w" -a -installsuffix cgo -o /app

Sample:

FROM golang:alpine AS builder
RUN apk update && apk add --no-cache git build-base
WORKDIR /go/src/main
ENV GO111MODULE=on
COPY . .
RUN GCO_ENABLED=0 GOOS=linux go build -ldflags "-s -w" -a -installsuffix cgo -o /app .

FROM scratch
COPY --from=builder /app /app
ENTRYPOINT ["/app"]

Screen Shot 2022-02-16 at 11 29 41

@jeffotoni
Copy link
Author

@BrunoBMelo Perfect 😍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment