commit bbf988c58a79246b32754634438b49ebd1e78a68 Author: serxoz Date: Tue Sep 5 09:43:11 2023 +0200 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba2906d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +main diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9c96eb9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +# Compilado +FROM golang:1.21.0 as builder + +WORKDIR / +COPY . . +RUN go build -ldflags "-linkmode external -extldflags -static" -a main.go + +# Execución +FROM scratch +COPY --from=builder /main /main + +EXPOSE 8080 +CMD ["./main"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bcec02d --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +NAME = exemplo +VERSION = 0.0.1 + +.PHONY: all build run push + +all: build + +build: + docker build -t $(NAME):$(VERSION) . + +run: + docker run -p 8088:8080 --name exemplo $(NAME):$(VERSION) + +clean: + docker rm exemplo + docker rmi exemplo:0.0.1 + rm ./main diff --git a/README.md b/README.md new file mode 100644 index 0000000..139e93b --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# exemplo imaxes docker minimas +Exemplo para ilustrar cómo crear imaxes docker mínimas gracias á compilación estática e á imaxen base «scratch». diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1314be3 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module exemplo + +go 1.21.0 diff --git a/main.go b/main.go new file mode 100644 index 0000000..e2f0c6d --- /dev/null +++ b/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "fmt" + "net/http" +) + +func main() { + http.HandleFunc("/", HelloServer) + http.ListenAndServe(":8080", nil) +} + +func HelloServer(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:]) +}