Setup NextCloud on Docker

08/14/2024 20:27:13 - Edited: 04/18/2025 15:01:50
Docker, NextCloud
3 min read

NextCloud is a self-hosted file sync and share solution. It offers a personal cloud solution with clients supporting desktop and mobile operating systems. The setup of its extended feature sets, such as the integrated office suite are not part of this article.

Prerequisites

In this article we will use Docker to host NextCloud. If you do not already have docker installed on your system, follow the guides in the Docker documentation.

Furthermore you should have a reverse proxy setup to direct traffic to the NextCloud container. Using a reverse proxy is especially useful when you have multiple services running on the same server. It also allows you to handle SSL/TLS certificates in a central place. This is important because NextCloud does not allow access via HTTP.

Currently my preferred way of doing this is using the reverse proxy Caddy because it automatically acquires a certificate for you. A basic setup for Caddy looks like this:

services:
  caddy:
    network_mode: host
    image: caddy:2
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
      - "443:443/udp"
    volumes:
      - <path to your caddyfile>/Caddyfile:/etc/caddy/Caddyfile
      - caddy_data:/data
volumes:
  caddy_data:

Just choose a directory on you server where you would like the caddyfile to reside and use that path for the bind mount.

The caddyfile contains the configuration for the reverse proxy. Its where you configure the domain name and the path to the NextCloud container.

{
    email your@email.com
}

yourdomain.com {
    reverse_proxy http://localhost:8080
}

Assuming you have configured NextCloud to run on port 8080 on your server, this will redirect all traffic to yourdomain.com to the NextCloud container. The email section is the email that will be used for LetsEncrypt certificates.

Setup NextCloud

With the prerequisites out of the way, we can start setting up NextCloud. The first step is to copy the example docker-compose file and either create a new file for it or use a tool such as Portainer to create a new stack.

version: '2'

services:
  db:
    image: mariadb
    restart: unless-stopped
    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW --innodb-file-per-table=1 --skip-innodb-read-only-compressed

    volumes:
      - <path to where you want to store the database>/data:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=MY_SQL_ROOT_PASSWORD #change this
      - MYSQL_PASSWORD=MY_SQL_PASSWORD #change this
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud

  app:
    image: nextcloud
    restart: unless-stopped
    ports:
      - 8080:80
    links:
      - db
    volumes:
      - <path to your config>/config:/var/www/html
    environment:
      - MYSQL_PASSWORD=MY_SQL_PASSWORD  #change this to match the mysql_password above
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - MYSQL_HOST=db

NOTE: Maria DB is used as the database backend for NextCloud. It is recommended to use Maria DB because certain features of NextCloud are only available with it. Do note that certain operating systems do not support Maria DB.

With this docker-compose file we can start the stack with docker-compose up -d or Deploy the stack when using Portainer.

When first starting NextCloud it may take a while because both the Maria DB and NextCloud images are rather large and need to be downloaded.

Initial Login

Once the stack is up and running, you can access NextCloud by going to http://yourdomain.com in your browser or through the local IP address. When initially logging in you will be prompted to create an admin account and have the option to install recommended apps. Once that is completed NextCloud will guide you through the remaining processes of creating new users and other great options that NextCloud offers.