Running Nextcloud using Docker Compose

In this tutorial, we will show you how to run Nextcloud on your system using Docker Compose.

Nextcloud Docker Compose

Nextcloud is a popular self-hosted alternative to cloud solutions such as Dropbox, Office 365, and Google Drive.

Using this software, you have a handy web interface to upload all your files. It even features a web-based version of LibreOffice, allowing you to edit your documents, spreadsheets, and more, all from its web interface.

There are various ways that you can install Nextcloud on your system. In fact, we have guides for Ubuntu and the Raspberry Pi if you want to do a bare metal install. However, this guide will focus on using Docker to run this self-hosted cloud system.

Running Nextcloud within a Docker container has several advantages that we will quickly go over.

  • The first is that it is super easy to update. You can upgrade to the latest version of Nextcloud with just a couple of simple commands.
  • Secondly, it makes the set-up process super straightforward. You don’t have to worry about setting up a web server and PHP, as that’s all within the container.
  • Thirdly, containerizing everything means you don’t have to install software directly into your operating system. This helps, especially when using software that relies on different versions of PHP.

By the end of this tutorial, you will have Nextcloud running on your device within a Docker container.

Installing and Running Nextcloud using Docker

Over the following sections, we will be walking you through the process of installing and running Nextcloud by using a Docker container.

This process is written with Linux operating systems in mind, and you must be using the terminal. If you run this on a desktop system, you can often open the terminal by pressing CTRL + ALT + T.

Installing Docker to your System

1. The easiest way to install Docker to your system is to utilize the following command. We will need Docker to run Nextcloud later on in this guide.

This command downloads and runs a script to install Docker to your system.

curl -sSL https://get.docker.com | sh

2. After installing Docker, you must ensure your current user is added to the Docker group.

You can make this change by using the usermod command as shown below.

sudo usermod -aG docker $USER

3. Group changes only take effect once you log out and back in.

If you are using an SSH connection, this is as simple as using the following command.

logout

Alternatively, you can restart your machine by running the command below to ensure the group change takes effect.

sudo reboot

Preparing your System for the Nextcloud Docker Container

4. Our next step is to create a directory to store our Compose file for the Nextcloud Docker container.

You can create this folder by using the mkdir command within the terminal.

sudo mkdir -p /opt/stacks/nextcloud

5. After creating the directory, change to it by running the command below.

cd /opt/stacks/nextcloud

Setting up the Docker Proxy

6. To run Nextcloud through Docker while retaining SSL support, we must set up a proxy using NGINX.

Since we need to make some changes to the configuration for the default NGINX Proxy, we must write our own Docker file. First, create a directory to store this Dockerfile.

sudo mkdir proxy

Writing the Dockerfile

7. The first thing we want to do in our new proxy directory is begin writing the Dockerfile.

Docker will use this file to build a new image using the lines we specify within it.

sudo nano proxy/Dockerfile

8. Within this file, you will want to type in the following lines.

The first line tells Docker the image from which we want to build this new image. In our case, we are using the NGINX Proxy container.

Next, we want to copy our custom config file over into the NGINX proxy container.

FROM nginxproxy/nginx-proxy:alpine

COPY uploadsize.conf /etc/nginx/conf.d/uploadsize.conf

9. Once you have added the lines above to the file, save and quit by pressing CTRL + X, Y, and then ENTER.

Writing the Upload Size Config File

10. Next, we must write the NGINX config file that our new Dockerfile will copy.

Begin writing this configuration file by typing in the command below.

sudo nano proxy/uploadsize.conf

11. Within this file, add the following two lines.

These lines set the max body size to 10 gigabytes, allowing large uploads. We also disable the proxy buffering, so the request is immediately passed on to our Nextcloud Docker container.

client_max_body_size 10G;
proxy_request_buffering off;

12. After adding the lines above, you can save and quit by pressing CTRL + X, Y, and finally the ENTER key.

Generating a Password for MySQL

13. For our Nextcloud Docker container to talk with our database server, we must come up with a password.

You can use the following command to generate a somewhat secure password.

gpg --gen-random --armor 1 24 | base64

You should end up with a string looking somewhat like we have shown below. Copy this string, as you will need it for the next section.

UlFWSDBCMXF0dXlLaURieFJwbytURGUzbzJzN21FeisK

Writing a Docker Compose File for Nextcloud

14. You can now begin writing the Docker Compose file for Nextcloud. A compose file is like a set of instructions for Docker to follow.

