In my previous post about Hugo and Docker I went through the solution I came up with to have a dynamic Hugo setup in a Docker Container. Since then, all of about 12 hours, I have updated it.

Updates

The Dockerfile file changed to move the Caddyfile and watch.sh files to a defaults folder, it also now copies a entrypoint.sh script which manages these defaults.

FROM hugomods/hugo:exts-non-root

USER root
RUN apk add --no-cache caddy inotify-tools

# Copy static config files
COPY ./Caddyfile /defaults/Caddyfile
COPY ./watch.sh /defaults/watch.sh
COPY ./entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

USER hugo

WORKDIR /src

EXPOSE 80

ENTRYPOINT ["/entrypoint.sh"]

The Caddyfile file changed to handle 404 errors, right now if you used the previous config Caddy would be the one that sent the 404 to the browser, which is just a 404 response with no content, this causes your browser to pop up a error message instead of the page itself.

:80
root * /src/public
file_server

handle_errors {
	@404 {
		expression {http.error.status_code} == 404
	}
	rewrite @404 /404/
	file_server
}

The watch.sh script hasn’t changed.

A new script, entrypoint.sh, was made to copy the default Caddy config and watch.sh script if none was provided by the container volumes as bound overrides.

#!/bin/sh

# Use defaults if no override is mounted
[ -f /etc/caddy/Caddyfile ] || cp /defaults/Caddyfile /etc/caddy/Caddyfile
[ -f /watch.sh ] || cp /defaults/watch.sh /watch.sh

chmod +x /watch.sh

# Run watcher and Caddy
/watch.sh & caddy run --config /etc/caddy/Caddyfile --adapter caddyfile