Skip to content

Commit

Permalink
feat(library): Introduce Caddy 2.7
Browse files Browse the repository at this point in the history
Introduce Caddy bincompat run. Build Caddy as static PIE using a
Dockerfile, inspired by Caddy's own Docker setup. Use `xcaddy` to build.
The build uses binary compatibility mode (i.e. the `base` image).

Create a build of the `base` (ELF Loader) and embedded the Caddy files
as an initial ramdisk.

Add:

* `Kraftfile`: Kraft configuration to build and run
* `Dockerfile`: base Caddy filesystem
* `Caddyfile`: Caddy configuration file
* `README.md`: details on building and running Caddy

Signed-off-by: Razvan Deaconescu <[email protected]>
  • Loading branch information
Razvan Deaconescu authored and razvand committed Dec 19, 2023
1 parent 9a88dfc commit 0cfedfc
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 0 deletions.
3 changes: 3 additions & 0 deletions library/caddy/2.7/Caddyfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:2015

respond "Hello, world!"
37 changes: 37 additions & 0 deletions library/caddy/2.7/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Inspired from: https://github.com/caddyserver/caddy-docker/blob/master/2.7/builder/Dockerfile
FROM golang:1.21.4-bookworm AS build

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

ENV XCADDY_VERSION 0.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/v${XCADDY_VERSION}/xcaddy_${XCADDY_VERSION}_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
138 changes: 138 additions & 0 deletions library/caddy/2.7/Kraftfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
spec: v0.6

name: caddy

rootfs: ./Dockerfile

template:
source: https://github.com/unikraft/app-elfloader
version: staging

cmd: ["/usr/bin/caddy", "run", "--config", "/etc/caddy/Caddyfile"]

unikraft:
source: https://github.com/unikraft/unikraft
version: staging
kconfig:
# Configurations options for app-elfloader
# (they can't be part of the template atm)
CONFIG_APPELFLOADER_ARCH_PRCTL: 'y'
CONFIG_APPELFLOADER_BRK: 'y'
CONFIG_APPELFLOADER_CUSTOMAPPNAME: 'y'
CONFIG_APPELFLOADER_STACK_NBPAGES: 128
CONFIG_APPELFLOADER_VFSEXEC_EXECBIT: 'n'
CONFIG_APPELFLOADER_VFSEXEC: 'y'
CONFIG_APPELFLOADER_HFS: 'y'
CONFIG_APPELFLOADER_HFS_ETCRESOLVCONF: 'y'
CONFIG_APPELFLOADER_HFS_ETCHOSTS: 'y'
CONFIG_APPELFLOADER_HFS_ETCHOSTNAME: 'y'
CONFIG_APPELFLOADER_HFS_REPLACEEXIST: 'y'
# Unikraft options
CONFIG_HAVE_PAGING_DIRECTMAP: 'y'
CONFIG_HAVE_PAGING: 'y'
CONFIG_I8042: 'y'
CONFIG_LIBDEVFS_AUTOMOUNT: 'y'
CONFIG_LIBDEVFS_DEV_NULL: 'y'
CONFIG_LIBDEVFS_DEV_STDOUT: 'y'
CONFIG_LIBDEVFS_DEV_ZERO: 'y'
CONFIG_LIBDEVFS: 'y'
CONFIG_LIBPOSIX_ENVIRON_ENVP0: "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
CONFIG_LIBPOSIX_ENVIRON_ENVP1: "LD_LIBRARY_PATH=/usr/local/lib:/usr/lib:/lib"
CONFIG_LIBPOSIX_ENVIRON_ENVP2: "HOME=/"
CONFIG_LIBPOSIX_ENVIRON: 'y'
CONFIG_LIBPOSIX_EVENTFD: 'y'
CONFIG_LIBPOSIX_FDIO: 'y'
CONFIG_LIBPOSIX_FDTAB: 'y'
CONFIG_LIBPOSIX_FUTEX: 'y'
CONFIG_LIBPOSIX_MMAP: 'y'
CONFIG_LIBPOSIX_NETLINK: 'y'
CONFIG_LIBPOSIX_PIPE: 'y'
CONFIG_LIBPOSIX_POLL: 'y'
CONFIG_LIBPOSIX_PROCESS_CLONE: 'y'
CONFIG_LIBPOSIX_SOCKET: 'y'
CONFIG_LIBPOSIX_SYSINFO: 'y'
CONFIG_LIBPOSIX_TIME: 'y'
CONFIG_LIBPOSIX_TIMERFD: 'y'
CONFIG_LIBPOSIX_UNIXSOCKET: 'y'
CONFIG_LIBPOSIX_USER_GID: 0
CONFIG_LIBPOSIX_USER_GROUPNAME: "root"
CONFIG_LIBPOSIX_USER_UID: 0
CONFIG_LIBPOSIX_USER_USERNAME: "root"
CONFIG_LIBPOSIX_USER: 'y'
CONFIG_LIBRAMFS: 'y'
CONFIG_LIBSYSCALL_SHIM_HANDLER_ULTLS: 'y'
CONFIG_LIBSYSCALL_SHIM_HANDLER: 'y'
CONFIG_LIBSYSCALL_SHIM_LEGACY_VERBOSE: 'y'
CONFIG_LIBSYSCALL_SHIM: 'y'
CONFIG_LIBUKALLOCPOOL: 'y'
CONFIG_LIBUKBLKDEV_SYNC_IO_BLOCKED_WAITING: 'y'
CONFIG_LIBUKBLKDEV: 'y'
CONFIG_LIBUKBOOT_BANNER_MINIMAL: 'y'
CONFIG_LIBUKBOOT_HEAP_BASE: '0x400000000'
CONFIG_LIBUKBOOT_MAINTHREAD: 'y'
CONFIG_LIBUKBOOT_SHUTDOWNREQ_HANDLER: 'y'
CONFIG_LIBUKCPIO: 'y'
CONFIG_LIBUKDEBUG_CRASH_SCREEN: 'y'
CONFIG_LIBUKDEBUG_ENABLE_ASSERT: 'y'
CONFIG_LIBUKDEBUG_PRINT_SRCNAME: 'n'
CONFIG_LIBUKDEBUG_PRINT_TIME: 'y'
CONFIG_LIBUKDEBUG_PRINTK_ERR: 'y'
CONFIG_LIBUKDEBUG_PRINTK: 'y'
CONFIG_LIBUKDEBUG: 'y'
CONFIG_LIBUKFALLOC: 'y'
CONFIG_LIBUKMPI: 'n'
CONFIG_LIBUKSIGNAL: 'y'
CONFIG_LIBUKSWRAND_DEVFS: 'y'
CONFIG_LIBUKSWRAND: 'y'
CONFIG_LIBUKVMEM_DEFAULT_BASE: '0x0000001000000000'
CONFIG_LIBUKVMEM_DEMAND_PAGE_IN_SIZE: 12
CONFIG_LIBUKVMEM_PAGEFAULT_HANDLER_PRIO: 4
CONFIG_LIBUKVMEM: 'y'
CONFIG_LIBVFSCORE_AUTOMOUNT_ROOTFS: 'y'
CONFIG_LIBVFSCORE_FSTAB: 'y'
CONFIG_LIBVFSCORE_NONLARGEFILE: 'y'
CONFIG_LIBVFSCORE_ROOTFS_EINITRD: 'y'
CONFIG_LIBVFSCORE: 'y'
CONFIG_OPTIMIZE_DEADELIM: 'y'
CONFIG_OPTIMIZE_LTO: 'y'
CONFIG_PAGING: 'y'
CONFIG_STACK_SIZE_PAGE_ORDER: 4 # 128 * 4K = 512K
CONFIG_UKPLAT_KSP_SIZE: 32768
CONFIG_UKPLAT_MEMREGION_MAX_COUNT: 64

