Building NextCloud (Using Docker)

Since my existing NextCloud instance stopped working, I decided to reinstall it. I had been using a simple server provided by wappy, which was part of a shareholder benefit from GMO GlobalSign. However, due to the aging system, we had to migrate to a new system. Essentially, this was a simplified server without SSH access.

The new system transitioned to wadax, and as a result, NextCloud stopped functioning. Therefore, I will be installing NextCloud using Docker on a virtual machine in Oracle Cloud. It should also be possible to install NextCloud on other cloud services, as well as on Docker installed on Windows. NextCloud is a client-server software application that allows the creation and use of online storage. While it functions similarly to Dropbox, it does not offer off-premises online storage services. We will be using the official NextCloud image for this setup.

I will explain both the scenario where the database is hosted separately and a simple setup where both the database and NextCloud are installed on the same PC.

https://hub.docker.com/_/nextcloud

Target Directory Structure

The goal of this procedure is to create the following directory structure on the host side.

Reason for Copying Files to the Host

It is important to copy the files themselves to the host to ensure that NextCloud’s configurations and data are securely stored. This approach is crucial because, when the container is deleted or rebuilt in the future, the data will remain safe. While this step may not be necessary when using Docker Compose, in this case, we are performing a manual setup, so we are adopting this method.

Starting the NextCloud Container

First, start the NextCloud container with as few options as possible. This container will only be used to retrieve the related files for NextCloud. Therefore, this container will be deleted later.

docker run --name test -d nextcloud

Checking the Files Inside the Container

Next, access the container to check where the NextCloud-related files are stored.

docker exec -ti test bash

Verify that the files exist in the following directory:

/var/www/html

Once you have confirmed the location of the files, exit the container.

Copying Files to the Host

If the files inside the container are not copied to the host, there is a risk of data loss. In the past, when an empty directory on the host was mounted to a directory containing data in the container, the data in the container was overwritten and lost. Therefore, first, create a directory on the host and copy the NextCloud-related files from the container to it.

mkdir nextcloud
cd nextcloud
docker cp test:/var/www/html /root/nextcloud

Copying Directories and Making Corrections

When copying NextCloud-related files to the host, it is important to pay attention to the directory structure. Here, I will explain the correction method after mistakenly copying the files.

The Initial Mistake

At first, I tried copying the entire html directory to the host. However, this resulted in an incorrect directory structure, with files being placed in unintended locations.

docker cp test:/var/www/html /root/nextcloud

As a result of this command, a subdirectory named html was created inside the /root/nextcloud directory, and the NextCloud-related files were copied into it. This did not result in the expected directory structure.

Example of an Incorrect Directory Structure

First, let’s look at the directory structure that results from copying files incorrectly.

In this structure, the html directory is unnecessarily included. Ideally, the contents of the html directory should be placed directly inside the nextcloud directory.

Correct Directory Structure

Next, let’s look at the correct directory structure.

This structure is the final goal, and the steps will be followed to ensure that files and directories are arranged accordingly.

Correction and Proper Copying Method

To maintain the correct directory structure, the html directory should be deleted, and then the files should be copied again using the following command. The important point here is to include a period (.) at the end of the directory path. This period instructs the command to copy only the contents (subdirectories and files) of the html directory.

rm -rf html
docker cp test:/var/www/html/. /root/nextcloud

This command will copy the contents of the html directory directly into the /root/nextcloud directory, resulting in the desired directory structure.

Stopping and Removing the Container

Once the file copying is complete, stop and remove the container that was used.

docker stop test
docker rm test

Data Storage in Nextcloud Installation

When installing Nextcloud using Docker, a critical point to note is that all data, except for the database (such as uploaded files and configuration files), is stored in Docker volumes. A volume is a mechanism for storing data outside the container, ensuring that the data is not lost even if the container is deleted.

Data Storage Location

Typically, Nextcloud data is stored in unnamed Docker volumes. These volumes are created by default in the following location:

/var/lib/docker/volumes/

This directory contains all the volumes managed by Docker. However, by explicitly specifying where to store specific data, it becomes easier to manage.

Running the Container with a Specified Data Storage Location

To specify where the uploaded files and other data will be stored during the Nextcloud installation, you can start the container using the docker run command. The following command sets up the Nextcloud data to be stored in a designated directory on the host (/root/nextcloud).

docker run -d \
--name nc \
-p 5050:80 \
-v /root/nextcloud:/var/www/html \
nextcloud

Explanation of the Command

  • -d: Runs the container in the background.
  • --name nc: Specifies the name of the container as nc.
  • -p 5050:80: Maps port 80 (HTTP) of the container to port 5050 of the host. This allows you to access Nextcloud at http://<your-IP-address>:5050 on the host.
  • -v /root/nextcloud:/var/www/html: Mounts the /root/nextcloud directory on the host to the /var/www/html directory inside the container. This ensures that the Nextcloud web files are stored on the host, so the data is preserved even if the container is deleted.

Regarding the Database Location

In this example, database-related settings are not included. The reason is that the database exists in a separate location. Database settings can be specified during the initial setup of Nextcloud, and by entering the necessary information at that time, a connection can be established.

Setting Up Ingress Rules on Cloud

If you are hosting your server on Oracle Cloud, the firewall settings mentioned above are not sufficient. You also need to allow access to port 5050 in the Oracle Cloud network settings (Security List).

Accessing the Oracle Cloud Console

Log in to the Oracle Cloud management console and select the instance you want to configure.

