Running Nextcloud on a Raspberry Pi and exposing it with Cloudflare Tunnel
Host your own cloud storage: run Nextcloud and PostgreSQL with Docker Compose on a Raspberry Pi, and reach it securely from anywhere through a Cloudflare Tunnel, without opening ports.
Updated 1 November 20265 min read
In this guide we set up Nextcloud on a Raspberry Pi and make it reachable from the internet with a Cloudflare Tunnel. You don't need to open any ports on your router. I use a Raspberry Pi 4B, but a Pi 5 or any small Linux server works the same way.
What is Nextcloud, and why would you use it?
Nextcloud is a self-hosted, open-source alternative to OneDrive or Google Drive. You store your files, photos and videos on your own hardware, and it comes with extras like calendars, contacts and notes. I wanted my own cloud storage without depending on a third party, and a Raspberry Pi with an external SSD is perfect for that.
Prerequisites
- A Raspberry Pi with Raspberry Pi OS (64-bit)
- Docker with the Compose plugin installed on the Pi
- A domain name, added to a Cloudflare account
- Basic knowledge of Docker and Linux (new to Docker? Read my introduction to Docker)
I store all data on an external SSD mounted at /mnt/data, which mounts automatically on boot. SD cards wear out quickly under database writes, so I recommend doing the same.
Nextcloud and PostgreSQL with Docker Compose
Nextcloud needs a database. It defaults to SQLite, but that isn't recommended for real use, so we'll use PostgreSQL. Instead of starting each container by hand with long docker run commands, we describe both in one Compose file.
Create a folder, for example ~/nextcloud, and add a .env file with a strong password (never use something like 123456):
POSTGRES_PASSWORD=replace-with-a-long-random-passwordNext to it, create compose.yaml:
services:
db:
image: postgres:17
restart: unless-stopped
environment:
POSTGRES_DB: nextcloud
POSTGRES_USER: nextcloud
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
volumes:
- /mnt/data/postgres:/var/lib/postgresql/data
nextcloud:
image: nextcloud:stable
restart: unless-stopped
depends_on:
- db
ports:
- "8080:80"
volumes:
- /mnt/data/nextcloud:/var/www/htmlWhat's going on here:
dbruns PostgreSQL 17 and creates anextclouddatabase and user. Its data lives in/mnt/data/postgreson the host, so it survives container updates.nextcloudruns the official Nextcloud image. Port80in the container is published on port8080of the Pi, and all Nextcloud files (app, config and your data) live in/mnt/data/nextcloud.restart: unless-stoppedbrings both containers back after a reboot, anddepends_onstarts the database before Nextcloud.- Networking: Compose puts both services on a shared network automatically, so Nextcloud can reach the database simply by the host name
db. No manualdocker network createneeded.
Start everything:
docker compose up -dCreating your Nextcloud account
Open http://<your-pi-ip>:8080 in your browser. You'll see the Nextcloud setup page:
- Choose a username and a strong password for your admin account.
- Open Storage & database and pick PostgreSQL.
- Fill in the database user
nextcloud, the password from your.envfile, the database namenextcloudand the hostdb:5432. - Click Install and let Nextcloud do its thing. You'll land on the dashboard when it's done.
Setting up the Cloudflare Tunnel
Nextcloud now works on your local network. To reach it from anywhere, we use a Cloudflare Tunnel. A small connector (cloudflared) on your Pi opens an outgoing connection to Cloudflare, and Cloudflare sends traffic for your domain through it. Nothing on your network is exposed directly, and you get HTTPS for free.
In the Cloudflare dashboard, open Zero Trust. Cloudflare reorganises this menu now and then, so the names may differ slightly from my screenshots.
Go to Networks > Tunnels and create a new tunnel.
Choose the Cloudflared connector type and give your tunnel a name. On the next page, select Docker. Cloudflare shows a docker run command that contains your tunnel token.
Instead of running that command by hand, copy the token (the long value after --token) into your .env file:
TUNNEL_TOKEN=paste-your-tunnel-token-hereThen add the connector as a third service in compose.yaml:
cloudflared:
image: cloudflare/cloudflared:latest
restart: unless-stopped
command: tunnel --no-autoupdate run --token ${TUNNEL_TOKEN}Run docker compose up -d again. After a few seconds, the connector shows up as connected in the Cloudflare dashboard.
Now tell the tunnel where to send traffic. Open the tunnel's configuration, go to the Public Hostname tab and add a hostname:
Pick a subdomain (for example cloud) and your domain. For the service, choose HTTP. Because cloudflared runs in the same Compose project, it can reach Nextcloud directly at nextcloud:80. If you run the connector somewhere else, use your Pi's IP address and port 8080 instead, like in my screenshot:
Save the hostname. Your Nextcloud is now on the internet, but if you open the new address you'll see an error: Nextcloud doesn't trust the domain yet.
Trusting your domain in Nextcloud
Nextcloud only answers on domains in its trusted_domains list. Because Cloudflare handles HTTPS in front of it, it also needs to know that visitors use https and your public host name. Set all three with Nextcloud's occ command (replace cloud.example.com with your hostname):
docker compose exec -u www-data nextcloud php occ config:system:set trusted_domains 1 --value=cloud.example.com
docker compose exec -u www-data nextcloud php occ config:system:set overwriteprotocol --value=https
docker compose exec -u www-data nextcloud php occ config:system:set overwritehost --value=cloud.example.comYou can also edit the config file by hand. It's in /mnt/data/nextcloud/config/config.php on the Pi:
'trusted_domains' =>
array (
0 => '192.168.1.10:8080',
1 => 'cloud.example.com',
),
'overwriteprotocol' => 'https',
'overwritehost' => 'cloud.example.com',The overwriteprotocol setting also fixes a common issue where logging in from the desktop or mobile app gets stuck, because Nextcloud would otherwise generate http links. Restart with docker compose restart nextcloud if a change doesn't show up right away.
Keeping it up to date
To update, pull the new images and recreate the containers:
docker compose pull
docker compose up -dNextcloud only supports upgrading one major version at a time. The stable tag follows that path, but don't skip months of updates. Also make regular backups of /mnt/data: self-hosting means you are your own cloud provider.
Conclusion
You now have your own Nextcloud running on a Raspberry Pi, reachable from anywhere over HTTPS through a Cloudflare Tunnel, without opening a single port. Install the Nextcloud desktop and mobile apps, and start syncing your files, photos and more to your own cloud.
Sources
Frequently asked questions
Why does Nextcloud show an untrusted domain error behind Cloudflare Tunnel?
Nextcloud only answers on domains in its trusted_domains list. Add your public hostname with occ config:system:set trusted_domains, and set overwriteprotocol to https and overwritehost to your hostname.
Do I need to open ports on my router for a Cloudflare Tunnel?
No. The cloudflared connector opens an outgoing connection to Cloudflare, and traffic for your hostname comes in through that connection.
- Raspberry Pi
- Docker
- Nextcloud
- Cloudflare
- Self-hosting