This software relies on quite a few different containers, so there will be numerous lines you will have to add to this file.

sudo nano compose.yaml

15. You have two choices for setting up Nextcloud using Docker.

  • The first is to use a Signed Certificate from Let’s Encrypt. This will require you to have a domain name pointed to your Nextcloud server and ports 80 and 443 open on your server.

    This is the best option if you want a verifiable, secure connection for your server.
  • The other option is to use a self-signed certificate. This certificate is signed by your own server and won’t be verifiable by any web browser unless you manually install the certificate.

    The bonus of this method is that you don’t need outside access to your Nextcloud server, allowing you to add SSL support without a domain name or opening ports.

Using a Signed-Certificate from Lets Encrypt

a. In this section, we will set up your Nextcloud Docker Compose file to request a certificate using Lets Encrypt.

As you type out this file, you must replace the following values.

  • <SQLPASS>: Replace this with a password for Nextcloud to talk with your SQL Server. We generated a usable, random string back in step 13 of this tutorial.
  • <HOSTNAME>: Next, you must replace this value with the hostname you want to use. For example, we could use “nextcloud.pimylifeup.com“.
  • <EMAIL>: Replace this value with an email address that Lets Encrypt will associate with the generated certificate. It will use this email to alert you of any issues with your certificate.
  • <DATA>: Next, you must replace this text with the full path to where you want files to be stored on your system.
version: '3'

services:
  db:
    image: mariadb:10.6
    restart: always
    command: --transaction-isolation=READ-COMMITTED --log-bin=binlog --binlog-format=ROW
    volumes:
      - db:/var/lib/mysql
    environment:
      - MARIADB_RANDOM_ROOT_PASSWORD=yes
      - MYSQL_PASSWORD=<SQLPASS>
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud

  redis:
    image: redis:alpine
    restart: always

  app:
    image: nextcloud:apache
    restart: always
    volumes:
      - <DATA>:/var/www/html/data
      - nextcloud:/var/www/html
    environment:
      - VIRTUAL_HOST=<HOSTNAME>
      - LETSENCRYPT_HOST=<HOSTNAME>
      - LETSENCRYPT_EMAIL=<EMAIL>
      - MYSQL_PASSWORD=<SQLPASS>
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - MYSQL_HOST=db
      - REDIS_HOST=redis
    depends_on:
      - db
      - redis
    networks:
      - proxy-tier
      - default

  cron:
    image: nextcloud:apache
    restart: always
    volumes:
      - nextcloud:/var/www/html:z
    entrypoint: /cron.sh
    depends_on:
      - db
      - redis

  proxy:
    build: ./proxy
    restart: always
    ports:
      - 80:80
      - 443:443
    labels:
      com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy: "true"
    volumes:
      - certs:/etc/nginx/certs:z,ro
      - vhost.d:/etc/nginx/vhost.d:z
      - html:/usr/share/nginx/html:z
      - /var/run/docker.sock:/tmp/docker.sock:z,ro
    networks:
      - proxy-tier

  letsencrypt-companion:
    image: nginxproxy/acme-companion
    restart: always
    volumes:
      - certs:/etc/nginx/certs:z
      - acme:/etc/acme.sh:z
      - vhost.d:/etc/nginx/vhost.d:z
      - html:/usr/share/nginx/html:z
      - /var/run/docker.sock:/var/run/docker.sock:z,ro
    networks:
      - proxy-tier
    depends_on:
      - proxy

volumes:
  nextcloud:
  db:
  certs:
  acme:
  vhost.d:
  html:

networks:
  proxy-tier:

b. After typing out the above lines, you can save and quit by pressing CTRL + X, Y, and finally, the ENTER key.

Using a Self-Signed Certificate

a. If you want to use a self-signed certificate, you will want to use the following lines within the Compose file.

  • <SQLPASS>: You must replace this with a password for Nextcloud to communicate with the SQL database. We generated a possible value back in step 13.
  • <HOSTNAME>: Replace this value with a hostname for your Nextcloud server to utilize. You could use something like “nextcloud.local“. You will still be able to access Nextcloud through your IP address.
  • <DATA>: Replace this with the full path to where you want to store your Nextcloud files.
version: '3'

