How to Self-Host Nextcloud with Docker Compose on Ubuntu

Last Updated: May 17, 2025

In this post, I’ll show you how to use Docker and its Compose plugin to host your own Nextcloud service. We’ll begin with a clean Ubuntu server environment.

Installing Docker using the Official Script

First, ensure that curl is installed on your server.

sudo apt update
sudo apt install curl

Next, we’ll install Docker using the official installation script.

curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh

To manage Docker as a non-root user (without needing sudo), add your current user to the docker group

# sudo groupadd docker
# it should be done by the script

sudo usermod -aG docker $USER
newgrp docker

Install Nextcloud

First, create a directory to store your Nextcloud data.

mkdir nextcloud
cd nextcloud

Next, create a docker-compose.yml file. Populate it with the following configuration:

services:
  nextcloud-database:
    image: mariadb
    container_name: nextcloud-database
    restart: always
    volumes:
      - ./database:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=<YOUR_PASSWORD_HERE>
      - MYSQL_PASSWORD=<YOUR_PASSWORD_HERE>
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud

  nextcloud-server:
    image: nextcloud
    container_name: nextcloud-server
    restart: always
    ports:
      - <CHOOSE_A_PORT>:80
    links:
      - nextcloud-database
    volumes:
      - ./server:/var/www/html
    environment:
      - MYSQL_PASSWORD=<YOUR_PASSWORD_HERE>
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - MYSQL_HOST=nextcloud-database

You need to replace <YOUR_PASSWORD_HERE> with your own strong, unique password, and change <CHOOSE_A_PORT> to the external port on which you want your Nextcloud service to be accessible.

Finally, run the following command to start your Nextcloud service in detached mode:

docker compose up -d

Configuring the Service

First, open your web browser and navigate to http://<YOUR_SERVER_IP>:<THE_PORT_YOU_CONFIGURED> (replacing <YOUR_SERVER_IP> with your server’s IP address and <THE_PORT_YOU_CONFIGURED> with the port you set in the docker-compose.yml file). Follow the on-screen instructions to set up your administrator account.

Next, create a script to handle Nextcloud’s background jobs using cron. The content of the script should be:

#! /bin/bash
sudo docker exec --user www-data nextcloud-server php -f /var/www/html/cron.php

Make the script executable using chmod (replace <YOUR_SCRIPT_NAME>.sh with your actual script filename):

chmod +x <YOUR_SCRIPT_NAME>.sh

Finally, edit your user’s crontab to run this script every 5 minutes (ensure you use the correct path to your script):

crontab -e
# Add this line
*/5 * * * * <PATH_TO_SCRIPT>/<YOUR_SCRIPT>.sh

(Optional) If you are routing traffic to Nextcloud through a reverse proxy (which might be your own HTTP server forwarding requests), you can edit the server/config/config.php file. Adding the following trusted_proxies configuration helps Nextcloud correctly process client information. Adjust the IP ranges if necessary to include your proxy server:

'trusted_proxies' => 
array (
  0 => '10.0.0.0/8',
  1 => '100.64.0.0/10',
  2 => '127.0.0.0/8',
  3 => '172.16.0.0/12',
  4 => '192.168.0.0/16',
),

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *