From 7f5179ea3e65cf7ed72fc1a303e558df13c2c70d Mon Sep 17 00:00:00 2001 From: Milas Bowman Date: Tue, 12 Jul 2022 04:08:02 -0400 Subject: [PATCH] nginx-golang-postgres: add dev envs config (#275) * Add Docker Desktop Development Environments config * Upgrade to Go 1.18 * Replace nginx build with image + read-only bind mount Signed-off-by: Milas Bowman --- .../.docker/docker-compose.yaml | 50 +++++++++++++++++++ nginx-golang-postgres/README.md | 48 ++++++++++++------ nginx-golang-postgres/backend/Dockerfile | 45 ++++++++++++++--- nginx-golang-postgres/backend/go.mod | 7 +-- nginx-golang-postgres/backend/go.sum | 8 +++ nginx-golang-postgres/compose.yaml | 25 ++++++++-- nginx-golang-postgres/proxy/Dockerfile | 2 - .../proxy/{conf => nginx.conf} | 3 +- 8 files changed, 156 insertions(+), 32 deletions(-) create mode 100644 nginx-golang-postgres/.docker/docker-compose.yaml create mode 100644 nginx-golang-postgres/backend/go.sum delete mode 100755 nginx-golang-postgres/proxy/Dockerfile rename nginx-golang-postgres/proxy/{conf => nginx.conf} (50%) diff --git a/nginx-golang-postgres/.docker/docker-compose.yaml b/nginx-golang-postgres/.docker/docker-compose.yaml new file mode 100644 index 0000000..0176bb1 --- /dev/null +++ b/nginx-golang-postgres/.docker/docker-compose.yaml @@ -0,0 +1,50 @@ +services: + backend: + build: + context: backend + target: dev-envs + volumes: + - /var/run/docker.sock:/var/run/docker.sock + secrets: + - db-password + depends_on: + db: + condition: service_healthy + + db: + image: postgres + restart: always + user: postgres + secrets: + - db-password + volumes: + - db-data:/var/lib/postgresql/data + environment: + - POSTGRES_DB=example + - POSTGRES_PASSWORD_FILE=/run/secrets/db-password + expose: + - 5432 + healthcheck: + test: [ "CMD", "pg_isready" ] + interval: 10s + timeout: 5s + retries: 5 + + 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 + +volumes: + db-data: + +secrets: + db-password: + file: db/password.txt diff --git a/nginx-golang-postgres/README.md b/nginx-golang-postgres/README.md index 4630c76..87170bc 100644 --- a/nginx-golang-postgres/README.md +++ b/nginx-golang-postgres/README.md @@ -7,29 +7,36 @@ Project structure: ├── backend │   ├── Dockerfile │   ├── go.mod +│   ├── go.sum │   └── main.go ├── db │   └── password.txt ├── compose.yaml ├── proxy -│   ├── conf -│   └── Dockerfile +│   └── nginx.conf └── README.md ``` [_compose.yaml_](compose.yaml) -``` +```shell services: backend: - build: backend + build: + context: backend + target: builder ... db: image: postgres ... proxy: - build: proxy + image: nginx + volumes: + - type: bind + source: ./proxy/nginx.conf + target: /etc/nginx/conf.d/default.conf + read_only: true ports: - - 80:80 + - 80:80 ... ``` The compose file defines an application with three services `proxy`, `backend` and `db`. @@ -38,7 +45,7 @@ Make sure port 80 on the host is not already being in use. ## Deploy with docker compose -``` +```shell $ docker compose up -d Creating network "nginx-golang-postgres_default" with the default driver Pulling db (postgres:)... @@ -55,21 +62,32 @@ Creating nginx-golang-postgres_proxy_1 ... done ## Expected result Listing containers must show three containers running and the port mapping as below: -``` -$ docker ps -CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES -5e3ecd0289c0 nginx-golang-postgres_proxy "nginx -g 'daemon of…" 48 seconds ago Up 48 seconds 0.0.0.0:80->80/tcp nginx-golang-postgres_proxy_1 -ffa1410b1c8a nginx-golang-postgres_backend "/server" 49 seconds ago Up 48 seconds 8000/tcp nginx-golang-postgres_backend_1 -e63be7db7cbc postgres "docker-entrypoint.s…" 49 seconds ago Up 49 seconds 5432/tcp nginx-golang-postgres_db_1 +```shell +$ docker compose ps +NAME COMMAND SERVICE STATUS PORTS +nginx-golang-postgres-backend-1 "/code/bin/backend" backend running +nginx-golang-postgres-db-1 "docker-entrypoint.s…" db running (healthy) 5432/tcp +nginx-golang-postgres-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: -``` +```shell $ curl localhost:80 ["Blog post #0","Blog post #1","Blog post #2","Blog post #3","Blog post #4"] ``` Stop and remove the containers -``` +```shell $ 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-postgres +``` diff --git a/nginx-golang-postgres/backend/Dockerfile b/nginx-golang-postgres/backend/Dockerfile index 36e9525..6f794aa 100755 --- a/nginx-golang-postgres/backend/Dockerfile +++ b/nginx-golang-postgres/backend/Dockerfile @@ -1,10 +1,41 @@ -FROM golang:1.13-alpine AS build -WORKDIR /go/src/github.com/org/repo +# syntax=docker/dockerfile:1.4 +FROM --platform=$BUILDPLATFORM golang:1.18-alpine AS builder + +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 go build -o server . +RUN --mount=type=cache,target=/go/pkg/mod/cache \ + --mount=type=cache,target=/go-build \ + go build -o bin/backend main.go -FROM alpine:3.12 -EXPOSE 8000 -COPY --from=build /go/src/github.com/org/repo/server /server -CMD ["/server"] +CMD ["/code/bin/backend"] + +FROM builder as dev-envs + +RUN <