If you pay for cloud storage every month, you can replace most of it with a Nextcloud instance running on your own VPS. Ready to self host Nextcloud Docker Compose on your own server? This guide walks you through the whole thing: one compose file spins up the Nextcloud app, a MariaDB database, and Redis caching. You will end up with a private, self hosted cloud storage VPS behind HTTPS, with cron jobs and basic hardening switched on. The full nextcloud docker compose setup takes about an hour, and you only need a fresh Ubuntu or Debian VPS, a domain name pointed at it, and basic comfort with the Linux terminal.
What you need before you start
Get these three things ready and the rest is smooth sailing.
- A VPS with at least 2 vCPU and 4 GB RAM. That handles a few users and everyday file sync without breaking a sweat. Size the disk for the data you plan to store; you can attach block storage later. VPS pricing changes often and differs by provider, so check official pricing before you buy.
- A domain name with an A record pointing at the VPS. Nextcloud also works on a bare IP, but HTTPS is far easier with a domain.
- SSH access and a non-root user with sudo. The commands below assume Ubuntu 22.04 or 24.04, but Debian works the same way.
Step-by-step plan to self host Nextcloud Docker Compose
Every self host Nextcloud Docker Compose project has the same moving parts: the app container, the database, and the cache. We will run Nextcloud’s official Apache image, MariaDB for the database, and Redis for file locking and caching. Data lives in two named volumes so it survives container rebuilds, and all project files go under /opt/nextcloud on the host.
1. Install Docker and Docker Compose
On a fresh server, install Docker from Docker’s own apt repository so you get current versions:
sudo apt update && sudo apt install -y ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg |
sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg]
https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" |
sudo tee /etc/apt/sources.list.d/docker.list
sudo apt update && sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
docker compose version
If the last command prints a version number, you are ready.
2. Write the docker-compose.yml file
Create the project directory and save the compose file below as docker-compose.yml. This is the whole nextcloud docker compose setup in one place:
services:
db:
image: mariadb:11
restart: unless-stopped
command: --transaction-isolation=READ-COMMITTED --log-bin=binlog --binlog-format=ROW
volumes:
- db:/var/lib/mysql
environment:
MARIADB_ROOT_PASSWORD: "change-me-root"
MARIADB_PASSWORD: "change-me-nc"
MARIADB_DATABASE: nextcloud
MARIADB_USER: nextcloud
redis:
image: redis:7-alpine
restart: unless-stopped
app:
image: nextcloud:30-apache
restart: unless-stopped
ports:
- "8080:80"
volumes:
- nextcloud:/var/www/html
environment:
MYSQL_PASSWORD: "change-me-nc"
MYSQL_DATABASE: nextcloud
MYSQL_USER: nextcloud
MYSQL_HOST: db
REDIS_HOST: redis
volumes:
db:
nextcloud:
A few notes before you start it. The db volume keeps your database on the host so it survives container updates, and the nextcloud volume holds your files and config. Replace both change-me passwords with long random strings first. The app exposes port 8080 on the host; a reverse proxy will sit in front of it later to handle HTTPS.
Start the containers and run the Nextcloud setup wizard
Bring the stack up:
mkdir -p /opt/nextcloud && cd /opt/nextcloud
# save the compose file above as docker-compose.yml, then:
sudo docker compose up -d
sudo docker compose ps
Give it a minute to initialize the database, then open http://YOUR_SERVER_IP:8080 in a browser. Create an admin account, pick MySQL/MariaDB in the storage and database section, and enter these values:
- Database user:
nextcloud - Database password: the
MARIADB_PASSWORDyou set - Database name:
nextcloud - Database host:
db
Leave the data folder at its default. Click through and you land in the Nextcloud dashboard. Files you upload now live in the named volume on the host, not inside a throwaway container.
Nextcloud reverse proxy HTTPS configuration with Caddy
Serving Nextcloud over plain HTTP is asking for trouble, so put Caddy in front of it. Install Caddy on the same VPS, then create /etc/caddy/Caddyfile:
cloud.example.com {
reverse_proxy localhost:8080
}
Replace cloud.example.com with your own domain. Caddy fetches a Let’s Encrypt certificate automatically and renews it, so there is no certbot step. Reload Caddy, visit https://cloud.example.com, and confirm the padlock icon. A nextcloud reverse proxy https setup like this also lets you add rate limiting or basic auth later without touching Nextcloud itself.
Basic hardening: trusted domains, cron, and safe defaults
Three things worth doing on day one.
Add your domain to trusted domains
Nextcloud refuses to serve hostnames it does not recognize, so register yours:
cd /opt/nextcloud
sudo docker exec --user www-data nextcloud-app-1
php occ config:system:set trusted_domains 1 --value=cloud.example.com
Container names vary; check yours with docker compose ps and adjust the command.
Switch background jobs to system cron
The default AJAX mode only runs when someone visits the site. A real cron keeps previews, sync, and cleanup running on schedule. Add this line to the host crontab (sudo crontab -e):
*/5 * * * * docker exec --user www-data nextcloud-app-1 php -f /var/www/html/cron.php
Then in Nextcloud go to Administration settings, Basic settings, and set background jobs to Cron.
Read the security warnings
Open Administration settings, then Overview. Nextcloud flags missing PHP modules, insecure headers, and outdated versions right there. Work through the list before you invite other users. Finally, keep the stack current: run docker compose pull followed by docker compose up -d when you want the latest patched images, and read the release notes first on major versions.
Back up before you need it
Your data lives in two places: the nextcloud volume (files and config) and the db volume (database). A simple approach: stop the app container, copy both volumes off the server, and dump the database:
sudo docker exec nextcloud-db-1 mariadb-dump -u root -p nextcloud > nextcloud-backup.sql
If the data matters, follow the 3-2-1 rule: three copies, two different media, one off-site. Then test a restore once. A backup you have never restored is a hope, not a plan.
Frequently asked questions
How much disk space does a self-hosted Nextcloud need?
The app and database use well under 2 GB. The rest is your files. Start with what your VPS offers and grow into a bigger disk or object storage when you need it. Exact disk pricing differs per provider, so check official pricing.
Can I run Nextcloud without HTTPS?
It will technically run on plain HTTP, but logins and file transfers would travel unencrypted. Use HTTPS from the start; with Caddy it is one small config file.
How do I update Nextcloud in Docker?
Run docker compose pull, then docker compose up -d. Patch updates are painless. For major version jumps, read the Nextcloud upgrade notes first and take a backup before you pull.
Is it safe to expose Nextcloud to the internet?
Plenty of people do it. The basics are HTTPS, a strong admin password, trusted domains set, cron running, and images kept current. Fail2ban on the host adds another layer if you want it. Once file sync runs, the same VPS usually has room for a Vaultwarden vault or an n8n automation stack too.
Where are my uploaded files stored?
In the nextcloud named volume on the host, which you can inspect with docker volume inspect. The database holds metadata; the files themselves live on disk in the volume. Back up both.
Conclusion
You now have Nextcloud running in Docker Compose with MariaDB and Redis, served over HTTPS through Caddy, with system cron and trusted domains configured. From here the usual next steps are connecting the desktop and mobile sync clients, turning on server-side encryption if you want it, and scheduling regular backups. Your files sit on your own server now, which is the whole point of self-hosting. If Nextcloud turns out heavier than you need, lighter tools like Syncthing or Seafile are worth a look.