From f122b52ce620bcc768758d3bf1d6855a6edc86d1 Mon Sep 17 00:00:00 2001 From: Milas Bowman Date: Mon, 11 Jul 2022 15:14:28 -0400 Subject: [PATCH] nginx-golang-mysql: add dev envs support * Add Docker Desktop Development Environments config * Use nginx image with read-only bind mount instead of building a custom image * Upgrade Go dependencies Signed-off-by: Milas Bowman --- .../.docker/docker-compose.yaml | 50 +++++++++++++++++++ nginx-golang-mysql/backend/Dockerfile | 45 ++++++++++++++--- nginx-golang-mysql/backend/go.mod | 13 ++--- nginx-golang-mysql/backend/go.sum | 8 +++ nginx-golang-mysql/compose.yaml | 21 ++++++-- nginx-golang-mysql/proxy/Dockerfile | 2 - nginx-golang-mysql/proxy/{conf => nginx.conf} | 4 +- 7 files changed, 121 insertions(+), 22 deletions(-) create mode 100644 nginx-golang-mysql/.docker/docker-compose.yaml create mode 100644 nginx-golang-mysql/backend/go.sum delete mode 100755 nginx-golang-mysql/proxy/Dockerfile rename nginx-golang-mysql/proxy/{conf => nginx.conf} (50%) diff --git a/nginx-golang-mysql/.docker/docker-compose.yaml b/nginx-golang-mysql/.docker/docker-compose.yaml new file mode 100644 index 0000000..2b04dd0 --- /dev/null +++ b/nginx-golang-mysql/.docker/docker-compose.yaml @@ -0,0 +1,50 @@ +services: + backend: + build: + context: backend + target: builder + volumes: + - /var/run/docker.sock:/var/run/docker.sock + secrets: + - db-password + depends_on: + db: + condition: service_healthy + + db: + image: mariadb:10-focal + command: '--default-authentication-plugin=mysql_native_password' + restart: always + healthcheck: + test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1", "--silent"] + interval: 3s + retries: 5 + start_period: 30s + secrets: + - db-password + volumes: + - db-data:/var/lib/mysql + environment: + - MYSQL_DATABASE=example + - MYSQL_ROOT_PASSWORD_FILE=/run/secrets/db-password + expose: + - 3306 + + 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-mysql/backend/Dockerfile b/nginx-golang-mysql/backend/Dockerfile index 36e9525..6f794aa 100755 --- a/nginx-golang-mysql/backend/Dockerfile +++ b/nginx-golang-mysql/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 <