Adding Rules to the Security List

Navigate to the “Security List” section and add an “Ingress Rule” for port 5050. This will allow external access to the specified port.

Database Configuration

Nextcloud uses a database to manage user information, settings, and other data. While we have skipped the database creation in this instance, it is essential to ensure the following:

Pre-creating the Database

You must pre-create the database that will be used by Nextcloud using an RDBMS (Relational Database Management System) such as MySQL or MariaDB.

Configuring External Connections

Make sure that the database allows external connections. This will enable the Nextcloud container to access the database.

Accessing Nextcloud

Once these settings are complete, open your web browser and navigate to the following URL to confirm that the Nextcloud installation screen is displayed:

http://<your-IP-address>:5050

From here, proceed with the initial setup of Nextcloud, entering the database information and completing the setup process.

However, an error occurred. The solution to this issue will be explained next.

Can’t write into config directory!
This can usually be fixed by giving the webserver write access to the config directory.

Handling Errors: File Write Permission Issues

When setting up Nextcloud, you may encounter the following error:

Error Message:

Can't write into config directory!
This can usually be fixed by giving the webserver write access to the config directory.

This error occurs when the web server (such as Apache or Nginx) does not have write permissions for the directory where Nextcloud’s configuration files are stored. Here, we’ll explain how to resolve this issue.

Cause of the Issue

This error happens because the ownership of the files and directories was changed to the root user on the host when the files were copied from the container. While the root user has administrative privileges, the web server typically runs under a user account called www-data, and this user does not have access to the files, leading to the error.

Solution

To resolve this issue, you need to change the ownership of all files and subdirectories within the Nextcloud directory to the www-data user. This will allow the web server to write to the files.

Command:

chown -R www-data:www-data /root/nextcloud
  • The chown command changes the ownership of files and directories.
  • The -R option applies this change to all files and subdirectories within the specified directory.
  • www-data:www-data means that both the owner and the group will be changed to www-data.
  • /root/nextcloud is the path to the directory where the Nextcloud files are stored.

By running this command, the web server will be able to access and write to the Nextcloud configuration files.

Steps After Resolving the Error

After changing the ownership, try accessing the Nextcloud installation page in your browser again. If the error is resolved, it should now function correctly.

Next, select the database type, such as MySQL or MariaDB, and enter the required information to proceed with the setup. Once you follow these steps, the Nextcloud installation will be completed.

External Integration Features of NextCloud

Nextcloud can integrate with various external services, such as:

  • DropBox: Used for backing up and sharing files in the cloud.
  • GoogleDrive: Can be integrated with Google’s cloud storage service.
  • CloudFlare: Used for security and performance enhancements.

By utilizing these integration features, you can make Nextcloud even more versatile.

If you also want to run the database as a container simultaneously, a simple setup using Docker Compose is recommended.

Simple Nextcloud Setup Guide Using Docker Compose

Here, we will introduce a method to easily set up Nextcloud using Docker Compose. There is no need to prepare a separate database. First, prepare the necessary files, and then start the services using Docker Compose. The linked GitHub repository (https://github.com/nextcloud/docker) hosts the official Docker image for Nextcloud. However, this repository does not include a docker-compose.yml file. The docker-compose file mentioned in the README is provided as a reference example and is not included in the repository’s code.

Necessity of Cloning

If you are setting up using docker-compose, cloning the repository might not be necessary, as the repository does not contain the intended docker-compose.yml file. Therefore, cloning may not be required.

Docker Compose vs. Docker Run

This repository primarily assumes that you will be setting up Nextcloud using docker run. If you want to use docker-compose after cloning the repository, you will need to create the docker-compose.yml file yourself based on the content provided in the README.

1. Preparing Docker Compose

To set up Nextcloud and MariaDB, you need to create a docker-compose.yml file. This file includes the configuration for both Nextcloud and the database.

First, copy the following content into a text editor, create a new file, and save it as docker-compose.yml.

Explanation of the Proposed docker-compose.yml File

Next, I will explain the contents of the docker-compose.yml file as proposed in the README.

version: '2'

volumes:
  nextcloud:
  db:

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:
      - MYSQL_ROOT_PASSWORD=
      - MYSQL_PASSWORD=
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud

  app:
    image: nextcloud
    restart: always
    ports:
      - 8080:80
    links:
      - db
    volumes:
      - nextcloud:/var/www/html
    environment:
      - MYSQL_PASSWORD=
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - MYSQL_HOST=db

This docker-compose.yml file provides a simple configuration to set up Nextcloud and MariaDB. Below is an explanation of each section.

app:

This is the Nextcloud application service. The Nextcloud code and data are stored in the nextcloud volume.

version: ‘2’

This specifies the version of Docker Compose.

volumes:

This defines the volumes used to persist data. The nextcloud volume stores the Nextcloud data, and the db volume stores the MariaDB data.

services:

This defines each service that makes up the application.

db:

This is the database service using MariaDB. The database data is stored in the db volume.

2. Starting Services with Docker Compose

After creating the file, the next step is to start the services using Docker Compose. Open a terminal (or command prompt), navigate to the directory where the docker-compose.yml file is located, and execute the following command:

docker-compose up -d

This command will start both the Nextcloud and MariaDB services in the background.

3. Accessing Nextcloud

Once the services are running, open a web browser and navigate to the following URL:

http://localhost:8080/

Here, you can proceed with the Nextcloud setup. Follow the on-screen instructions to enter the database information and complete the configuration.

Please share if you like it!
TOC