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>
master
Milas Bowman 2 years ago committed by GitHub
parent 7f5179ea3e
commit 42f6713231
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,19 @@
services:
proxy:
image: nginx
volumes:
- type: bind
source: ./proxy/nginx.conf
target: /etc/nginx/conf.d/default.conf
read_only: true
ports:
- 80:80
depends_on:
- backend
backend:
build:
context: backend
target: dev-envs
volumes:
- /var/run/docker.sock:/var/run/docker.sock

@ -1,5 +1,5 @@
## Compose sample application ## Compose sample application
### NGINX proxy with GO backend ### NGINX proxy with Go backend
Project structure: Project structure:
``` ```
@ -8,25 +8,32 @@ Project structure:
│   ├── Dockerfile │   ├── Dockerfile
│   └── main.go │   └── main.go
├── compose.yaml ├── compose.yaml
├── frontend ├── proxy
│   ├── Dockerfile
│   └── nginx.conf │   └── nginx.conf
└── README.md └── README.md
``` ```
[_compose.yaml_](compose.yaml) [`compose.yaml`](compose.yaml)
``` ```
services: services:
frontend: proxy:
build: frontend image: nginx
volumes:
- type: bind
source: ./proxy/nginx.conf
target: /etc/nginx/conf.d/default.conf
read_only: true
ports: ports:
- 80:80 - 80:80
depends_on: depends_on:
- backend - backend
backend: backend:
build: backend build:
context: backend
target: builder
``` ```
The compose file defines an application with two services `frontend` and `backend`. The compose file defines an application with two services `proxy` and `backend`.
When deploying the application, docker compose maps port 80 of the frontend service container to the same port of the host as specified in the file. When deploying the application, docker compose maps port 80 of the frontend service container to the same port of the host as specified in the file.
Make sure port 80 on the host is not already in use. Make sure port 80 on the host is not already in use.
@ -40,20 +47,19 @@ Step 1/7 : FROM golang:1.13 AS build
1.13: Pulling from library/golang 1.13: Pulling from library/golang
... ...
Successfully built 4b24f27138cc Successfully built 4b24f27138cc
Successfully tagged nginx-golang_frontend:latest Successfully tagged nginx-golang_proxy:latest
WARNING: Image for service frontend was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating nginx-golang_backend_1 ... done Creating nginx-golang_backend_1 ... done
Creating nginx-golang_frontend_1 ... done Creating nginx-golang_proxy_1 ... done
``` ```
## Expected result ## Expected result
Listing containers must show two containers running and the port mapping as below: Listing containers must show two containers running and the port mapping as below:
``` ```
$ docker ps $ docker compose ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES NAME COMMAND SERVICE STATUS PORTS
8bd5b0d78e73 nginx-golang_frontend "nginx -g 'daemon of…" 53 seconds ago Up 52 seconds 0.0.0.0:80->80/tcp nginx-golang_frontend_1 nginx-golang-backend-1 "/code/bin/backend" backend running
56f929c240a0 nginx-golang_backend "/usr/local/bin/back…" 53 seconds ago Up 53 seconds nginx-golang_backend_1 nginx-golang-proxy-1 "/docker-entrypoint.…" proxy running 0.0.0.0:80->80/tcp
``` ```
After the application starts, navigate to `http://localhost:80` in your web browser or run: After the application starts, navigate to `http://localhost:80` in your web browser or run:
@ -77,3 +83,14 @@ Stop and remove the containers
``` ```
$ docker compose down $ docker compose down
``` ```
## Use with Docker Development Environments
You can use this sample with the Dev Environments feature of Docker Desktop.
![Screenshot of creating a Dev Environment in Docker Desktop](../dev-envs.png)
To develop directly on the services inside containers, use the HTTPS Git url of the sample:
```
https://github.com/docker/awesome-compose/tree/master/nginx-golang
```

@ -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 WORKDIR /code
COPY main.go main.go
RUN CGO_ENABLED=0 go build -o backend main.go
FROM scratch ENV CGO_ENABLED 0
COPY --from=build /compose/hello-docker/backend /usr/local/bin/backend ENV GOPATH /go
CMD ["/usr/local/bin/backend"] 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=builder /code/bin/backend /usr/local/bin/backend
CMD ["/usr/local/bin/backend"]

@ -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

@ -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=

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

@ -1,9 +1,17 @@
services: services:
frontend: proxy:
build: frontend image: nginx
volumes:
- type: bind
source: ./proxy/nginx.conf
target: /etc/nginx/conf.d/default.conf
read_only: true
ports: ports:
- 80:80 - 80:80
depends_on: depends_on:
- backend - backend
backend: backend:
build: backend build:
context: backend
target: builder

@ -1,4 +0,0 @@
FROM nginx:alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf
Loading…
Cancel
Save