Skip to content

Commit f329300

Browse files
milasglours
andauthored
nginx-golang-mysql: add dev envs support (#274)
* Add Docker Desktop Development Environments config * Use nginx image with read-only bind mount instead of building a custom image * Upgrade Go dependencies Co-authored-by: Guillaume Lours <[email protected]> Signed-off-by: Milas Bowman <[email protected]>
1 parent 111c55d commit f329300

File tree

8 files changed

+158
-43
lines changed

8 files changed

+158
-43
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
services:
2+
backend:
3+
build:
4+
context: backend
5+
target: dev-envs
6+
volumes:
7+
- /var/run/docker.sock:/var/run/docker.sock
8+
secrets:
9+
- db-password
10+
depends_on:
11+
db:
12+
condition: service_healthy
13+
14+
db:
15+
image: mariadb:10-focal
16+
command: '--default-authentication-plugin=mysql_native_password'
17+
restart: always
18+
healthcheck:
19+
test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1", "--silent"]
20+
interval: 3s
21+
retries: 5
22+
start_period: 30s
23+
secrets:
24+
- db-password
25+
volumes:
26+
- db-data:/var/lib/mysql
27+
environment:
28+
- MYSQL_DATABASE=example
29+
- MYSQL_ROOT_PASSWORD_FILE=/run/secrets/db-password
30+
expose:
31+
- 3306
32+
33+
proxy:
34+
image: nginx
35+
volumes:
36+
- type: bind
37+
source: ./proxy/nginx.conf
38+
target: /etc/nginx/conf.d/default.conf
39+
read_only: true
40+
ports:
41+
- 80:80
42+
depends_on:
43+
- backend
44+
45+
volumes:
46+
db-data:
47+
48+
secrets:
49+
db-password:
50+
file: db/password.txt

nginx-golang-mysql/README.md

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
11
## Compose sample application
2-
### Go server with an Nginx proxy and a MySQL database
2+
### Go server with an Nginx proxy and a MariaDB/MySQL database
33

44
Project structure:
55
```
66
.
77
├── backend
88
│   ├── Dockerfile
99
│   ├── go.mod
10+
│   ├── go.sum
1011
│   └── main.go
1112
├── db
1213
│   └── password.txt
13-
├── compose.yaml
1414
├── proxy
15-
│   ── conf
16-
│   └── Dockerfile
15+
│   ── nginx.conf
16+
── compose.yaml
1717
└── README.md
1818
```
1919

2020
[_compose.yaml_](compose.yaml)
21-
```
21+
```yaml
2222
services:
2323
backend:
24-
build: backend
24+
build:
25+
context: backend
26+
target: builder
2527
...
2628
db:
2729
# We use a mariadb image which supports both amd64 & arm64 architecture
28-
image: mariadb:10.6.4-focal
30+
image: mariadb:10-focal
2931
# If you really want to use MySQL, uncomment the following line
30-
#image: mysql:8.0.27
32+
#image: mysql:8
3133
...
3234
proxy:
33-
build: proxy
35+
image: nginx
36+
volumes:
37+
- type: bind
38+
source: ./proxy/nginx.conf
39+
target: /etc/nginx/conf.d/default.conf
40+
read_only: true
3441
ports:
3542
- 80:80
3643
...
@@ -42,11 +49,11 @@ Make sure port 80 on the host is not already being in use.
4249
> ℹ️ **_INFO_**
4350
> For compatibility purpose between `AMD64` and `ARM64` architecture, we use a MariaDB as database instead of MySQL.
4451
> You still can use the MySQL image by uncommenting the following line in the Compose file
45-
> `#image: mysql:8.0.27`
52+
> `#image: mysql:8`
4653
4754
## Deploy with docker compose
4855

49-
```
56+
```shell
5057
$ docker compose up -d
5158
Creating network "nginx-golang-mysql_default" with the default driver
5259
Building backend
@@ -64,24 +71,33 @@ Creating nginx-golang-mysql_proxy_1 ... done
6471
## Expected result
6572

6673
Listing containers must show three containers running and the port mapping as below:
67-
```
68-
$ docker ps
69-
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
70-
8906b14c5ad1 nginx-golang-mysql_proxy "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 0.0.0.0:80->80/tcp nginx-golang-mysq
71-
l_proxy_1
72-
13e0e0a7715a nginx-golang-mysql_backend "/server" 2 minutes ago Up 2 minutes 8000/tcp nginx-golang-mysq
73-
l_backend_1
74-
ca8c5975d205 mysql:5.7 "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 3306/tcp, 33060/tcp nginx-golang-mysq
74+
```shell
75+
$ docker compose ps
76+
NAME COMMAND SERVICE STATUS PORTS
77+
nginx-golang-mysql-backend-1 "/code/bin/backend" backend running
78+
nginx-golang-mysql-db-1 "docker-entrypoint.s…" db running (healthy) 3306/tcp
79+
nginx-golang-mysql-proxy-1 "/docker-entrypoint.…" proxy running 0.0.0.0:80->80/tcp
7580
l_db_1
7681
```
7782

7883
After the application starts, navigate to `http://localhost:80` in your web browser or run:
79-
```
84+
```shell
8085
$ curl localhost:80
8186
["Blog post #0","Blog post #1","Blog post #2","Blog post #3","Blog post #4"]
8287
```
8388

8489
Stop and remove the containers
85-
```
90+
```shell
8691
$ docker compose down
8792
```
93+
94+
## Use with Docker Development Environments
95+
96+
You can use this sample with the Dev Environments feature of Docker Desktop.
97+
98+
![Screenshot of creating a Dev Environment in Docker Desktop](../dev-envs.png)
99+
100+
To develop directly on the services inside containers, use the HTTPS Git url of the sample:
101+
```
102+
https://github.com/docker/awesome-compose/tree/master/nginx-golang-mysql
103+
```
Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,41 @@
1-
FROM golang:1.13-alpine AS build
2-
WORKDIR /go/src/github.com/org/repo
1+
# syntax=docker/dockerfile:1.4
2+
FROM --platform=$BUILDPLATFORM golang:1.18-alpine AS builder
3+
4+
WORKDIR /code
5+
6+
ENV CGO_ENABLED 0
7+
ENV GOPATH /go
8+
ENV GOCACHE /go-build
9+
10+
COPY go.mod go.sum ./
11+
RUN --mount=type=cache,target=/go/pkg/mod/cache \
12+
go mod download
13+
314
COPY . .
415

5-
RUN go build -o server .
16+
RUN --mount=type=cache,target=/go/pkg/mod/cache \
17+
--mount=type=cache,target=/go-build \
18+
go build -o bin/backend main.go
19+
20+
CMD ["/code/bin/backend"]
21+
22+
FROM builder AS dev-envs
23+
24+
RUN <<EOF
25+
apk update
26+
apk add git
27+
EOF
28+
29+
RUN <<EOF
30+
addgroup -S docker
31+
adduser -S --shell /bin/bash --ingroup docker vscode
32+
EOF
33+
34+
# install Docker tools (cli, buildx, compose)
35+
COPY --from=gloursdocker/docker / /
36+
37+
CMD ["go", "run", "main.go"]
638

7-
FROM alpine:3.12
8-
EXPOSE 8000
9-
COPY --from=build /go/src/github.com/org/repo/server /server
10-
CMD ["/server"]
39+
FROM scratch
40+
COPY --from=builder /code/bin/backend /usr/local/bin/backend
41+
CMD ["/usr/local/bin/backend"]

nginx-golang-mysql/backend/go.mod

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
module github.com/org/repo
1+
module github.com/docker/awesome-compose/nginx-golang-mysql/backend
22

3-
go 1.13
3+
go 1.18
44

55
require (
6-
github.com/go-sql-driver/mysql v1.3.0
7-
github.com/gorilla/context v1.1.1
8-
github.com/gorilla/handlers v1.3.0
9-
github.com/gorilla/mux v1.6.2
6+
github.com/go-sql-driver/mysql v1.6.0
7+
github.com/gorilla/handlers v1.5.1
8+
github.com/gorilla/mux v1.8.0
109
)
10+
11+
require github.com/felixge/httpsnoop v1.0.1 // indirect

nginx-golang-mysql/backend/go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
2+
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
3+
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
4+
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
5+
github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
6+
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
7+
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
8+
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=

nginx-golang-mysql/compose.yaml

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
services:
22
backend:
3-
build: backend
3+
build:
4+
context: backend
5+
target: builder
46
secrets:
57
- db-password
68
depends_on:
79
db:
810
condition: service_healthy
11+
912
db:
1013
# We use a mariadb image which supports both amd64 & arm64 architecture
11-
image: mariadb:10.6.4-focal
14+
image: mariadb:10-focal
1215
# If you really want to use MySQL, uncomment the following line
13-
#image: mysql:8.0.27
16+
#image: mysql:8
1417
command: '--default-authentication-plugin=mysql_native_password'
1518
restart: always
1619
healthcheck:
17-
test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1", "--silent"]
20+
test: ['CMD-SHELL', 'mysqladmin ping -h 127.0.0.1 --password="$$(cat /run/secrets/db-password)" --silent']
1821
interval: 3s
1922
retries: 5
2023
start_period: 30s
@@ -27,14 +30,22 @@ services:
2730
- MYSQL_ROOT_PASSWORD_FILE=/run/secrets/db-password
2831
expose:
2932
- 3306
33+
3034
proxy:
31-
build: proxy
35+
image: nginx
36+
volumes:
37+
- type: bind
38+
source: ./proxy/nginx.conf
39+
target: /etc/nginx/conf.d/default.conf
40+
read_only: true
3241
ports:
3342
- 80:80
3443
depends_on:
3544
- backend
45+
3646
volumes:
3747
db-data:
48+
3849
secrets:
3950
db-password:
4051
file: db/password.txt

nginx-golang-mysql/proxy/Dockerfile

Lines changed: 0 additions & 2 deletions
This file was deleted.

nginx-golang-mysql/proxy/conf renamed to nginx-golang-mysql/proxy/nginx.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ server {
22
listen 80;
33
server_name localhost;
44
location / {
5-
proxy_pass http://backend:8000;
5+
proxy_pass http://backend:8000;
6+
proxy_http_version 1.1;
67
}
7-
88
}

0 commit comments

Comments
 (0)