How to Run Nextcloud with Redis and PostgreSQL in Docker Compose

Running Nextcloud on a single container with SQLite works for a quick test, but it starts to struggle once real files, real users, and real sync clients show up. A better setup pairs Nextcloud with Redis for caching and file locking and PostgreSQL for the database, all wired together with Docker Compose. This guide walks through a nextcloud docker compose redis postgresql stack you can run on a VPS, from the compose file to the first login and the checks that prove each piece is working.

How Nextcloud Docker Compose Redis PostgreSQL Fits Together

Each container has one job. The app container runs Nextcloud and serves the web interface. PostgreSQL stores users, shares, file metadata, and activity. Redis holds the cache and handles transactional file locking, which stops two sync clients from writing over each other. A separate cron container runs background jobs on a schedule. The default SQLite database locks the whole file during writes, while PostgreSQL handles concurrent writes better and Redis answers lock requests from memory instead of disk. If you already run Nextcloud another way, our guide to hosting your own cloud storage with Nextcloud on a VPS covers the plain install, while this article focuses on the three-container version.

What You Need Before You Start

You need a VPS with Docker and Docker Compose installed, SSH access, and a domain pointed at the server if you will use it outside your home network. A VPS with at least 2 GB of RAM is a sensible starting point for a small household or team. Create a project folder on the VPS, for example /opt/nextcloud, and put two files inside it: docker-compose.yml and .env. Keep the .env file out of any shared folder or version control, because it holds your database password and the Nextcloud admin password.

Your .env file needs a database name, user, and password, plus an admin username and password for Nextcloud. Pick a long random password for the database. You will also need to decide which port Nextcloud listens on. Port 8080 on the host is a common choice, but you should not expose it directly to the internet. Put a reverse proxy with HTTPS in front of it, or reach it through a private network, and keep the server firewall closed to everything except SSH and the proxy.

Check disk space before you pull images and create volumes. Nextcloud data grows with every file you upload, and a full disk stops the database first. Our guide to cleaning up old Docker images and freeing disk space shows how to reclaim room when images and old volumes pile up.

The Docker Compose File, Explained

The compose file below defines four services: db, redis, app, and cron. Read through it before you run it, so you know what each line does.

services:
  db:
    image: postgres:16
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - db-data:/var/lib/postgresql/data

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    command: redis-server --appendonly yes
    volumes:
      - redis-data:/data

  app:
    image: nextcloud:latest
    restart: unless-stopped
    ports:
      - "8080:80"
    environment:
      POSTGRES_HOST: db
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      REDIS_HOST: redis
      REDIS_HOST_PORT: 6379
      NEXTCLOUD_ADMIN_USER: ${NEXTCLOUD_ADMIN_USER}
      NEXTCLOUD_ADMIN_PASSWORD: ${NEXTCLOUD_ADMIN_PASSWORD}
      NEXTCLOUD_TRUSTED_DOMAINS: ${NEXTCLOUD_TRUSTED_DOMAINS}
    volumes:
      - nextcloud-data:/var/www/html
    depends_on:
      - db
      - redis

  cron:
    image: nextcloud:latest
    restart: unless-stopped
    entrypoint: /cron.sh
    environment:
      POSTGRES_HOST: db
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      REDIS_HOST: redis
      REDIS_HOST_PORT: 6379
    volumes:
      - nextcloud-data:/var/www/html
    depends_on:
      - db
      - redis
      - app

volumes:
  db-data:
  redis-data:
  nextcloud-data:

A few choices in this file deserve a note. PostgreSQL 16 and Redis 7 are pinned to a major version, while the Nextcloud image uses the latest tag. Pinning the database and cache keeps an update from jumping a major version without you noticing. The nextcloud-data volume holds both the app code and your uploaded files, so back it up along with the database volume. The cron container shares that volume and runs the same image with a different entrypoint, which is the pattern Nextcloud documents for background jobs.

If you want a deeper look at how Redis behaves on a server before you rely on it, our guide to installing Redis 7 on Ubuntu and using it as a cache walks through the same concepts outside Docker.

Start the Stack and Finish the Setup

From the project folder, pull the images and start the containers in the background:

docker compose pull
docker compose up -d
docker compose ps

Give the database a minute to initialise on first start, then open Nextcloud in a browser at your domain or at http://your-server-ip:8080 for a local test. Because the compose file already passes the admin user, database details, and Redis host through environment variables, the setup screen should be pre-filled or skipped entirely. Log in with the admin account from your .env file and check the admin overview page under Settings, Administration, Overview. A clean install shows no warnings about the database or file locking.

Next, set your trusted domains. The NEXTCLOUD_TRUSTED_DOMAINS value accepts a space-separated list, so include the domain you will actually use, for example cloud.example.com. If you change domains later, update the variable and recreate the app container. Behind a reverse proxy, confirm the admin overview page reports no mixed-content or proxy warnings.

Check That Redis Caching and File Locking Work

A docker compose nextcloud setup is easy to start and easy to misconfigure, so verify Redis rather than assuming it connected. Run these checks from the project folder:

docker compose exec redis redis-cli ping
docker compose exec app php occ config:system:get memcache.locking
docker compose exec app php occ status

The first command should answer PONG. The second should return OCMemcacheRedis. The third shows the installed version and whether maintenance mode is off. If the locking value comes back empty or points at a different cache, check that REDIS_HOST is set on the app container and that both containers share the same Compose network, which they do by default.

Nextcloud redis caching also covers the distributed cache, not just locking. You can confirm it is in use by uploading a folder of files, syncing it with the desktop client, and watching the admin overview page for warnings. The warning most people meet is the one about transactional file locking not being configured, which appears when Redis is missing or unreachable. If that warning shows up after you set REDIS_HOST, recreate the app container instead of restarting it, because environment changes only apply on recreation.

While you are here, look at the background jobs setting under Settings, Administration, Basic settings. It should say Cron, not AJAX. AJAX runs jobs when someone loads a page, which is slow and unreliable. The cron container handles this, and you can confirm it by checking that the “last job execution” time stays recent after a few minutes.

Backups, Updates, and Common Problems

Back up two things: the PostgreSQL database and the nextcloud-data volume. A file copy of the volume alone is not a working backup, because the database holds the file list, shares, and metadata that give those files meaning. For the database side, our guide to backing up PostgreSQL with pg_dump explains the dump command and the restore steps; run the dump from the db container on a schedule and copy the output off the server. Test a restore at least once, before you need it for real.

Update in this order: back up first, then pull new images, then recreate the containers. Never jump more than one Nextcloud major version at a time, and read the release notes for the version you are moving to. If an update fails, stop the stack, restore the database dump and the data volume from the backup you just took, and work out what went wrong before you try again.

Three problems come up most often. First, the app container starts before the database is ready and logs a connection error. Waiting a minute and restarting the app container usually fixes it, and a healthcheck on the db service makes the ordering stricter. Second, uploads fail with a disk-full error even though the VPS still shows free space. Check the volume location and the Docker data directory, because the disk that fills up is not always the one you checked. Third, the site loads over HTTP after you add HTTPS. That is almost always a trusted domain or trusted proxy setting, so compare the values in the compose file with the domain in the browser address bar.

A nextcloud postgresql docker stack like this one is a solid base for personal file sync, shared folders for a small team, and document storage you control. Keep the images updated, keep the backups tested, and check the admin overview page after every change.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.