Skip to content

Commit c338719

Browse files
committed
feat(examples): Add nginx-golang compose example
This example is dervied from the nginx-golang docker/awesome-compose example. Signed-off-by: Luca Seritan <luca.seritan@gmail.com>
1 parent 3dd4c28 commit c338719

File tree

9 files changed

+283
-0
lines changed

9 files changed

+283
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: examples/nginx-golang
2+
3+
on:
4+
repository_dispatch:
5+
types: [core_merge]
6+
7+
workflow_dispatch:
8+
9+
schedule:
10+
- cron: '0 0 * * *' # Everyday at 12AM
11+
12+
push:
13+
branches: [main]
14+
paths:
15+
- 'examples/compose/nginx-golang/**'
16+
- '.github/workflows/examples-nginx-golang.yaml'
17+
- '!examples/compose/nginx-golang/README.md'
18+
19+
pull_request:
20+
types: [opened, synchronize, reopened]
21+
branches: [main]
22+
paths:
23+
- 'examples/compose/nginx-golang/**'
24+
- '.github/workflows/examples-nginx-golang.yaml'
25+
- '!examples/compose/nginx-golang/README.md'
26+
27+
jobs:
28+
up:
29+
runs-on: ubuntu-latest
30+
31+
steps:
32+
- uses: actions/checkout@v3
33+
34+
- name: Create Docker bridge network
35+
run: docker network create my-bridge-network
36+
37+
- name: Check IP address
38+
run: ip a s
39+
40+
- name: Compose up nginx-golang
41+
run: |
42+
set -xe
43+
44+
curl --proto '=https' --tlsv1.2 -sSf https://get.kraftkit.sh > script.sh
45+
chmod +x script.sh
46+
sudo ./script.sh -y
47+
48+
sudo kraft compose up -d
49+
working-directory: examples/compose/nginx-golang
50+
51+
- name: Compose up nginx-golang
52+
uses: unikraft/kraftkit@staging
53+
with:
54+
loglevel: debug
55+
workdir: examples/compose/nginx-golang
56+
runtimedir: /github/workspace/.kraftkit
57+
run: |
58+
set -xe
59+
60+
sudo kraft net create a
+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
## Compose sample application
2+
3+
### NGINX proxy with Go backend
4+
5+
This example was derived from the [nginx-golang docker/awesome-compose example](https://github.com/docker/awesome-compose/tree/master/nginx-golang).
6+
7+
Project structure:
8+
9+
```bash
10+
.
11+
├── backend
12+
│   ├── Kraftfile
13+
│   ├── Dockerfile
14+
│   └── main.go
15+
├── compose.yaml
16+
├── proxy
17+
│   └── nginx.conf
18+
└── README.md
19+
```
20+
21+
[`compose.yaml`](compose.yaml)
22+
23+
```yml
24+
services:
25+
proxy:
26+
image: nginx:1.25
27+
volumes:
28+
- ./proxy:/etc/nginx
29+
ports:
30+
- 8080:80
31+
depends_on:
32+
- backend
33+
networks:
34+
internal:
35+
36+
backend:
37+
build:
38+
context: backend
39+
networks:
40+
internal:
41+
ipv4_address: 172.23.0.2
42+
43+
networks:
44+
internal:
45+
ipam:
46+
config:
47+
- subnet: 172.23.0.1/16
48+
```
49+
50+
The compose file defines an application with two services `proxy` and `backend`.
51+
When deploying the application, kraft compose maps port 80 of the frontend service VM to port 8080 of the host as specified in the file.
52+
Make sure port 8080 on the host is not already in use.
53+
54+
## Deploy with kraft compose
55+
56+
```bash
57+
$ kraft compose up -d
58+
creating network nginx-golang_internal...
59+
nginx-golang_internal
60+
i building service backend...
61+
...
62+
nginx-golang-backend
63+
nginx-golang-proxy
64+
```
65+
66+
## Expected result
67+
68+
Listing VMs must show two VMs running and the port mapping as below:
69+
70+
```bash
71+
$ kraft compose ps
72+
NAME KERNEL ARGS CREATED STATUS MEM PORTS PLAT
73+
nginx-golang-backend oci://unikraft.org/base:latest /server 23 seconds ago running 64M qemu/x86_64
74+
nginx-golang-proxy oci://nginx:1.25 /usr/bin/nginx 22 seconds ago running 64M 0.0.0.0:8080->80/tcp qemu/x86_64
75+
```
76+
77+
After the application starts, navigate to `http://localhost:8080` in your web browser or run:
78+
79+
```bash
80+
$ curl localhost:8080
81+
.--------------------.
82+
( Hello from Unikraft! )
83+
'--------------------'
84+
\\
85+
\\
86+
_
87+
c'o'o .--.
88+
(| |)_/
89+
```
90+
91+
Stop and remove the VMs
92+
93+
```bash
94+
kraft compose down
95+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
FROM golang:1.21.3-bookworm AS build
2+
3+
WORKDIR /src
4+
5+
COPY ./main.go /src/server.go
6+
7+
COPY go.mod go.sum ./
8+
RUN --mount=type=cache,target=/go/pkg/mod/cache \
9+
go mod download
10+
11+
COPY . .
12+
13+
RUN set -xe; \
14+
CGO_ENABLED=1 \
15+
go build \
16+
-buildmode=pie \
17+
-ldflags "-linkmode external -extldflags '-static-pie'" \
18+
-tags netgo \
19+
-o /server server.go \
20+
;
21+
22+
FROM scratch
23+
24+
COPY --from=build /server /server
25+
COPY --from=build /lib/x86_64-linux-gnu/libc.so.6 /lib/x86_64-linux-gnu/
26+
COPY --from=build /lib64/ld-linux-x86-64.so.2 /lib64/
27+
28+
CMD ["/server"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
spec: v0.6
2+
3+
runtime: base:latest
4+
5+
rootfs: ./Dockerfile
6+
7+
cmd: ["/server"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/docker/awesome-compose/nginx-golang/backend
2+
3+
go 1.18
4+
5+
require github.com/go-chi/chi/v5 v5.0.7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8=
2+
github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
8+
"github.com/go-chi/chi/v5"
9+
"github.com/go-chi/chi/v5/middleware"
10+
)
11+
12+
func handler(w http.ResponseWriter, r *http.Request) {
13+
fmt.Fprintf(
14+
w, `
15+
.--------------------.
16+
( Hello from Unikraft! )
17+
'--------------------'
18+
\\
19+
\\
20+
_
21+
c'o'o .--.
22+
(| |)_/
23+
24+
`,
25+
)
26+
}
27+
28+
func main() {
29+
r := chi.NewRouter()
30+
r.Use(middleware.Logger)
31+
r.Get("/", handler)
32+
33+
fmt.Println("Go backend started!")
34+
log.Fatal(http.ListenAndServe(":80", r))
35+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
services:
3+
proxy:
4+
image: nginx:1.25
5+
volumes:
6+
- ./proxy:/etc/nginx
7+
ports:
8+
- 8080:80
9+
depends_on:
10+
- backend
11+
networks:
12+
internal:
13+
14+
backend:
15+
build:
16+
context: backend
17+
networks:
18+
internal:
19+
ipv4_address: 172.23.0.2
20+
21+
networks:
22+
internal:
23+
ipam:
24+
config:
25+
- subnet: 172.23.0.1/16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
worker_processes 1;
2+
daemon off;
3+
master_process off;
4+
user root root;
5+
6+
events {
7+
worker_connections 32;
8+
}
9+
10+
http {
11+
error_log stderr error;
12+
access_log off;
13+
14+
keepalive_timeout 10s;
15+
keepalive_requests 10000;
16+
send_timeout 10s;
17+
18+
server {
19+
listen 80;
20+
server_name localhost;
21+
22+
location / {
23+
proxy_pass http://172.23.0.2;
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)