X
Popular Searches

How to Self-Host a Collaborative Cloud with Nextcloud and Docker

next cloud logo
monticello/Shutterstock.com

Nextcloud is an open-source file-sharing and collaboration platform to rival services like Google Workplace and Microsoft 365. Nextcloud lets you self-host your own collaborative cloud, so you don’t need to shackle yourself to a public provider.

A regular Nextcloud installation can be time-consuming to set up. Nextcloud is built on the classic LAMP stack, so you’ll need Apache, PHP, and MySQL, each with its own prerequisites. This can make it tricky to maintain the installation or use it alongside other workloads on your server.

Running Nextcloud as a Docker container simplifies the setup procedure and lets you isolate the installation from your other applications. Nextcloud has an official Docker image, which we’ll focus on in this article. The community linuxserver project also provides its own image with some pre-configured defaults.

Planning the Setup

The Nextcloud Docker image comes with a functioning web server setup. You’ll get Apache, PHP, and a pre-configured Nextcloud installation. An SQLite database is used by default.

While a basic docker run -d -p 80:80 nextcloud will bring up a ready-to-run server, this doesn’t include provision for persistent storage. The most critical aspect of Dockerized Nextcloud deployment is correct volume configuration so that you don’t lose your data.

Advertisement

In addition, the standard SQLite database is only suitable for small-scale usage. If you’re going to have more than a handful of users, provisioning a MySQL or PostgreSQL database will improve performance.

Let’s now build a Dockerized Nextcloud installation that runs on MySQL and uses Docker volumes to safely persist your data. If you’d rather use PostgreSQL, substitute references to MySQL below with their PostgreSQL counterparts.

Getting Set Up

You can deploy Nextcloud using basic Docker CLI commands. This quickly becomes tedious, though, not to mention difficult to remember in the future. Instead, using Docker Compose lets you define your configuration as code.

Make sure that you have Docker and Docker Compose installed on your system. Create a new directory to hold your Nextcloud configuration files. Add a docker-compose.yml file and paste in the following contents:

version: "3"

services:
  nextcloud:
    image: nextcloud:latest
    restart: unless-stopped
    ports:
      - 80:80
    environment:
      - MYSQL_HOST=mysql
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - MYSQL_PASSWORD=nextcloud
    volumes:
      - nextcloud:/var/www/html

  mysql:
    image: mysql:8.0
    restart: unless-stopped
    environment:
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - MYSQL_PASSWORD=nextcloud
      - MYSQL_ROOT_PASSWORD=nextcloud
    volumes:
      - mysql:/var/lib/mysql

volumes:
  mysql:
  nextcloud:

This Compose file encapsulates all the configuration for a safe Nextcloud installation. It provisions a MySQL database and configures Nextcloud to connect to it. You should set MYSQL_PASSWORD and MYSQL_ROOT_PASSWORD to secure custom values.

The entire /var/www/html directory is mounted as a Docker volume. Nextcloud stores its source, settings, and user data here. By turning the whole directory into a volume, Nextcloud’s self-updater will work correctly. Otherwise, you’d have to pull a new container image to update, as the self-updater wouldn’t be able to persistently replace Nextcloud’s files.

Advertisement

The Nextcloud server will bind to port 80 on your machine by default. You can use a different port, such as 8080, by updating the ports configuration:

- 8080:80

Now, you’re ready to deploy Nextcloud with Docker Compose:

docker-compose up -d

Wait while Compose pulls the images and starts your containers. You’ll then be able to access Nextcloud by visiting localhost in your browser.

First Run

The first time that you visit Nextcloud, you’ll be shown the default setup wizard. Enter a username and password for your first user account. If you want to install some core apps, including calendar, contacts, and Nextcloud Talk calls, leave the “Install recommended apps” checkbox ticked.

Click “Finish setup” to complete the installation process. It might take a few moments while apps are installed. Don’t close your browser tab until setup completes. Once Nextcloud’s ready, you’ll be shown a quick set of getting started slides. You’ll then be taken to the Nextcloud dashboard.