# Debug options
# CONFIG_LIBUKDEBUG_PRINTD: 'y'
# CONFIG_LIBUKDEBUG_PRINTK_INFO: 'y'
CONFIG_LIBSYSCALL_SHIM_STRACE: 'y'
# CONFIG_LIBSYSCALL_SHIM_DEBUG: 'y'

libraries:
lwip:
source: https://github.com/unikraft/lib-lwip
version: staging
kconfig:
CONFIG_LWIP_LOOPIF: 'y'
CONFIG_LWIP_UKNETDEV: 'y'
CONFIG_LWIP_LOOPBACK: 'y'
CONFIG_LWIP_TCP: 'y'
CONFIG_LWIP_UDP: 'y'
CONFIG_LWIP_RAW: 'y'
CONFIG_LWIP_WND_SCALE: 'y'
CONFIG_LWIP_TCP_KEEPALIVE: 'y'
CONFIG_LWIP_THREADS: 'y'
CONFIG_LWIP_HEAP: 'y'
CONFIG_LWIP_SOCKET: 'y'
CONFIG_LWIP_AUTOIFACE: 'y'
CONFIG_LWIP_IPV4: 'y'
CONFIG_LWIP_DHCP: 'n'
CONFIG_LWIP_DNS: 'n'
CONFIG_LWIP_NUM_TCPCON: 64
CONFIG_LWIP_NUM_TCPLISTENERS: 64
CONFIG_LWIP_ICMP: 'y'
libelf:
source: https://github.com/unikraft/lib-libelf
version: staging

targets:
- fc/x86_64
- qemu/x86_64
41 changes: 41 additions & 0 deletions library/caddy/2.7/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Caddy 2.7

This directory contains the definition for building the `unikraft.org/caddy:2.7` image.

To run this image, [install Unikraft's companion command-line toolchain `kraft`](https://unikraft.org/docs/cli) and then you can run:

```console
kraft run -p 2015:2015 unikraft.org/caddy:2.7
```

To customize the `Caddyfile`, simply provide an alternative path to `/etc/caddy`:

```console
kraft run -p 2015:2015 -v $(pwd):/etc/caddy unikraft.org/caddy:2.7
```

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

```console
curl http://localhost:2015
```


## Building and packaging locally

To build this image locally:

```console
kraft build --no-cache
```

Then package:

```console
kraft pkg --name unikraft.org/caddy:2.7 --strategy overwrite
```


## See also

- [How to run unikernels locally in Unikraft's Documentation](https://unikraft.org/docs/cli/running).

0 comments on commit 0cfedfc

Please sign in to comment.