Nextcloud with Docker Compose
A safe home for all your data: store your files, contacts, calendars and more on a server of your choosing, keep them synchronised across your devices and share them with others. In the project's words: Nextcloud Server README checked 2026-09-23
Replaces: Microsoft 365. Nextcloud's own site calls it "the better Microsoft 365 for private clouds". nextcloud.com front page checked 2026-09-23
Add Nextcloud to the builder to use your own domain, get its passwords generated in your browser, and combine it with other apps in one file.
What Nextcloud needs
Only figures Nextcloud's own documentation publishes, each linked to the page it is on. Where the project gives no number, this page does not invent one.
| Resource | Minimum | Recommended |
|---|---|---|
| RAM | 128 MB per PHP process | 512 MB per PHP process |
| Per PHP process, not per server: Nextcloud says the total varies greatly with the number of users, apps, files and server activity. The built-in updater needs at least 256 MB. Source: Nextcloud admin manual: System requirements (Memory) checked 2026-09-23 | ||
| CPU | Not published by the project | |
| No speed or core count. A 64-bit CPU, OS and PHP is "strongly recommended"; 32-bit works but cannot handle dates before 1970 or after 2038. Source: Nextcloud admin manual: System requirements (CPU Architecture and OS) checked 2026-09-23 | ||
| Disk | Not published by the project | |
docker-compose.yml
This is exactly what the builder writes for Nextcloud alone on example.org: the official definition with its published ports removed and Caddy added in front. Every ${VARIABLE} is defined in the .env below.
# docker-compose.yml built at dankhost.com for: Nextcloud
# Each app block is its official compose definition. Only the names were prefixed with
# the app slug (services, containers, volumes, bind folders, .env variables) so any set of
# apps can share one file; other differences are listed under "Changed:" in each block.
# Only Caddy publishes ports. Secrets live in .env next to this file.
services:
# ---- Caddy: the one reverse proxy, HTTPS certificates from Let's Encrypt ----
# Source: https://caddyserver.com/docs/running (checked 2026-09-23)
# Source: https://github.com/dani-garcia/vaultwarden/wiki/Using-Docker-Compose (checked 2026-09-23)
# Caddyfile goes in ./caddy/Caddyfile. Fixed address 172.30.0.2 so apps can trust it exactly.
caddy:
image: "caddy:2"
container_name: "caddy"
restart: "unless-stopped"
ports:
- "80:80"
- "443:443"
- "443:443/udp"
volumes:
- "./caddy:/etc/caddy"
- "caddy-data:/data"
- "caddy-config:/config"
networks:
proxy:
ipv4_address: "172.30.0.2"
# ---- Nextcloud (cloud.example.org) ----
# Source: https://github.com/nextcloud/docker (checked 2026-09-23)
# Source: https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/reverse_proxy_configuration.html (checked 2026-09-23)
# Changed: Removed the documented '8080:80' port; Caddy reaches the app on the proxy network.
# The documented blank MYSQL_ROOT_PASSWORD / MYSQL_PASSWORD are filled from generated secrets in .env.
# Added REDIS_HOST (the README's Redis caching variable) so the Redis container the example starts is actually used.
# Added NEXTCLOUD_ADMIN_USER/PASSWORD so the install finishes by itself and no stranger can claim the install wizard.
# Added the README's reverse-proxy variables: NEXTCLOUD_TRUSTED_DOMAINS, APACHE_DISABLE_REWRITE_IP, TRUSTED_PROXIES and OVERWRITEHOST/PROTOCOL/CLIURL.
nextcloud-db:
image: "mariadb:lts"
container_name: "nextcloud-db"
restart: "always"
command: "--transaction-isolation=READ-COMMITTED"
volumes:
- "nextcloud-db:/var/lib/mysql"
environment:
MYSQL_ROOT_PASSWORD: "${NEXTCLOUD_MYSQL_ROOT_PASSWORD}"
MYSQL_PASSWORD: "${NEXTCLOUD_MYSQL_PASSWORD}"
MYSQL_DATABASE: "nextcloud"
MYSQL_USER: "nextcloud"
networks:
nextcloud-backend:
aliases:
- "db"
nextcloud-redis:
image: "redis:alpine"
container_name: "nextcloud-redis"
restart: "always"
networks:
nextcloud-backend:
aliases:
- "redis"
nextcloud-app:
image: "nextcloud"
container_name: "nextcloud-app"
restart: "always"
depends_on:
- "nextcloud-redis"
- "nextcloud-db"
volumes:
- "nextcloud:/var/www/html"
environment:
MYSQL_PASSWORD: "${NEXTCLOUD_MYSQL_PASSWORD}"
MYSQL_DATABASE: "nextcloud"
MYSQL_USER: "nextcloud"
MYSQL_HOST: "db"
REDIS_HOST: "redis"
NEXTCLOUD_ADMIN_USER: "admin"
NEXTCLOUD_ADMIN_PASSWORD: "${NEXTCLOUD_ADMIN_PASSWORD}"
NEXTCLOUD_TRUSTED_DOMAINS: "cloud.example.org"
APACHE_DISABLE_REWRITE_IP: "1"
TRUSTED_PROXIES: "172.30.0.2"
OVERWRITEHOST: "cloud.example.org"
OVERWRITEPROTOCOL: "https"
OVERWRITECLIURL: "https://cloud.example.org"
networks:
proxy: {}
nextcloud-backend:
aliases:
- "app"
networks:
# Pinned so the trusted-proxy address given to each app (172.30.0.2) is exact.
# If "docker compose up" reports "Pool overlaps", change 172.30.0 here, in Caddy's
# ipv4_address and in every trusted-proxy value to a free 172.x.0 range.
proxy:
ipam:
config:
- subnet: "172.30.0.0/24"
ip_range: "172.30.0.128/25"
gateway: "172.30.0.1"
nextcloud-backend:
internal: true
volumes:
caddy-data:
caddy-config:
nextcloud:
nextcloud-db:
Caddyfile save as caddy/Caddyfile
Caddy gets and renews the HTTPS certificate for cloud.example.org by itself once the DNS record points at your server and ports 80 and 443 reach it.
# Caddyfile built at dankhost.com. Save it as ./caddy/Caddyfile next to docker-compose.yml.
# Caddy gets and renews a Let's Encrypt certificate for each name below by itself,
# once the DNS records point at this server and ports 80 and 443 reach it.
# Nextcloud
cloud.example.org {
redir /.well-known/carddav /remote.php/dav/ 301
redir /.well-known/caldav /remote.php/dav/ 301
reverse_proxy nextcloud-app:80
}
.env
The secrets are left blank here on purpose: a password printed on a public page is the same for everyone who copies it. The builder fills them with random letters and digits made in your browser.
# .env for Nextcloud, from dankhost.com/app/nextcloud/. Fill the blank secrets before starting. # Keep this file private (chmod 600). # Nextcloud # 32 random letters and digits: the builder fills this in your browser NEXTCLOUD_MYSQL_ROOT_PASSWORD= # 32 random letters and digits: the builder fills this in your browser NEXTCLOUD_MYSQL_PASSWORD= # 24 random letters and digits: the builder fills this in your browser NEXTCLOUD_ADMIN_PASSWORD=
Changes from the official file
Besides prefixing every service, container, volume, folder and variable with nextcloud (so any set of apps can share one file), these are all the differences from what the project documents:
- Removed the documented '8080:80' port; Caddy reaches the app on the proxy network.
- The documented blank MYSQL_ROOT_PASSWORD / MYSQL_PASSWORD are filled from generated secrets in .env.
- Added REDIS_HOST (the README's Redis caching variable) so the Redis container the example starts is actually used.
- Added NEXTCLOUD_ADMIN_USER/PASSWORD so the install finishes by itself and no stranger can claim the install wizard.
- Added the README's reverse-proxy variables: NEXTCLOUD_TRUSTED_DOMAINS, APACHE_DISABLE_REWRITE_IP, TRUSTED_PROXIES and OVERWRITEHOST/PROTOCOL/CLIURL.
Notes for Nextcloud behind the proxy
- Log in as 'admin' with NEXTCLOUD_ADMIN_PASSWORD from .env. The first start takes a minute or two while the installer runs.
- The two Caddy 'redir' lines are the ones Nextcloud's admin manual gives for CalDAV/CardDAV discovery; without them the security check warns and phone calendar apps cannot find the server.
How to run it
- At your DNS provider, add an A record named
cloudon your domain pointing at the server's public IP address, and forward TCP 80, TCP 443 and UDP 443 to it. - Save the three files in one folder:
docker-compose.ymland.envside by side, the Caddyfile at./caddy/Caddyfile. Replaceexample.orgwith your domain in the Caddyfile, the compose file and the .env, or let the builder do it. - Fill the blank
NEXTCLOUD_MYSQL_ROOT_PASSWORD,NEXTCLOUD_MYSQL_PASSWORD,NEXTCLOUD_ADMIN_PASSWORDin.env. The builder generates them; on the server,tr -dc A-Za-z0-9 </dev/urandom | head -c 32prints 32 random letters and digits. Thenchmod 600 .env. - Start it:
docker compose up -d docker compose logs -f caddy # watch the certificate arrive
- Open the new address (
cloud.plus your domain) in a browser and follow the notes above.
Back it up
Nextcloud's admin manual lists five things to keep: the config, data, theme and custom apps folders, and the database. In this compose all four folders are inside the nextcloud volume, and the database is dumped with mariadb-dump while Nextcloud is in maintenance mode.
Below is the BACKUP.md the builder writes next to the compose file above: which of its folders and volumes hold data, the dump command the project documents, rewritten for this file's service names, what to copy with which container stopped, and a restore check. Pick more apps in the builder and it covers them all in one file.
# BACKUP.md built at dankhost.com for: Nextcloud
What to copy from the docker-compose.yml built with this file, the database dump each project documents (rewritten for the service names in that file), what to copy with which container stopped, and how to check the result. Run every command in the folder that holds docker-compose.yml.
The folders are owned by the containers' users: put sudo in front of tar if it says "Permission denied". Named volumes live under /var/lib/docker/volumes, not next to the compose file, so they are copied through a throwaway ubuntu container (Docker's documented --volumes-from method). tar makes a full copy each run; for large folders an incremental tool (restic, borg, rsync) saves time and space.
Never run "docker compose down -v" on this file: it deletes the named volumes of every app in it.
## Before you start
BACKUP=/mnt/backup/selfhost # change this: a disk or machine other than this server's system disk
mkdir -p "$BACKUP"
## Every time: the three stack files
tar czf "$BACKUP/stack-files.tar.gz" docker-compose.yml .env caddy
.env holds the database passwords and secret keys the apps were set up with; keep this archive as private as the data.
## Caddy (the reverse proxy)
Caddy's docs: 'The data directory must not be treated as a cache.' It holds the TLS certificates and their private keys; without it Caddy has to ask Let's Encrypt for every certificate again.
- caddy-data (named volume, /data in caddy)
BACK UP: TLS certificates, private keys, OCSP staples.
- caddy-config (named volume, /config in caddy)
SKIP: the last active configuration, kept for 'caddy run --resume', which this setup does not use: it starts from the Caddyfile.
- ./caddy (/etc/caddy in caddy)
IN stack-files.tar.gz: the Caddyfile, copied with the stack files above.
Copy (Caddy keeps serving while you copy):
docker run --rm --volumes-from caddy -v "$BACKUP":/backup ubuntu tar cvf /backup/caddy-data.tar /data
Restore check:
tar tf "$BACKUP/caddy-data.tar" > /dev/null && echo OK
docker run --rm --volumes-from caddy -v "$BACKUP":/backup ubuntu bash -c "cd /data && tar xvf /backup/caddy-data.tar --strip 1"
Sources:
- Caddy docs: Conventions, Data directory: https://caddyserver.com/docs/conventions#data-directory (checked 2026-09-24)
- Docker docs: Volumes, Back up, restore, or migrate data volumes: https://docs.docker.com/engine/storage/volumes/#back-up-restore-or-migrate-data-volumes (checked 2026-09-24)
## Nextcloud
Nextcloud's admin manual lists five things to keep: the config, data, theme and custom apps folders, and the database. In this compose all four folders are inside the nextcloud volume, and the database is dumped with mariadb-dump while Nextcloud is in maintenance mode.
What this compose mounts:
- nextcloud-db (named volume, /var/lib/mysql in nextcloud-db)
COVERED BY THE COMMAND BELOW: MariaDB data files; copy them only with the database stopped.
- nextcloud (named volume, /var/www/html in nextcloud-app)
BACK UP: Nextcloud's whole /var/www/html: config/ (config.php, with the instance's secrets), data/ (every user's files), custom_apps/ and themes/.
1. Nextcloud's backup page uses maintenance mode instead of stopping the server: it locks logged-in sessions and blocks new logins so files and database cannot drift apart.
docker compose exec -u www-data nextcloud-app php occ maintenance:mode --on
2. Database dump for nextcloud-db:
docker compose exec -T nextcloud-db sh -c 'mariadb-dump --single-transaction -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" "$MYSQL_DATABASE"' > "$BACKUP/nextcloud-sqlbkp.bak"
The manual's command is mariadb-dump --single-transaction -h [server] -u [username] -p[password] [db_name]. Here it runs inside the database container, which already has the user, password and database name in its environment, so no password is typed on the host. If you turned on 4-byte (emoji) support, the manual adds --default-character-set=utf8mb4.
3. Copy in maintenance mode:
docker run --rm --volumes-from nextcloud-app -v "$BACKUP":/backup ubuntu tar cvf /backup/nextcloud.tar /var/www/html
4. Leave maintenance mode:
docker compose exec -u www-data nextcloud-app php occ maintenance:mode --off
Restore check:
grep -c 'Dump completed' "$BACKUP/nextcloud-sqlbkp.bak" # 1 means mariadb-dump finished
tar tf "$BACKUP/nextcloud.tar" > /dev/null && echo OK
Put a copy back (with Nextcloud stopped):
docker run --rm --volumes-from nextcloud-app -v "$BACKUP":/backup ubuntu bash -c "cd /var/www/html && tar xvf /backup/nextcloud.tar --strip 3"
Nextcloud's restore page: put the folders back, drop and recreate the database, then load the dump. The last step with this file's names:
docker compose exec -T nextcloud-db sh -c 'mariadb -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" "$MYSQL_DATABASE"' < "$BACKUP/nextcloud-sqlbkp.bak"
Turn maintenance mode off afterwards. The restore page also covers re-syncing desktop clients after data recovery.
Sources:
- Nextcloud admin manual: Backup: https://docs.nextcloud.com/server/latest/admin_manual/maintenance/backup.html (checked 2026-09-24)
- Nextcloud admin manual: Restoring backup: https://docs.nextcloud.com/server/latest/admin_manual/maintenance/restore.html (checked 2026-09-24)
- nextcloud/docker README: Accessing the Nextcloud command-line interface (occ) with docker compose: https://github.com/nextcloud/docker#accessing-the-nextcloud-command-line-interface-occ (checked 2026-09-24)
- Docker docs: Volumes, Back up, restore, or migrate data volumes: https://docs.docker.com/engine/storage/volumes/#back-up-restore-or-migrate-data-volumes (checked 2026-09-24)
Sources
Every page below was fetched on the date shown.
- Nextcloud Server README checked 2026-09-23 (what it does)
- nextcloud.com front page checked 2026-09-23 (what it replaces)
- Nextcloud admin manual: System requirements (Memory) checked 2026-09-23 (hardware requirements)
- Nextcloud admin manual: System requirements (CPU Architecture and OS) checked 2026-09-23 (hardware requirements)
- nextcloud/docker README: 'Base version - apache' compose file, environment variables and reverse-proxy settings checked 2026-09-23 (compose definition and proxy settings)
- Nextcloud admin manual: Reverse proxy (Caddy example) checked 2026-09-23 (compose definition and proxy settings)
- Caddy docs: Keep Caddy running (Docker Compose section) checked 2026-09-23 (Caddy)
- Vaultwarden wiki: Caddy with HTTP challenge (uses the caddy:2 image) checked 2026-09-23 (Caddy)
- Nextcloud admin manual: Backup checked 2026-09-24 (backup plan)
- Nextcloud admin manual: Restoring backup checked 2026-09-24 (backup plan)
- nextcloud/docker README: Accessing the Nextcloud command-line interface (occ) with docker compose checked 2026-09-24 (backup plan)
- Docker docs: Volumes, Back up, restore, or migrate data volumes checked 2026-09-24 (backup plan)
- Caddy docs: Conventions, Data directory checked 2026-09-24 (backup plan)