Last active
February 16, 2022 17:07
-
-
Save jeffotoni/59c0490f3c8ae4007170f9e89a0d082c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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"] |
$ docker build -t example-golang-scratch -f Dockerfile .
$ docker run --rm -it example-golang-scratch
$ curl localhost:8080/api/hello
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"]
@BrunoBMelo Perfect 😍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source Code Here:
https://github.com/jeffotoni/goexample/tree/master/api-simple