14 Deploying your API
So far, your API lives on your computer only and is accessible with URLs like http://127.0.0.1:6312
or http://localhost:6312
. If you only ever want to run this stuff locally, that’s great! You’re done!
But you probably want to make it more publicly accessible. And that’s a little tricky.
The {plumber} documentation has a whole page about hosting the API, with several different options. The most common options (unless you somehow have access to Posit Connect, which lets you do this all automatically) are to use something like a $4/month DigitalOcean server and the {plumberDeploy} package, or to use Docker and run the Docker image on any server.
14.1 The super easy way
I actually use Docker on a DigitalOcean server. To make it even easier, I use Docker Compose to connect my {plumber} image with a Caddy webserver, which automatically handles HTTPS/SSL/TLS. You can see a minimal example of the configuration at this repository here. It has just two services or containers defined in docker-compose.yml
:
plumber
: This runs a simple plumber app inplumber-thing/
that exposes the API on port 8000. This is exposed on an internal local network that only Docker Compose can see—the broader internet can’t see it.caddy
: This is a web server that automatically makes HTTPS work. When you run stuff locally, it creates self-signed SSL certificates; when you run it on a publicly accessible server, it creates SSL certificates through Let’s Encrypt. The server configuration incaddy/Caddyfile
forwards all traffic tohttps://api.localhost
to the plumber app that’s running on port 8000.
And that’s it!
To do this yourself on DigitalOcean, create a new cheapo Droplet (use this link to create a DigitalOcean account and get $200 for free, if you want), and follow these guides to get it set up:
- Initial server setup for setting up SSH, correct user groups, firewall, etc.
- Installing Docker for installing, um, Docker
- Installing Docker Compose for installing Docker Compose
Then, using a DNS service (iwantmyname, GoDaddy, whatever), point a domain name (like api.example.com) to the new DigitalOcean server’s IP address.
Put the repository on the server (use Visual Studio Code’s remote session feature to make life easier) and edit caddy/Caddyfile
with the domain name.
Finally, from your server’s terminal, run this
docker compose -f docker-compose.yml -d up
Go to https://api.example.com/__docs__/
and you should see the Plumber documentation page, and you’re done!