Docker - Setting Nextcloud



Nextcloud is a free, open-source, and self-hosted cloud storage platform that allows users to store, share, and sync their files, contacts, etc., across multiple devices.

Like Google Drive and Dropbox, it enables file sharing and collaboration but with the extra benefit of complete control of data and infrastructure. However, setting up Nextcloud for different environments can pose a real challenge in terms of both management and scalability.

Here comes Docker where you can containerize your Nextcloud instance to behave the same way across all the production environments without deploying it separately for each environment. This chapter takes you through dockerizing Nextcloud so that you can easily manage it across multiple environments.

Prerequisites for Dockerizing Nextcloud

Make sure you have the following before starting this guide −

  • Docker − Make sure you have Docker installed on your local machine. It would be beneficial if you have a basic knowledge of Docker too.
  • Docker Compose − Make sure you have Docker Compose installed in your machine if you want to set up Nextlcloud with Docker via Docker Compose.
  • Command Line Interface (CLI) − A basic knowledge of the Command Line Interface or CLI and its commands would help you better understand this guide.
  • Text Editor − A text editor like VSCode or Sublime Text would be required for writing config files.

Setting up a Nextcloud Project

Before installing Nextcloud on Docker, you can create a Nextcloud project on your local machine. This will work as a Docker Image for Nextcloud.

Step 1: Create a Project Directory

Create a project folder or directory that will store all the files related to Nextcloud. Use the following command on CLI to create the directory.

# This command will create a folder named nextcloud-docker
$ mkdir nextcloud-docker

# This command will take you inside the folder
$ cd nextcloud-docker
Setting up Nextcloud Project 1

Step 2: Create a basic Dockerfile

Next, you need to create a basic docker file which will be populated later on. A Dockerfile is a script that holds a set of instructions and is used as a Docker image.

# Create a Dockerfile in your project directory 

$ touch Dockerfile
Setting up Nextcloud Project 2

Open your Dockerfile in any text editor and add the following content to it −

# Base image for Nextcloud
FROM nextcloud:latest

# Install any additional dependencies (optional)
RUN apt-get update && apt-get install -y \
   vim \
   curl \
   && apt-get clean

# Copy your custom configuration files (if any)
# COPY config/ /var/www/html/config/

# Expose the default Nextcloud port
EXPOSE 80

This Dockerfile exposes the port 80 and sets the base image to the Nextcloud image.

Step 3: Build a custom Docker Image

Now that you have created your Dockerfile, you can create your own custom Docker image with the following command −

# Build the custom Nextcloud Docker image named nextcloud

$ docker build -t my-nextcloud .
Build Custom Nextcloud Docker image

Step 4: Run the Nextcloud container

$ docker run -d --name nextcloud -p 8080:80 my-nextcloud
Run Nextcloud Container 1

Where,

  • -d − represents the detached mode
  • --name nextcloud − represents the name of your container
  • -p 8000:80 − mapping of your localhost port (8000) to the Nextcloud container"s port (80). On visiting http://localhost:8080, you are accessing Nextcloud"s container
  • my-nextcloud − name of the docker image that is used to create the container
Run Nextcloud Container 2

Using a pre-built Docker Image

Instead of creating a custom project and docker image to install Nextcloud in Docker, you can use the official Docker image for Nextcloud which is pre-built and ready to use.

Step 1. Pull the Nextcloud Image

Before running a Nextcloud container, you need to pull the pre-built official Docker image.

$ docker pull nextcloud
Using pre-built Nextcloud Docker Image 1

Step 2: Run the Nextcloud Container

Once you pull the Docker image, it"s time to run the Nextcloud container.

$ docker run -d --name nextcloud -p 8080:80 nextcloud
Using pre-built Nextcloud Docker Image 2

This will run the Nextcloud container on localhost port 8000 which is mapped with the Docker container port 80.

Setting up Nextcloud using Docker Compose

Step 1. Create a .env file

Create an .env file to store all the database environment variables. You can directly use them in your config file too but exposing your env variables is not recommended due to security reasons.

$ touch db.env

Add the following environment variables in this file. You can later add the path of this file in your config file.

MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: nextcloud
MYSQL_USER: nextcloud
MYSQL_PASSWORD: nextcloudpassword

Step 2. Create a docker-compose.yml file

Next, create the docker-compose.yml file in your nextcloud-docker directory.

$ touch docker-compose.yml
Setting up Nextcloud using Docker Compose 1

Your file structure will look like this −

nextcloud-docker/
 Dockerfile
 db.env
 docker-compose.yml

Step 3: Populate .yml file

Add the following configuration to your docker-compose.yml file.

version: "3"

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:
         - db:/var/lib/mysql
      env_file:
         - db.env

   redis:
      image: redis
      restart: always
      command: redis-server --requirepass your_redis_password

   app:
      image: nextcloud:latest
      restart: unless-stopped
      ports:
         - 8080:80
      links:
         - db
         - redis
      volumes:
         - nextcloud:/var/www/html
      environment:
         - MYSQL_HOST=db
         - REDIS_HOST_PASSWORD=your_redis_password
      env_file:
         - db.env
      depends_on:
         - db
         - redis
volumes:
   db:
   nextcloud:
  • Version 3 − This is the stable version of the docker-compose used.
  • Services − Services represent the separate containers that make up this application
  • db − This represents the database used in the service, MariaDB in this case. The database is set to restart unless stopped explicitly. Volumes represent the docker volume for permanent data storage to prevent data loss once the container is deleted. The "env-file" is the location of the db.env file where all the db-related environment variables have been added.
  • redis − This service uses using Redis server for caching.
  • app − This is the Nextcloud app image which runs on port 80 and is mapped to localhost port 8000. MariaD and Redis services are linked and the volume"s name is Nextcloud.
  • volumes − These are the docker volumes for permanently storing the data. Two volumes named "db" and "nextcloud" are used for storing MariaDB and app data.

Step 4: Run docker-compose.yml file

Run your docker-compose.yml file using the following command:

$ docker-compose up -d
Setting up Nextcloud using Docker Compose 2

Conclusion

You have successfully dockerized your Nextcloud container, created a docker image, and ran it inside your docker container ensuring consistency across different environments. This approach allows you to easily update your docker instance by simply pulling your updated images and redeploying your container. Modularity, security, portability, and isolation are the key features provided by this approach.

FAQs on Setting Nextcloud in Docker

In this section, we have collected a set of Frequently Asked Questions on Setting Nextcloud in Docker followed by their answers.

1. Can I change the database from MariaDB to any other database like PostgreSQL?

Yes, you change your database from MariaDB to any other database like PostgreSQL. All you need to do is change the "db" configuration in the docker-compose.yml file.

Change the db image from MariaDB to PostgreSQL. You will also need to change the environment variables accordingly in the db.env file.

2. How can I diagnose and fix performance problems in a dockerized Nextcloud system?

To begin, use docker logs <container_name> to examine the container logs for the database and Nextcloud containers. Docker stats can be used to track resource consumption and identify any performance issues. Increasing the amount of RAM and CPU that are available or setting Redis up for greater caching are common ways to boost speed.

3. If Nextcloud operates in a Docker container, how can I manage file permissions there?

File permissions on your host computer might need to match the user and group IDs of the container when using Docker to run Nextcloud.

By changing the ownership of the Docker volumes or by adding the user directive to your Dockerfile or docker-compose.yml file, you may control permissions.

Advertisements