Skip to main content

Self-hosting with Caddy and Docker

·2 mins

After re-building my VPS because things were a bit messy, I decided to move away from the rather complicated Traefik setup that I had before and move over to Caddy. This took a little while to get my head around, but ultimately is much simpler!

Docker compose #

I’m running Caddy within a docker compose environment using a pretty basic setup in a docker-compose.yml file:

version: "3.7"

networks:
  default:
    name: caddy-proxy

volumes:
  caddy_data:
    external: true
  caddy_config

services:
  caddy:
    image: caddy:2.6.4
    container_name: caddy
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
      - "443:443/udp"
    volumes:
      - $PWD/caddy:/etc/caddy
      - $PWD/sites:/sites
      - caddy_data:/data
      - caddy_config:/config

Caddyfile #

My Caddyfile sits in the caddy subdirectory and is really very simple:

{
	email: example@example.com
}

www.example.com {
	redir https://example.com{uri}
}

example.com {
	root * /sites/example.com
	file_server
}

Here I’m removing the www with a redirect, then I’m serving files from a directory under the mounted /sites directory which was mounted into the caddy container by docker. I can then just copy over my public folder from hugo to serve up this site. Caddy handles all the letsencrypt certificates and things without any messing about!

More services #

With this setup it’s pretty simple to add more services and use Caddy as proxy, for example FreshRSS:

Addition to the services section of docker-compose.yml:

  freshrss:
    image: freshrss/freshrss:latest
    container_name: freshrss
    restart: unless-stopped
    logging:
      options:
        max-size: 10m
    volumes:
      - $PWD/freshrss/data:/var/www/FreshRSS/data
      - $PWD/freshrss/extensions:/var/www/FreshRSS/extensions
    environment:
      - TZ=Europe/London
      - CRON_MIN='2,32'

and the Caddyfile addition:

freshrss.example.com {
	reverse_proxy freshrss:80
}

Comments #

Please go to this post on Mastodon to add any comments or suggestions!