Skip to content
joshixPublic

About

Caddy web server container image

Topics

Resources

Stars

33 stars

Watchers

2 watching

Forks

Latest commit

 

History

202 Commits

Folders and files

Repository files navigation

Caddy container image

Container image

This container image encapsulates a Caddy HTTP server. It is built FROM the scratch image and executes a statically-linked caddy binary with no added modules. It includes a tiny index.html landing page so that it can be demonstrated without configuration on any container host by invoking e.g., docker run -d -P ghcr.io/joshix/caddy.

By default this caddy listens on the container's EXPOSEd TCP port #8080 and attempts to fulfill requests with files beneath the container's /var/www/html/.

Content should be added by binding a host volume over that path, or by COPYing/ADDing files there when building an image FROM this one. Adding a Caddyfile through the same mechanisms allows configuration of the web server and sites as described in the Caddy documentation.

Image Tags

A GitHub Action builds images for linux/amd64 and linux/arm64 and publishes them to ghcr.io/joshix/caddy. Pulls select the matching architecture automatically.

  • ghcr.io/joshix/caddy:2.11.9 - A release built from the git tag v2.11.9. The image tag drops the v prefix.
  • ghcr.io/joshix/caddy:2.11 - The newest release in that minor version line. E.g., always the latest 2.11, but never 2.12.
  • ghcr.io/joshix/caddy:2 - The newest release in that major version line. E.g., always the latest 2.x, but never 3.x.
  • ghcr.io/joshix/caddy:latest - The current master branch (also tagged :master).
  • ghcr.io/joshix/caddy:sha-<commit> - A specific commit.

Container File System

The caddy binary produced by the build stage and the file tree beneath ./rootfs/ are COPY'd to the container's /, resulting in this file hierarchy in the container image:

  • /bin/caddy - Server executable and container ENTRYPOINT
  • /var/www/html/ - Caddy working directory and root of HTTP name space
  • /var/www/html/Caddyfile - Default configuration
  • /var/www/html/index.html - Default landing page

Adding Content

There are at least two ways to provide Caddy with content and configuration.

  • Bind a host file system path over the container's HTTP name space root:
$ ls site/html
  index.html
  [...]
$ docker run -d -p 8080:8080 -v ./site:/var/www:ro ghcr.io/joshix/caddy

OR,

  • Build the files into an image based on this one:
$ cd site/html
$ ls
  Dockerfile
  index.html
  [...]
$ cat Dockerfile
  FROM ghcr.io/joshix/caddy
  COPY . /var/www/html
$ docker build -t "com.mysite-caddy" .
$ docker run -d -p 8080:8080 com.mysite-caddy

Configuration

To configure Caddy, add Caddyfile to the server's working directory:

$ ls site/html
  Caddyfile
  index.md
  [...]
$ cat site/html/Caddyfile
  :8080 {
    file_server {
    }
    log {
      output stdout
    }
  }
  [...]
$ docker run -d -p 8080:8080 -v ./site:/var/www:ro ghcr.io/joshix/caddy

Manual TLS

To serve HTTPS, add certificate and key files, with a Caddyfile naming them:

$ ls site
  html/
  tls/
$ ls site/html
  Caddyfile
  index.html
  [...]
$ ls site/tls
  site.crt
  site.key
$ cat site/html/Caddyfile
  {
    http_port 8080
  }
  :8443 {
    tls ../tls/site.crt ../tls/site.key
    file_server {
    }
    log {
      output stdout
    }
  }
  [...]
$ docker run -d -p 8080:8080 -p 8443:8443 -v ./site:/var/www:ro ghcr.io/joshix/caddy

Automatic Let's Encrypt TLS

Caddy can automatically acquire and renew TLS keys and certificates using the ACME protocol. Doing so requires further arrangement not covered in this README.

Cloning this repo

Versions up to v2.6.2-cb.1 included a caddy binary built outside the container build process. While that is no longer true, and caddy is built in a multi-stage container build, this repo remains large with every previous version having a binary at rootfs/bin/caddy.

Work around this with git's shallow clone. This fetches only the given number of revisions. For most new clones of this repo, that number should be 1. Something like git clone --depth 1 https://github.com/joshix/caddybox should require only a small download and disk allocation.

Building Caddy with xcaddy

Preserved for reference. The build is no longer done out-of-band and the caddy binary is no longer included in this container image source repo. Instead, the xcaddy build tool runs inside a first stage build container in a multi-stage Dockerfile.

https://github.com/caddyserver/xcaddy

cd /tmp/caddyboxbuild
GOOS=linux GOARCH=amd64 xcaddy build v2.11.9
file caddy
cp caddy [...]/caddybox/rootfs/bin/caddy

About

Caddy web server container image

Topics

Resources

Stars

33 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages