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 <milas.bowman@docker.com>
This commit is contained in:
parent
9d547d23fb
commit
f122b52ce6
50
nginx-golang-mysql/.docker/docker-compose.yaml
Normal file
50
nginx-golang-mysql/.docker/docker-compose.yaml
Normal file
@ -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
|
@ -1,10 +1,41 @@
|
|||||||
FROM golang:1.13-alpine AS build
|
# syntax=docker/dockerfile:1.4
|
||||||
WORKDIR /go/src/github.com/org/repo
|
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 . .
|
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
|
CMD ["/code/bin/backend"]
|
||||||
EXPOSE 8000
|
|
||||||
COPY --from=build /go/src/github.com/org/repo/server /server
|
FROM builder as dev-envs
|
||||||
CMD ["/server"]
|
|
||||||
|
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"]
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
module github.com/org/repo
|
module github.com/docker/awesome-compose/nginx-golang-mysql/backend
|
||||||
|
|
||||||
go 1.13
|
go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/go-sql-driver/mysql v1.3.0
|
github.com/go-sql-driver/mysql v1.6.0
|
||||||
github.com/gorilla/context v1.1.1
|
github.com/gorilla/handlers v1.5.1
|
||||||
github.com/gorilla/handlers v1.3.0
|
github.com/gorilla/mux v1.8.0
|
||||||
github.com/gorilla/mux v1.6.2
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
require github.com/felixge/httpsnoop v1.0.1 // indirect
|
||||||
|
8
nginx-golang-mysql/backend/go.sum
Normal file
8
nginx-golang-mysql/backend/go.sum
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
|
||||||
|
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
|
||||||
|
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
|
||||||
|
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
||||||
|
github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
|
||||||
|
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
|
||||||
|
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
|
||||||
|
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
@ -1,20 +1,23 @@
|
|||||||
services:
|
services:
|
||||||
backend:
|
backend:
|
||||||
build: backend
|
build:
|
||||||
|
context: backend
|
||||||
|
target: builder
|
||||||
secrets:
|
secrets:
|
||||||
- db-password
|
- db-password
|
||||||
depends_on:
|
depends_on:
|
||||||
db:
|
db:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
|
|
||||||
db:
|
db:
|
||||||
# We use a mariadb image which supports both amd64 & arm64 architecture
|
# We use a mariadb image which supports both amd64 & arm64 architecture
|
||||||
image: mariadb:10.6.4-focal
|
image: mariadb:10-focal
|
||||||
# If you really want to use MySQL, uncomment the following line
|
# If you really want to use MySQL, uncomment the following line
|
||||||
#image: mysql:8.0.27
|
#image: mysql:8
|
||||||
command: '--default-authentication-plugin=mysql_native_password'
|
command: '--default-authentication-plugin=mysql_native_password'
|
||||||
restart: always
|
restart: always
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1", "--silent"]
|
test: ['CMD-SHELL', 'mysqladmin ping -h 127.0.0.1 --password="$$(cat /run/secrets/db-password)" --silent']
|
||||||
interval: 3s
|
interval: 3s
|
||||||
retries: 5
|
retries: 5
|
||||||
start_period: 30s
|
start_period: 30s
|
||||||
@ -27,14 +30,22 @@ services:
|
|||||||
- MYSQL_ROOT_PASSWORD_FILE=/run/secrets/db-password
|
- MYSQL_ROOT_PASSWORD_FILE=/run/secrets/db-password
|
||||||
expose:
|
expose:
|
||||||
- 3306
|
- 3306
|
||||||
|
|
||||||
proxy:
|
proxy:
|
||||||
build: proxy
|
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
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
db-data:
|
db-data:
|
||||||
|
|
||||||
secrets:
|
secrets:
|
||||||
db-password:
|
db-password:
|
||||||
file: db/password.txt
|
file: db/password.txt
|
||||||
|
@ -1,2 +0,0 @@
|
|||||||
FROM nginx:1.13-alpine
|
|
||||||
COPY conf /etc/nginx/conf.d/default.conf
|
|
@ -2,7 +2,7 @@ server {
|
|||||||
listen 80;
|
listen 80;
|
||||||
server_name localhost;
|
server_name localhost;
|
||||||
location / {
|
location / {
|
||||||
proxy_pass http://backend:8000;
|
proxy_pass http://backend:8000;
|
||||||
|
proxy_http_version 1.1;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user