From 42f6713231319b39f5719f37cb8cf59c7fc084a7 Mon Sep 17 00:00:00 2001 From: Milas Bowman Date: Tue, 12 Jul 2022 04:27:45 -0400 Subject: [PATCH] nginx-golang: add dev envs support (#273) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- nginx-golang/.docker/docker-compose.yaml | 19 +++++++++ nginx-golang/README.md | 47 ++++++++++++++------- nginx-golang/backend/Dockerfile | 44 ++++++++++++++++--- nginx-golang/backend/go.mod | 5 +++ nginx-golang/backend/go.sum | 2 + nginx-golang/backend/main.go | 18 +++++--- nginx-golang/compose.yaml | 14 ++++-- nginx-golang/frontend/Dockerfile | 4 -- nginx-golang/{frontend => proxy}/nginx.conf | 0 9 files changed, 119 insertions(+), 34 deletions(-) create mode 100644 nginx-golang/.docker/docker-compose.yaml create mode 100644 nginx-golang/backend/go.mod create mode 100644 nginx-golang/backend/go.sum delete mode 100644 nginx-golang/frontend/Dockerfile rename nginx-golang/{frontend => proxy}/nginx.conf (100%) diff --git a/nginx-golang/.docker/docker-compose.yaml b/nginx-golang/.docker/docker-compose.yaml new file mode 100644 index 0000000..f09c393 --- /dev/null +++ b/nginx-golang/.docker/docker-compose.yaml @@ -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 diff --git a/nginx-golang/README.md b/nginx-golang/README.md index 943fa62..b4d0853 100644 --- a/nginx-golang/README.md +++ b/nginx-golang/README.md @@ -1,5 +1,5 @@ ## Compose sample application -### NGINX proxy with GO backend +### NGINX proxy with Go backend Project structure: ``` @@ -8,25 +8,32 @@ Project structure: โ”‚ย ย  โ”œโ”€โ”€ Dockerfile โ”‚ย ย  โ””โ”€โ”€ main.go โ”œโ”€โ”€ compose.yaml -โ”œโ”€โ”€ frontend -โ”‚ย ย  โ”œโ”€โ”€ Dockerfile +โ”œโ”€โ”€ proxy โ”‚ย ย  โ””โ”€โ”€ nginx.conf โ””โ”€โ”€ README.md ``` -[_compose.yaml_](compose.yaml) +[`compose.yaml`](compose.yaml) ``` services: - frontend: - build: frontend + 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: 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. 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 ... Successfully built 4b24f27138cc -Successfully tagged nginx-golang_frontend: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`. +Successfully tagged nginx-golang_proxy:latest Creating nginx-golang_backend_1 ... done -Creating nginx-golang_frontend_1 ... done +Creating nginx-golang_proxy_1 ... done ``` ## Expected result Listing containers must show two containers running and the port mapping as below: ``` -$ docker ps -CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES -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 -56f929c240a0 nginx-golang_backend "/usr/local/bin/backโ€ฆ" 53 seconds ago Up 53 seconds nginx-golang_backend_1 +$ docker compose ps +NAME COMMAND SERVICE STATUS PORTS +nginx-golang-backend-1 "/code/bin/backend" backend running +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: @@ -77,3 +83,14 @@ Stop and remove the containers ``` $ 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 +``` diff --git a/nginx-golang/backend/Dockerfile b/nginx-golang/backend/Dockerfile index 949f8a3..6f794aa 100644 --- a/nginx-golang/backend/Dockerfile +++ b/nginx-golang/backend/Dockerfile @@ -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 <