Dokku and static website with Caddy

3 minute read Published:

How to deploy a static web in dokku
Table of Contents

Today I was checking my Digital Ocean droplet and I recognized that some of the docker images that are stored there are quite huge.

My surprise came when I saw that the image that is use to serve a static website has 1.2 GB, when the total weight of the serve files the is 80 kB.

As dokku has support for Dockerfile, I decided to create my own “buildpack” using a docker configuration file.

With two mayor goals:

  • get the minimum image size possible (<100MB).
  • easy to configure.

Caddy

After some research I have found a awesome open source project named caddy

Caddy is an alternative web server that is easy to configure and use.

And that’s right, only with a small config file we can configure it

matt.life   # Your site's address

ext .html   # Clean URLs
errors {
    log ../errors.log    # Error log
    404 error-404.html   # Custom error page
}
fastcgi /blog localhost:9000 php   # PHP backend

I have used other static web server like nginx and I can only say that in term of configuration this is much more easier.

It also has several plugins that make it really useful. Some of then are:

  • http/2
  • Automatic HTTPS
  • Gzip
  • Multi-core
  • Serve Markdown documents rendered on-the-fly as HTML.

As is build as a binary it has also a really small size. I have found docker images in dockehub with less the 10 MB

Dokku configuration

Lets write a Dockerfile that suit dokku. As there are several docker images already build I have decided not to build it from the scratch, instead I extend of the existing ones.

One thing to take in account, the image should not expose any ports so dokku can be auto configure without problems.

I have chosen zzrot/alpine-caddy with a size of 6MB. This image is serving the files under the folder /var/www/html and copy the Coddyfile to /etc/

FROM zzrot/alpine-caddy
COPY Caddyfile /etc/Caddyfile
COPY ./static /var/www/html
I have my static files under the static folder in my project.

So lets write now the Caddyfile

0.0.0.0:5000
root /var/www/html
gzip
log stdout
errors stdout
This conf file makes the caddy webserver:

  • listen in the port 5000,
  • use gzip compression
  • serve the file under /var/www/html
  • pipe all the logs to the standard output.

Last but not least we need to push our code to dokku.

git push dokku master
Then I checked the size of the new images

The new image is almost 10 times smaller

I have created a repo in dockerhub with a prebuild image so you only need to add this Dockerfile to your project to use it

FROM cgonzalezg/dokku-static-files
Hope you enjoy

comments powered by Disqus