services:
  db:
    image: mariadb:10.6
    restart: always
    command: --transaction-isolation=READ-COMMITTED --log-bin=binlog --binlog-format=ROW
    volumes:
      - db:/var/lib/mysql
    environment:
      - MARIADB_RANDOM_ROOT_PASSWORD=yes
      - MYSQL_PASSWORD=<SQLPASS>
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud

  redis:
    image: redis:alpine
    restart: always

  app:
    image: nextcloud:apache
    restart: always
    volumes:
      - <DATA>:/var/www/html/data
      - nextcloud:/var/www/html
    environment:
      - VIRTUAL_HOST=<HOSTNAME>
      - MYSQL_PASSWORD=<SQLPASS>
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - MYSQL_HOST=db
      - REDIS_HOST=redis
    depends_on:
      - db
      - redis
    networks:
      - proxy-tier
      - default

  cron:
    image: nextcloud:apache
    restart: always
    volumes:
      - nextcloud:/var/www/html:z
    entrypoint: /cron.sh
    depends_on:
      - db
      - redis

  proxy:
    build: ./proxy
    restart: always
    ports:
      - 80:80
      - 443:443
    environment:
      - DEFAULT_HOST=<HOSTNAME>
    volumes:
      - certs:/etc/nginx/certs:z,ro
      - vhost.d:/etc/nginx/vhost.d:z
      - html:/usr/share/nginx/html:z
      - /var/run/docker.sock:/tmp/docker.sock:z,ro
    depends_on:
      - omgwtfssl
    networks:
      - proxy-tier

  omgwtfssl:
    image: csckcac/omgwtfssl
    restart: "no"
    volumes:
      - certs:/certs
    environment:
      - SSL_SUBJECT=<HOSTNAME>.local
      - CA_SUBJECT=my@example.com
      - SSL_KEY=/certs/<HOSTNAME>.local.key
      - SSL_CSR=/certs/<HOSTNAME>.local.csr
      - SSL_CERT=/certs/<HOSTNAME>.local.crt
    networks:
      - proxy-tier

volumes:
  nextcloud:
  db:
  certs:
  acme:
  vhost.d:
  html:

networks:
  proxy-tier:

b. Once you have configured the Docker compose file for Nextcloud, you can save and quit by pressing CTRL + X, then Y, and then pressing the ENTER key.

Starting Nextcloud

16. After writing your Compose file, you can start Nextcloud on your device by running the following command.

Docker will download and start all the containers specified within your compose file. This can take a few minutes to complete, so please be patient.

docker compose up -d

We use the “-d” option to detach Docker from the terminal once it has downloaded and started all containers.

Accessing the Nextcloud Web Interface

17. Now that you have gotten Nextcloud up and running using Docker, you must know your IP address or domain name.

If you plan on connecting using an IP address, you can use the hostname command below to get it.

hostname -I

18. Once you know whether you are using a domain name or IP, go to the following address in your favourite web browser.

https://<IPADDRESS OR DOMAINNAME>/

Initial Setup of your Docker Nextcloud Installation

19. When you first access Nextcloud, you will be asked to create an admin account. Type in the username and password (1.) you want to use to access Nextcloud.

Once you are happy with the details you have entered, click the “Install” button (2.).

Create Nextcloud Admin Account

20. Nextcloud can take a few minutes to finish installing the database and setting up some initial files.

Installing

21. At this point, you finally have Nextcloud up and running on your device using a Docker container.

Nextcloud Installed using Docker Compose

Updating Nextcloud using Docker Compose

One of the main advantages of using Docker Compose to install Nextcloud on your system is that it makes updating the software very easy.

The following steps will show you how to pull the latest version of Nextcloud and move your running container to the new image.

1. Before you update your Nextcloud Docker container you must change to the directory where we wrote the Compose file.

If you have followed our tutorial exactly, you should be able just to use the command below.

cd /opt/stacks/nextcloud

2. We now need to tell Docker to pull the latest version of all the containers within the Compose file.

docker compose pull

3. Finally, to get Docker to move your current running Nextcloud to the new container, you need to use the following command.

Docker will detect the new image and automatically move containers to their new versions.

docker compose up -d

Conclusion

Hopefully, you will now have full access to Nextcloud on your device. Running this software through Docker has made setting up and running on your device simple.

Nextcloud is a powerful tool that allows you to upload and manage your files in one central location. It is packed with features that make it more than just a file server.

Please feel free to comment below if you have had any issues getting Nextcloud to run using Docker.

If you found this tutorial helpful, we highly recommend checking out our other tutorials and Linux guides.

Leave a Reply

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