nginx-golang: add dev envs support (#273)

* Add Docker Desktop Developer Environments config
* Upgrade from Go 1.13 (🙀) to 1.18
* Rename `frontend` -> `proxy` for clarity & consistency
  with other samples
* Add Chi as a dependency to provide an example of Go
  module dependencies for parity with other samples

Signed-off-by: Milas Bowman <milas.bowman@docker.com>
This commit is contained in:
Milas Bowman
2022-07-12 04:27:45 -04:00
committed by GitHub
parent 7f5179ea3e
commit 42f6713231
9 changed files with 119 additions and 34 deletions

View File

@@ -1,11 +1,41 @@
FROM golang:1.13 AS build
# syntax=docker/dockerfile:1.4
FROM --platform=$BUILDPLATFORM golang:1.18-alpine AS builder
WORKDIR /compose/hello-docker
COPY main.go main.go
RUN CGO_ENABLED=0 go build -o backend main.go
WORKDIR /code
ENV CGO_ENABLED 0
ENV GOPATH /go
ENV GOCACHE /go-build
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod/cache \
go mod download
COPY . .
RUN --mount=type=cache,target=/go/pkg/mod/cache \
--mount=type=cache,target=/go-build \
go build -o bin/backend main.go
CMD ["/code/bin/backend"]
FROM builder as dev-envs
RUN <<EOF
apk update
apk add git
EOF
RUN <<EOF
addgroup -S docker
adduser -S --shell /bin/bash --ingroup docker vscode
EOF
# install Docker tools (cli, buildx, compose)
COPY --from=gloursdocker/docker / /
CMD ["go", "run", "main.go"]
FROM scratch
COPY --from=build /compose/hello-docker/backend /usr/local/bin/backend
COPY --from=builder /code/bin/backend /usr/local/bin/backend
CMD ["/usr/local/bin/backend"]

View File

@@ -0,0 +1,5 @@
module github.com/docker/awesome-compose/nginx-golang/backend
go 1.18
require github.com/go-chi/chi/v5 v5.0.7

View File

@@ -0,0 +1,2 @@
github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8=
github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=

View File

@@ -4,11 +4,14 @@ import (
"fmt"
"log"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL.RawQuery)
fmt.Fprintf(w, `
fmt.Fprintf(
w, `
## .
## ## ## ==
## ## ## ## ## ===
@@ -21,10 +24,15 @@ func handler(w http.ResponseWriter, r *http.Request) {
Hello from Docker!
`)
`,
)
}
func main() {
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":80", nil))
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Get("/", handler)
fmt.Println("Go backend started!")
log.Fatal(http.ListenAndServe(":80", r))
}