The dashboard offers you a centralized view of your cloud assets. Individual apps can surface content on the dashboard. You can get to all of your apps using the icons in the top-left corner.

To install extra apps, click your user profile icon in the top-right corner. Choose “Apps” from the menu. Click one of the categories in the left sidebar to see all of the available apps. Press “Download and enable” below any app’s card to add it to your Nextcloud instance.

Advertisement

Nextcloud admin settings are found by clicking your user profile icon and choosing “Settings” in the menu. Click the links under “Administrator” in the left sidebar to find management facilities. Your Nextcloud version and available updates are displayed on the “Overview” page.

Some Nextcloud administration tasks are invoked through the occ command-line binary. This is a PHP script within the Nextcloud source. You can interact with OCC using docker-compose exec without fully attaching to the container:

docker-compose exec --user www-data php occ occ-command-here

It’s important to include the --user flag so that occ is run as the same user as the Nextcloud web interface. Otherwise, you might encounter filesystem ownership and permissions errors.

Automating Configuration

Many of Nextcloud’s admin settings can be preset when you start the container. Beyond database setup, the image supports environment variables to configure a mail system, connect to remote object storage, and automatically create an initial administrator user. When you supply these values, you won’t need to provide them to the first-run setup wizard.

As an alternative to environment variables, you can use Docker secrets to set values in a more security-conscious way. Add your values to files and map them into the container using the secrets key in Compose. Then, update the environment section to read the injected secret files.

services:
  nextcloud:
    environment:
      - NEXTCLOUD_ADMIN_PASSWORD_FILE=/run/secrets/nextcloud_admin_password
secrets:
  nextcloud_admin_password:
    file: ./nextcloud_admin_password

When you’re using secrets, each Nextcloud environment variable should have _FILE appended to its name. This instructs Nextcloud to get the value from the referenced file path.

Security

The standard Docker image doesn’t set up SSL by default. It’s recommended that you deploy behind an SSL-terminating reverse proxy such as Apache, Nginx, or Traefik. This should then forward requests to your Nextcloud container.

Advertisement

The Docker image automatically works with requests that are proxied from the 10.0.0.8/72, 172.16.0.0/12, and 192.168.0.0/16 address spaces. If your proxy server has a different IP, you should add it to the TRUSTED_PROXIES environment variable when deploying Nextcloud. You’ll also need to set APACHE_DISABLE_REWRITE_IP=1.

These steps ensure that Nextcloud will handle rewrites properly. The Docker image’s usage instructions include more guidance on using Nextcloud with a proxy. You can check whether your installation is installed correctly using the Overview page in the admin center.

Managing Nextcloud Updates

When you’ve deployed Nextcloud with the Compose file above, you should be able to use the self-updater in the admin center to update Nextcloud. Bear in mind that this will only get you the latest Nextcloud source—the underlying container will remain the same.

It’s a good idea to periodically pull a new Docker image. This helps you avoid running outdated OS packages, which could be a security risk.

If you’re using Docker Compose, you can rerun docker-compose up with the --pull flag. Compose will automatically pull the new image and replace your current container if required.

docker-compose up -d --pull

Conclusion

Running a Nextcloud server gives you full control over your files. Beyond simple file sharing, Nextcloud also provides a full ecosystem of productivity apps. You’ll find email, calendar, notes, and tasks systems as well as a complete voice and video-calling solution.

Advertisement

Deploying Nextcloud with Docker simplifies the setup procedure and avoids polluting your host with a bare-metal LAMP stack. Once Nextcloud is installed, you’ll be able to connect from any of the supported desktop and mobile sync clients.

We’ve only covered the fundamentals of getting a Nextcloud server operational. If you want to learn more about installing apps and maintaining your installation, the Nextcloud administration manual provides exhaustive instructions.

James Walker James Walker
James Walker is a CloudSavvy IT contributor. He is the founder of Heron Web, a UK-based digital agency providing bespoke software development services to SMEs. He has experience managing complete end-to-end web development workflows with DevOps, CI/CD, Docker, and Kubernetes. Read Full Bio »

The above article may contain affiliate links, which help support CloudSavvy IT.