Skip to content

Commit

Permalink
feat(examples): Introduce Caddy as example
Browse files Browse the repository at this point in the history
Introduce Caddy as binary compatibility run. Build Caddy as static PIE
application using `Dockerfile`. Then run it with the `base` kernel
images from `../../kernels/`.

Add typical files for a bincompat app:

* `Kraftfile`: build / run rules, including pulling the `base` image
* `Dockerfile`: filesystem, including binary and libraries
* `Makefile`: used to generate the root filesystem from the
  `Dockerfile`
  rules
* `README.md`: instructions to set up, build and run the application
* `config.yaml`: configuration file to generate scripts to the
  application
* `hosts`, `Caddyfile`: required Caddy configuration files

`config.yaml` is used to generate run scripts using the
`../../utils/bincompat/generate.py` script.

The kernels in `../../kernels` are generated by running the
`../../utils/bincompat/base-build-all.sh` script while inside the
`../../library/base/` directory.

Signed-off-by: Razvan Deaconescu <[email protected]>
  • Loading branch information
razvand committed Dec 3, 2023
1 parent c434a7e commit b8eeda8
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 0 deletions.
6 changes: 6 additions & 0 deletions examples/caddy-2.7/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/rootfs/
/rootfs.cpio
/run-qemu*
/run-fc*
/kraft-run-*
/fc*.json
3 changes: 3 additions & 0 deletions examples/caddy-2.7/Caddyfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:2015

respond "Hello, World!"
34 changes: 34 additions & 0 deletions examples/caddy-2.7/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
FROM --platform=linux/x86_64 golang:1.21.4-bookworm AS build

# Inspired from: https://github.com/caddyserver/caddy-docker/blob/master/2.7/builder/Dockerfile

RUN set -xe; apt-get update ; \
apt-get install -y --no-install-recommends \
ca-certificates \
git \
libcap2-bin \
;

ENV XCADDY_VERSION v0.3.5
# Configures xcaddy to build with this version of Caddy
ENV CADDY_VERSION v2.7.5
# Configures xcaddy to not clean up post-build (unnecessary in a container)
ENV XCADDY_SKIP_CLEANUP 1
# Sets capabilities for output caddy binary to be able to bind to privileged ports
ENV XCADDY_SETCAP 1

RUN set -xe; \
wget -O /tmp/xcaddy.tar.gz "https://github.com/caddyserver/xcaddy/releases/download/v0.3.5/xcaddy_0.3.5_linux_amd64.tar.gz"; \
tar x -z -f /tmp/xcaddy.tar.gz -C /usr/bin xcaddy; \
rm -f /tmp/xcaddy.tar.gz; \
chmod +x /usr/bin/xcaddy;

ENV CGO_ENABLED=1
ENV XCADDY_GO_BUILD_FLAGS "-buildmode=pie -ldflags '-linkmode external -extldflags -static-pie'"

RUN /usr/bin/xcaddy build

FROM scratch

COPY --from=build /go/caddy /usr/local/bin/caddy
COPY Caddyfile /etc/caddy/Caddyfile
9 changes: 9 additions & 0 deletions examples/caddy-2.7/Kraftfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
spec: v0.6

name: caddy

runtime: index.unikraft.io/unikraft.org/base:latest

rootfs: ./rootfs

cmd: ["/usr/local/bin/caddy", "run", "--config", "/etc/caddy/Caddyfile"]
8 changes: 8 additions & 0 deletions examples/caddy-2.7/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
IMAGE_NAME = unikraft-caddy

include ../../utils/bincompat/docker.Makefile

export: clean create cleanfs
mkdir rootfs
docker export $(CONTAINER_NAME) | tar -x -C rootfs
cp hosts rootfs/etc/hosts
34 changes: 34 additions & 0 deletions examples/caddy-2.7/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Caddy 2.7

This directory contains the definition to run [Caddy](https://github.com/caddyserver/caddy) on Unikraft in binary compatibility mode.

Follow the instructions in the common `../README.md` file to set up and configure the application.

## Quick Run

Use `kraft` to run the image:

```console
kraft run -M 256M -p 2015:2015
```

Once executed, it will open port `2015` and wait for connections, and can be queried.

Query the service using:

```console
curl localhost:2015
```

This doesn't work, however, probably due to some Caddy specifics.

## Scripted Run

Use the scripted runs, detailed in the common `../README.md`.
Once you run the the scripts, query the service:

```console
curl 172.44.0.2:2015
```

This will show a `Hello, World!` message, provided by the Caddy, based on the configuration on `Caddyfile`.
3 changes: 3 additions & 0 deletions examples/caddy-2.7/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
networking: True
accel: True
memory: 512
2 changes: 2 additions & 0 deletions examples/caddy-2.7/hosts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
127.0.0.1 localhost
::1 ip6-localhost ip6-loopback

0 comments on commit b8eeda8

Please sign in to comment.