Posts

Nextcloud setup

🚀 Self-Hosted Nextcloud with MariaDB Using Docker Compose

In this guide, we’ll set up a fresh, clean Nextcloud instance on your server using Docker Compose. This includes MariaDB for your database, persistent data storage, and trusted domain setup — all the essentials for your personal cloud!

🗂️ Step 1: Create a Working Directory

mkdir -p ~/nextcloud-docker && cd ~/nextcloud-docker

📝 Step 2: Create the docker-compose.yml File

Create and open the file:

nano docker-compose.yml

Paste this content:

version: '3.8'

services:
  db:
    image: mariadb:10.6
    container_name: nextcloud_db
    restart: always
    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
    volumes:
      - db_data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: yourStrongRootPassword
      MYSQL_PASSWORD: yourStrongDbPassword
      MYSQL_DATABASE: nextcloud
      MYSQL_USER: nextcloud

  app:
    image: nextcloud:latest
    container_name: nextcloud
    ports:
      - 8080:80
    links:
      - db
    volumes:
      - nextcloud_data:/var/www/html
      - /srv/dev-disk-by-uuid-c1dc714b-0922-467e-817b-761dff2a1b13/nextcloud/data:/var/www/html/data
    environment:
      MYSQL_PASSWORD: yourStrongDbPassword
      MYSQL_DATABASE: nextcloud
      MYSQL_USER: nextcloud
      MYSQL_HOST: db
    restart: always

volumes:
  db_data:
  nextcloud_data:

⚠️ Important: Replace yourStrongRootPassword and yourStrongDbPassword with secure passwords.

▶️ Step 3: Start Everything

docker compose up -d

🔍 Step 4: Check Status

docker ps

🌐 Step 5: Finish Setup in Browser

Open your browser and go to:

http://your-local-ip:8080

Then:

  • Create an admin user
  • Use the following DB settings:
DB User: nextcloud
DB Password: (yourStrongDbPassword)
DB Name: nextcloud
DB Host: db

🔥 Resetting After Errors (Optional)

Error: Login is invalid because files already exist for this user

This means leftover data is causing issues. Let’s fix it:

1️⃣ Stop and Remove Everything

cd ~/nextcloud-docker
docker compose down

2️⃣ Delete Old Data

rm -rf /srv/dev-disk-by-uuid-c1dc714b-0922-467e-817b-761dff2a1b13/nextcloud/data

3️⃣ Remove Docker Volumes

docker volume rm nextcloud-docker_nextcloud_data
docker volume rm nextcloud-docker_db_data

4️⃣ Recreate Data Directory

mkdir -p /srv/dev-disk-by-uuid-c1dc714b-0922-467e-817b-761dff2a1b13/nextcloud/data
chown -R 33:33 /srv/dev-disk-by-uuid-c1dc714b-0922-467e-817b-761dff2a1b13/nextcloud/data

5️⃣ Restart Everything

docker compose up -d

✅ Fix: Access Through Trusted Domain (cloud.saurabthakur.com)

1️⃣ Enter the Nextcloud Container

docker exec -it nextcloud bash

2️⃣ Install nano (optional)

apt update && apt install nano -y

3️⃣ Edit config.php

nano /var/www/html/config/config.php

4️⃣ Modify trusted_domains

Change it like this:

'trusted_domains' =>
array (
  0 => '192.168.0.8:8080',
  1 => 'cloud.saurabthakur.com',
),

Save with Ctrl + O, then Enter, then exit with Ctrl + X.

5️⃣ Restart Container

exit
docker restart nextcloud

🆕 Updating Nextcloud via Docker

If a new version is available, update with:

docker compose down
docker pull nextcloud
docker pull mariadb
docker compose up -d

🎉 Done!

Your personal cloud is now up and running securely with your custom domain.

Need Help Next?

  • 🔒 HTTPS with Cloudflare Tunnel
  • 📲 Auto smartphone photo/video backup
  • ⚡ Redis cache for faster performance

Nextcloud Permission Issue Prevention Guide

Step 1: Ensure the .ncdata File Exists

Check if the .ncdata file exists in your Nextcloud data directory. If it’s missing, create it manually:

cd /srv/dev-disk-by-uuid-c1dc714b-0922-467e-817b-761dff2a1b13/nextcloud/data
ls -la

# If .ncdata is missing:
echo "# Nextcloud data directory" > .ncdata

Step 2: Fix Permissions for Nextcloud Data Directory

Ensure that the data directory is owned by the www-data user (UID 33) and has correct file/directory permissions.

From Host (Non-Docker)

sudo chown -R www-data:www-data /srv/dev-disk-by-uuid-c1dc714b-0922-467e-817b-761dff2a1b13/nextcloud/data
sudo find /srv/dev-disk-by-uuid-c1dc714b-0922-467e-817b-761dff2a1b13/nextcloud/data -type d -exec chmod 750 {} \;
sudo find /srv/dev-disk-by-uuid-c1dc714b-0922-467e-817b-761dff2a1b13/nextcloud/data -type f -exec chmod 640 {} \;

From Inside Docker Container

docker exec -it nextcloud bash
chown -R 33:33 /var/www/html/data
find /var/www/html/data -type d -exec chmod 750 {} \;
find /var/www/html/data -type f -exec chmod 640 {} \;

Step 3: Check Disk Space

Ensure the disk isn’t full. If it is, Nextcloud won't function properly.

df -h

Step 4: File System Check

If you've experienced power loss or filesystem errors, check the disk with:

sudo fsck /dev/sdX

Replace /dev/sdX with your actual disk identifier.

Step 5: Mount Persistence

If your data drive is an external or additional disk, ensure it's mounted consistently via /etc/fstab so that it doesn't get missed on reboot.

Step 6: Verify datadirectory in config.php

Ensure that Nextcloud is pointing to the correct data directory by checking the config.php inside the container.

docker exec -it nextcloud bash
nano /var/www/html/config/config.php

Check for this line:

'datadirectory' => '/var/www/html/data',

By taking these proactive steps, you'll avoid common permission issues and ensure stable Nextcloud operation.

Let me know in the comments or reach out — I’ll help you build your dream NAS! 💻☁️

About the author

Saurab Thakur
Student, Photographer by Hobby, Blogger / Content Writer

Post a Comment