My go-to instant Caddy setup
When deciding to put a project in the cloud, it usually goes like this:
- Spin up Ubuntu (on Digital Ocean)
- Clone you
my_project
and run it
- Access it on
YOUR.IP.123:3000
Now, how do you make that accessible by the world with SSL and a custom domain? Is it hard?
Good news, Caddy to the rescue!
With a minimal config, you can deploy a Caddy server that automatically handles config for you. You just have to add one folder (caddy
), and two files (docker-compose.yml
and Caddyfile
).
Look at this folder structure:
- my_project
- caddy # <- create this
- docker-compose.yml # <- and this
- Caddyfile # <- and this
To add these files, run:
mkdir caddy
cd caddy
vim docker-compose.yml
# paste `docker-compose.yml` from below, and save
vim Caddyfile
# modify and paste `Caddy` from below, and save
Contents of docker-compose.yml
:
services:
caddy:
image: caddy:latest
restart: unless-stopped
ports:
- "80:80"
- "443:443"
- "443:443/udp"
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile
- ./site:/srv
- ./data:/data
- ./config:/config
extra_hosts:
- "host.docker.internal:host-gateway"
environment:
- CADDY_WATCH_CONFIG=true
command: caddy run --watch --config /etc/caddy/Caddyfile
Contents of Caddyfile
:
# Point a domain to your server's IP
#
# my-subdomain.larskarbo.no {
#
# }
# Alternatively, run it on :80 without SSL
:80 {
# Set this path to your site's directory.
root * /usr/share/caddy
# Enable the static file server.
file_server
# Another common task is to set up a reverse proxy:
# reverse_proxy host.docker.internal:8080
}
Testing and running
When you have added the config, you can run the Caddy server like this:
docker compose up --build
You should be able to access the site with automatic SSL at the domain you configured. Having problems? Look at the log output.
When you are ready, run docker compose
with the --detach
flag, to keep it running even when you close your terminal:
docker compose up --build --detach
Conclusion
Caddy is amazing software and makes me happy. Use it!