How to configure Nextcloud using Docker

Submitted on Mon, 04/22/2024 - 22:55

Tags

Nextcloud is a web based file server which offers multiple features including chat, video conference and file sharing.

To run nextcloud in Docker, we will have to create two containers. One for the web server and another one for the database server. Before we run the containers, we create volume and network resources needed by the containers.

Volume will make sure of data persistence and the network will make sure that our containers have fixed IP. Having static IP will make sure that the web server can always connect to the database server. Containers, if not assigned static IP, can have their IP change which can cause issues with the application hosted.

1. Create Docker volumes for data persistence.

docker volume create nextweb

docker volume create nextdb

2. Create Custom Docker network for static IPs for servers.

docker network create --subnet 11.11.11.0/29 nextnet

Here, we create a /29 network and name it nextnet. In this network, the first IP address shall be assigned for a virtual gateway needed by Docker to connect to host OS's network. Thus, 5 containers can be connected in this network, making the maximum nodes in the network as 6.

We will later assign two static IPs from this network for the two containers.

3. Run a docker container for nextcloud's web server.

docker run -dit -p 4000:80 -v nextweb:/var/www/html --name nextsrv --network nextnet --ip 11.11.11.2 --restart unless-stopped nextcloud

Here, we have mapped host OS's port 4000 to port 80 of the container. We also used our volume nextweb to map the folder /var/www/html where the nextcloud's files will be stored. The container shall be in a network called nextnet which was created earlier and it was assigned an IP address 11.11.11.2. The --restart unless-stopped argument will make the containers auto start when in stopped state except when the containers are manually stopped. This will make sure that the container starts automatically in case the host OS restarts including cases of power failure. The last argument is the name of the image to be used. If the image is not available locally, it will pull the image from Docker registry at hub.docker.com.

4. Run a docker container for nextcloud's database server.

docker run -dit -v nextdb:/var/lib/mysql -e MARIADB_ROOT_PASSWORD=nepal --network nextnet --ip 11.11.11.3 --name nextdbsrv --restart unless-stopped mariadb

Here, we have not performed any port mapping as it is a good practice not to expose any ports of the database server. A volume named nextdb was mapped to a folder /var/lib/mysql where the database files shall reside. An environment variable named MARIADB_ROOT_PASSWORD has been set as nepal. The container shall also be part of the same nextnet network with a fixed IP address of 11.11.11.3. Restart policy remains the same as for the earlier container.

Thus the web server and database server shall communicate with each other through the nextnet network at 11.11.11.0/29.

5. Log into the database server container.

docker exec -it nextdbsrv bash

6. Enter the mariadb shell as the root user.

mariadb -u root -p

7. Type the password and press enter.

Note: In Linux, no characters are shown while typing the password (not even stars).

8. Type the following SQL to create a database named next. Don't forget to end the statement with a semicolon.

create database next;

9. Exit the mariadb shell.

exit

10. Exit the container.

exit

11. Go to web browser and access the nextcloud's web server at following address:

localhost:4000

Note: Instead of localhost, the IP address of the host OS can also be used. This will make the nextcloud application accessible through the network by default. If the application is set up using localhost domain, a config file will have to be updated later inside the web server container.

12. Create an admin account using username and password of choice.

13. Select MySQL/MariaDB as database type and provide following details during the setup:

  • Database user: root
  • Database password: nepal
  • Database name: next
  • Database host: 11.11.11.3

nextcloud

Note: If SQlite is chosen as the type of database, no settings are required. However, sqlite is only suitable for learning purposes and not recommended for production servers. One major limitation of sqlite database is that in any table the maximum number of rows allowed is 264.

If the correct details are provided, the next screen would look like the following. Proceed with the nextcloud setup.

next

After the setup is done, click on the link below to learn how to setup HTTPS protocol with a registered domain for accessing NextCloud.

SSL configuration for Nextcloud server