Skip to content

dwin/caddy-docker

 
 

Repository files navigation

caddy

Fork of https://github.com/abiosoft/caddy-docker.

This is a Docker image for Caddy based on Alpine Linux 3.7. This image includes the git, rate limit, and cache plugins. Plugins can be configured via the plugins build arg.

Supported Tags

  • latest,0.10.14, 0.10.10,0.10.8
  • dns,dns-0.10.14, dns-0.10.10,dns-0.10.8, includes DNS plug-ins as detailed here
  • noplugins,noplugins-0.10.14,noplugins-0.10.10,noplugins-0.10.9,noplugins-0.10.8, noplugins-0.10.7

Getting Started

$ docker run -d -p 2015:2015 dwin/caddy

Point your browser to http://127.0.0.1:2015.

Be aware! If you don't bind mount the location certificates are saved to, you may hit Let's Encrypt rate limits rending further certificate generation or renewal disallowed (for a fixed period)! See "Saving Certificates" below!

Saving Certificates

Save certificates on host machine to prevent regeneration every time container starts. Let's Encrypt has rate limit.

$ docker run -d \
    -v $(pwd)/Caddyfile:/etc/Caddyfile \
    -v $HOME/.caddy:/root/.caddy \
    -p 80:80 -p 443:443 \
    dwin/caddy

Here, /root/.caddy is the location inside the container where caddy will save certificates.

Additionally, you can use an environment variable to define the exact location caddy should save generated certificates:

$ docker run -d \
    -e "CADDYPATH=/etc/caddycerts" \
    -v $HOME/.caddy:/etc/caddycerts \
    -p 80:80 -p 443:443 \
    dwin/caddy

Above, we utilize the CADDYPATH environment variable to define a different location inside the container for certificates to be stored. This is probably the safest option as it ensures any future docker image changes don't interfere with your ability to save certificates!

Using git sources

Caddy can serve sites from git repository using git plugin.

Create Caddyfile

Replace github.com/dwin/webtest with your repository.

$ printf "0.0.0.0\nroot src\ngit github.com/dwin/webtest" > Caddyfile
Run the image
$ docker run -d -v $(pwd)/Caddyfile:/etc/Caddyfile -p 2015:2015 dwin/caddy

Point your browser to http://127.0.0.1:2015.

Usage

Default Caddyfile

The image contains a default Caddyfile.

0.0.0.0 {
    gzip
    cache
    root /srv/www/public
    log ../access.log {
        rotate_size 20 # Rotate at 20MB
	    rotate_age 16 # Keep logs for 16 days
	    rotate_keep 15 # Keep up to 15 logs
    }
    errors ../error.log
}

Paths in container

Caddyfile: /etc/Caddyfile

Sites root: /srv

Using local Caddyfile and sites root

Replace /path/to/Caddyfile and /path/to/sites/root accordingly.

$ docker run -d \
    -v /path/to/sites/root:/srv \
    -v path/to/Caddyfile:/etc/Caddyfile \
    -p 2015:2015 \
    dwin/caddy

Let's Encrypt Auto SSL

Note that this does not work on local environments.

Use a valid domain and add email to your Caddyfile to avoid prompt at runtime. Replace mydomain.com with your domain and user@host.com with your email.

mydomain.com
tls user@host.com
Run the image

You can change the the ports if ports 80 and 443 are not available on host. e.g. 81:80, 444:443

$ docker run -d \
    -v $(pwd)/Caddyfile:/etc/Caddyfile \
    -p 80:80 -p 443:443 \
    dwin/caddy