From 993646bb4a187e3110f4705e03b1d3cf43f3bb4f Mon Sep 17 00:00:00 2001 From: Razvan Deaconescu Date: Thu, 26 Sep 2024 22:51:41 +0300 Subject: [PATCH 1/3] feat(examples): Add initial version of nginx-golang Add initial version of `nginx-golang` from the `awesome-compose` repository. Signed-off-by: Razvan Deaconescu --- nginx-golang/README.md | 85 +++++++++++++++++++++++++++++++++ nginx-golang/backend/Dockerfile | 41 ++++++++++++++++ nginx-golang/backend/go.mod | 5 ++ nginx-golang/backend/go.sum | 2 + nginx-golang/backend/main.go | 38 +++++++++++++++ nginx-golang/compose.yaml | 17 +++++++ nginx-golang/proxy/nginx.conf | 6 +++ 7 files changed, 194 insertions(+) create mode 100644 nginx-golang/README.md create mode 100644 nginx-golang/backend/Dockerfile create mode 100644 nginx-golang/backend/go.mod create mode 100644 nginx-golang/backend/go.sum create mode 100644 nginx-golang/backend/main.go create mode 100644 nginx-golang/compose.yaml create mode 100644 nginx-golang/proxy/nginx.conf diff --git a/nginx-golang/README.md b/nginx-golang/README.md new file mode 100644 index 00000000..57debe8a --- /dev/null +++ b/nginx-golang/README.md @@ -0,0 +1,85 @@ +## Compose sample application +### NGINX proxy with Go backend + +Project structure: +``` +. +├── backend +│   ├── Dockerfile +│   └── main.go +├── compose.yaml +├── proxy +│   └── nginx.conf +└── README.md +``` + +[`compose.yaml`](compose.yaml) +``` +services: + 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 + + backend: + build: + context: backend + target: builder +``` +The compose file defines an application with two services `proxy` and `backend`. +When deploying the application, docker compose maps port 80 of the frontend service container to the same port of the host as specified in the file. +Make sure port 80 on the host is not already in use. + +## Deploy with docker compose + +``` +$ docker compose up -d +Creating network "nginx-golang_default" with the default driver +Building backend +Step 1/7 : FROM golang:1.13 AS build +1.13: Pulling from library/golang +... +Successfully built 4b24f27138cc +Successfully tagged nginx-golang_proxy:latest +Creating nginx-golang_backend_1 ... done +Creating nginx-golang_proxy_1 ... done +``` + +## Expected result + +Listing containers must show two containers running and the port mapping as below: +``` +$ docker compose ps +NAME COMMAND SERVICE STATUS PORTS +nginx-golang-backend-1 "/code/bin/backend" backend running +nginx-golang-proxy-1 "/docker-entrypoint.…" proxy running 0.0.0.0:80->80/tcp +``` + +After the application starts, navigate to `http://localhost:80` in your web browser or run: +``` +$ curl localhost:80 + + ## . + ## ## ## == + ## ## ## ## ## === +/"""""""""""""""""\___/ === +{ / ===- +\______ O __/ + \ \ __/ + \____\_______/ + + +Hello from Docker! +``` + +Stop and remove the containers +``` +$ docker compose down +``` diff --git a/nginx-golang/backend/Dockerfile b/nginx-golang/backend/Dockerfile new file mode 100644 index 00000000..6f794aab --- /dev/null +++ b/nginx-golang/backend/Dockerfile @@ -0,0 +1,41 @@ +# 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 --mount=type=cache,target=/go/pkg/mod/cache \ + --mount=type=cache,target=/go-build \ + go build -o bin/backend main.go + +CMD ["/code/bin/backend"] + +FROM builder as dev-envs + +RUN < Date: Thu, 26 Sep 2024 23:29:18 +0300 Subject: [PATCH 2/3] fix(nginx-golang): Update files for Unikraft Cloud Update setup for using Unikraft Cloud (Compose) with `nginx-golang`: - Add `Kraftfile`s for KraftCloud. - Update `Dockerfile` for `backend` to provide a static PIE binary. - Make `nginx.conf` work. Move to `/etc/nginx/`. - Update `compose.yaml` for Unikraft Cloud. - Update `backend/main.go` to print Unikraft logo. Signed-off-by: Razvan Deaconescu --- nginx-golang/.dockerignore | 1 + nginx-golang/backend/Dockerfile | 34 +++++-------------- nginx-golang/backend/Kraftfile | 12 +++++++ nginx-golang/backend/main.go | 15 ++++---- nginx-golang/compose.yaml | 13 ++----- nginx-golang/proxy/Kraftfile | 12 +++++++ nginx-golang/proxy/nginx.conf | 6 ---- .../proxy/rootfs/etc/nginx/nginx.conf | 17 ++++++++++ 8 files changed, 60 insertions(+), 50 deletions(-) create mode 100644 nginx-golang/.dockerignore create mode 100644 nginx-golang/backend/Kraftfile create mode 100644 nginx-golang/proxy/Kraftfile delete mode 100644 nginx-golang/proxy/nginx.conf create mode 100644 nginx-golang/proxy/rootfs/etc/nginx/nginx.conf diff --git a/nginx-golang/.dockerignore b/nginx-golang/.dockerignore new file mode 100644 index 00000000..1b8123b9 --- /dev/null +++ b/nginx-golang/.dockerignore @@ -0,0 +1 @@ +/.unikraft/ diff --git a/nginx-golang/backend/Dockerfile b/nginx-golang/backend/Dockerfile index 6f794aab..d9170308 100644 --- a/nginx-golang/backend/Dockerfile +++ b/nginx-golang/backend/Dockerfile @@ -1,9 +1,9 @@ # syntax=docker/dockerfile:1.4 -FROM --platform=$BUILDPLATFORM golang:1.18-alpine AS builder +FROM --platform=$BUILDPLATFORM golang:1.23-bookworm AS builder -WORKDIR /code +WORKDIR /app -ENV CGO_ENABLED 0 +#ENV CGO_ENABLED 0 ENV GOPATH /go ENV GOCACHE /go-build @@ -15,27 +15,11 @@ COPY . . RUN --mount=type=cache,target=/go/pkg/mod/cache \ --mount=type=cache,target=/go-build \ - go build -o bin/backend main.go - -CMD ["/code/bin/backend"] - -FROM builder as dev-envs - -RUN < Date: Thu, 26 Sep 2024 23:39:39 +0300 Subject: [PATCH 3/3] fix(nginx-golang): Update README Update the README of the `nginx-golang` Compose-based example with instructions for Unikraft Cloud. Signed-off-by: Razvan Deaconescu --- nginx-golang/README.md | 92 ++++++++---------------------------------- 1 file changed, 17 insertions(+), 75 deletions(-) diff --git a/nginx-golang/README.md b/nginx-golang/README.md index 57debe8a..fbe764cd 100644 --- a/nginx-golang/README.md +++ b/nginx-golang/README.md @@ -1,85 +1,27 @@ -## Compose sample application -### NGINX proxy with Go backend +# Compose: Nginx and Golang -Project structure: -``` -. -├── backend -│   ├── Dockerfile -│   └── main.go -├── compose.yaml -├── proxy -│   └── nginx.conf -└── README.md -``` +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). -[`compose.yaml`](compose.yaml) -``` -services: - 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 +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: - backend: - build: - context: backend - target: builder +```console +kraft cloud compose up ``` -The compose file defines an application with two services `proxy` and `backend`. -When deploying the application, docker compose maps port 80 of the frontend service container to the same port of the host as specified in the file. -Make sure port 80 on the host is not already in use. -## Deploy with docker compose +The command will deploy files in the current directory. -``` -$ docker compose up -d -Creating network "nginx-golang_default" with the default driver -Building backend -Step 1/7 : FROM golang:1.13 AS build -1.13: Pulling from library/golang -... -Successfully built 4b24f27138cc -Successfully tagged nginx-golang_proxy:latest -Creating nginx-golang_backend_1 ... done -Creating nginx-golang_proxy_1 ... done -``` - -## Expected result +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: -Listing containers must show two containers running and the port mapping as below: -``` -$ docker compose ps -NAME COMMAND SERVICE STATUS PORTS -nginx-golang-backend-1 "/code/bin/backend" backend running -nginx-golang-proxy-1 "/docker-entrypoint.…" proxy running 0.0.0.0:80->80/tcp +```console +kraft cloud instance list ``` -After the application starts, navigate to `http://localhost:80` in your web browser or run: -``` -$ curl localhost:80 - - ## . - ## ## ## == - ## ## ## ## ## === -/"""""""""""""""""\___/ === -{ / ===- -\______ O __/ - \ \ __/ - \____\_______/ - - -Hello from Docker! -``` +## Learn more -Stop and remove the containers -``` -$ docker compose down -``` +- [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)