Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions nginx-golang/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.unikraft/
27 changes: 27 additions & 0 deletions nginx-golang/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Compose: Nginx and Golang

This is a [Compose](https://unikraft.cloud/docs/guides/features/compose/)-based example of a [Go]() HTTP server and an [Nginx](https://nginx.org/en/) proxy.
It is imported from the [`awesome-compose` repository](https://github.com/docker/awesome-compose).

To run Nginx and Golang with Compose on Unikraft Cloud, first [install the `kraft` CLI tool](https://unikraft.org/docs/cli).
Then clone this examples repository and `cd` into this directory, and invoke:

```console
kraft cloud compose up
```

The command will deploy files in the current directory.

After deploying, you can query the service using the provided URL for Nginx.
Golang is only running in the backend with a public URL.
Find out the Nginx provided URL by using:

```console
kraft cloud instance list
```

## Learn more

- [The Compose Specification](https://github.com/compose-spec/compose-spec/blob/main/spec.md)
- [Unikraft Cloud's Documentation](https://unikraft.cloud/docs/)
- [Building `Dockerfile` Images with `Buildkit`](https://unikraft.org/guides/building-dockerfile-images-with-buildkit)
25 changes: 25 additions & 0 deletions nginx-golang/backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# syntax=docker/dockerfile:1.4
FROM --platform=$BUILDPLATFORM golang:1.23-bookworm AS builder

WORKDIR /app

#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 --mount=type=cache,target=/go/pkg/mod/cache \
--mount=type=cache,target=/go-build \
go build \
-buildmode=pie \
-ldflags "-linkmode external -extldflags -static-pie" \
-tags netgo \
-o bin/backend main.go

FROM scratch
COPY --from=builder /app/bin/backend /usr/local/bin/backend
12 changes: 12 additions & 0 deletions nginx-golang/backend/Kraftfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
spec: v0.6

runtime: base:latest

labels:
cloud.unikraft.v1.instances/scale_to_zero.policy: "off"
cloud.unikraft.v1.instances/scale_to_zero.stateful: "false"
cloud.unikraft.v1.instances/scale_to_zero.cooldown_time_ms: 1000

rootfs: ./Dockerfile

cmd: ["/usr/local/bin/backend"]
5 changes: 5 additions & 0 deletions nginx-golang/backend/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/docker/awesome-compose/nginx-golang/backend

go 1.18

require github.com/go-chi/chi/v5 v5.0.7
2 changes: 2 additions & 0 deletions nginx-golang/backend/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8=
github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
35 changes: 35 additions & 0 deletions nginx-golang/backend/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"fmt"
"log"
"net/http"

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)

func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(
w, `
o. .o _ _ __ _
Oo Oo ___ (_) | __ __ __ _ ' _) :_
oO oO ' _ '| | |/ / _)' _' | |_| _)
oOo oOO| | | | | (| | | (_) | _) :_
OoOoO ._, ._:_:_,\_._, .__,_:_, \___)


Hello from Unikraft!

`,
)
}

func main() {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Get("/", handler)

fmt.Println("Go backend started!")
log.Fatal(http.ListenAndServe(":80", r))
}
10 changes: 10 additions & 0 deletions nginx-golang/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
services:
proxy:
build: ./proxy
ports:
- 443:80
depends_on:
- backend

backend:
build: ./backend
12 changes: 12 additions & 0 deletions nginx-golang/proxy/Kraftfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
spec: v0.6

runtime: nginx:latest

labels:
cloud.unikraft.v1.instances/scale_to_zero.policy: "on"
cloud.unikraft.v1.instances/scale_to_zero.stateful: "false"
cloud.unikraft.v1.instances/scale_to_zero.cooldown_time_ms: 1000

rootfs: ./rootfs

cmd: ["/usr/bin/nginx", "-c", "/etc/nginx/nginx.conf"]
17 changes: 17 additions & 0 deletions nginx-golang/proxy/rootfs/etc/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
worker_processes 1;
daemon off;
master_process off;
user root root;

events {
worker_connections 64;
}

http {
server {
listen 80;
location / {
proxy_pass http://nginx-golang-backend.internal:80;
}
}
}