Nextcloud Docker Installation and Configuration Guide

Author

Reads 618

Modern data center corridor with server racks and computer equipment. Ideal for technology and IT concepts.
Credit: pexels.com, Modern data center corridor with server racks and computer equipment. Ideal for technology and IT concepts.

To install Nextcloud using Docker, you'll need to pull the official Nextcloud image from the Docker Hub registry. This image is available for both the amd64 and arm64 architectures.

First, ensure you have Docker installed on your system, as Nextcloud relies heavily on it. Docker provides a lightweight and efficient way to run applications like Nextcloud in containers.

Next, you'll need to create a new Docker network to connect your Nextcloud container to. This network will allow your Nextcloud container to communicate with other containers on your system.

Create a new directory to store your Nextcloud configuration files and initialize a new Nextcloud instance within it.

Explore further: Cloud Network Drive

Installation

Installing Nextcloud with Docker is a straightforward process. You'll need to install Docker Engine and Docker Compose on your system.

To start, you'll need to delete any other Docker installations, then prepare the repositories and update the package manager. This will ensure that your system is clean and ready for the installation.

Credit: youtube.com, Setting Up NextCloud on Docker w/ NGINX and Cloudflare for Remote Access!

Next, you'll need to install Docker Engine on Ubuntu. This involves adding Docker's official GPG key, updating the repositories, installing Docker Engine and its dependencies, and then executing a test container to verify the installation.

Once Docker Engine is installed, you can proceed with installing Nextcloud using Docker Compose. This involves creating a Docker Compose file (docker-compose.yaml) that defines the services and settings for your Nextcloud installation.

You'll also need to create an environment file (.env) that contains environment variables for your Nextcloud installation. This file is not included in the version control system, protecting sensitive data from accidental exposure.

Here's a step-by-step guide to installing Docker Engine on Ubuntu:

1. Add Docker's official GPG key

2. Update the repositories

3. Install Docker Engine and its dependencies

4. Execute a test container to verify the installation

By following these steps, you'll have Docker Engine installed on your Ubuntu system, and you'll be ready to install Nextcloud using Docker Compose.

For your interest: Nextcloud Set up

Configuration

Credit: youtube.com, DITCH Microsoft/Google with NextCloud AIO! - Server Setup Guide

To configure the Nextcloud server container, you'll need to create a service with a specific name and Docker image. The service should be based on the Docker image nextcloud and communicate with the network nextcloud_network.

You'll also need to specify the other services as dependencies to ensure the Nextcloud container is started last. This is important for the smooth operation of your Nextcloud setup.

To auto-configure your Nextcloud installation, you can use environment variables. One key environment variable is NEXTCLOUD_UPDATE, which is set to 0 by default.

Configuring the Container

You'll need to create a .env file for editing, which will store environment variables for Docker Compose. This file will be read when Docker Compose is executed later.

To configure the MariaDB service, you'll specify the names of environment variables in the environment section. These variables will be used by MariaDB to manage data created when using Nextcloud.

You can also configure the Nextcloud server container, which requires defining Docker volumes and environment variables. The Nextcloud image supports auto configuration via environment variables, allowing you to preconfigure key parameters relevant to initial installation.

Curious to learn more? Check out: Nextcloud Aio Docker Compose

Credit: youtube.com, Learn Docker in 7 Easy Steps - Full Beginner's Tutorial

Some key environment variables to consider are NEXTCLOUD_UPDATE, NEXTCLOUD_TRUSTED_DOMAINS, and NEXTCLOUD_DB_HOST. These variables can be set in the .env file to configure the Nextcloud server.

Here are some key environment variables to consider:

By configuring these environment variables, you can automate the setup of your Nextcloud server and ensure it's properly configured for use.

Configuring Let's Encrypt

Configuring Let's Encrypt is a crucial step in securing your Nextcloud server with HTTPS. You can add a new service block to your Docker Compose file to install Let's Encrypt.

To do this, add another service block with the name letsencrypt, based on the container letsencrypt-nginx-proxy-companion. This service should be dependent on the reverse proxy service and communicate in the same Docker network.

The service should also define the Docker volumes required for data exchange, and specify that it should be automatically restarted unless it was explicitly stopped. Most of the settings should be familiar at this point.

Credit: youtube.com, Protecting Your Pfsense Firewall: Free SSL Certificate Setup with Let's Encrypt

Here's a summary of the key settings for the letsencrypt service:

Once you've added the letsencrypt service block, you can save and quit your Docker Compose file by pressing CTRL + X, then Y, and then pressing the ENTER key.

Auto via Hooks

Auto configuration via hooks is a powerful feature in Nextcloud that allows you to automate tasks at different stages of the installation and upgrade process.

There are several hooks available, including pre-installation, post-installation, pre-upgrade, post-upgrade, and before-starting.

To use these hooks, you can add your script(s) to the corresponding hook folder, located at /docker-entrypoint-hooks.d in the container.

Only scripts ending with .sh and marked as executable will be executed.

You can also use volumes to mount script(s) from the host system inside the container.

Here are the available hooks:

  • pre-installation: Executed before the Nextcloud is installed/initiated
  • post-installation: Executed after the Nextcloud is installed/initiated
  • pre-upgrade: Executed before the Nextcloud is upgraded
  • post-upgrade: Executed after the Nextcloud is upgraded
  • before-starting: Executed before the Nextcloud starts

Note that only scripts located in the hook folder (not sub-folders) will be executed.

Database

The Nextcloud setup wizard allows connecting to an existing MySQL/MariaDB or PostgreSQL database, or you can use an external database container.

Credit: youtube.com, Docker Volume Backup Tutorial: Nextcloud Example

You can link a database container, for example, --link my-mysql:mysql, and then use mysql as the database host on setup.

To enable auto configuration, define your database connection via environment variables.

You must specify all of the environment variables for a given database, or the database environment variables will default to SQLite.

Here are the environment variables for each database type:

  • SQLite: SQLITE_DATABASE Name of the database using sqlite
  • MYSQL/Mar
  • MYSQL_DATABASE Name of the database using mysql / mariadb.
  • MYSQL_USER Username for the database using mysql / mariadb.
  • MYSQL_PASSWORD Password for the database user using mysql / mariadb.
  • MYSQL_HOST Hostname of the database server using mysql / mariadb.

PostgreSQL:

  • POSTGRES_DB Name of the database using postgres.
  • POSTGRES_USER Username for the database using postgres.
  • POSTGRES_PASSWORD Password for the database user using postgres.
  • POSTGRES_HOST Hostname of the database server using postgres.

Alternatively, you can load the values for these variables from files present in the container by appending _FILE to the environment variables.

For example, MYSQL_DATABASE_FILE will load the value for MYSQL_DATABASE from a file in the container.

Security

To set up a secure Nextcloud installation, consider using a reverse proxy in front of your Nextcloud container. This encrypts all traffic to clients.

We recommend using HTTPS with SSL encryption, which can be achieved through a reverse proxy. This setup makes your Nextcloud installation only reachable through the proxy.

You can mount manually generated certificates to the proxy or use a fully automated solution that generates and renews certificates for you. This automated solution can be a game-changer for those who want a hassle-free experience.

In our examples section, we have a setup using a reverse proxy, a container for Let's Encrypt certificate handling, database, and Nextcloud. This setup uses the popular nginx-proxy and acme-companion containers.

Check the documentation for these containers before using this setup, as it's essential for a smooth experience.

Storage

Credit: youtube.com, I think I found a Dropbox replacement with Nextcloud...

By default, Nextcloud stores all files in the /var/www/html/data/ directory. This is the primary storage location for your files.

Nextcloud allows the use of object storages like OpenStack Swift or Amazon S3-compatible implementations as primary storage. This can be a good option if you need more storage space or want to store your files on a cloud provider.

To use an external S3 compatible object store as primary storage, you'll need to set several variables. These include the name of the bucket, the region, hostname, port, access key, secret key, and more.

Here's a list of the variables you'll need to set for an S3 compatible object store:

You can also use an external OpenStack Swift object store as primary storage. To do this, you'll need to set a few variables, including the Swift identity endpoint, whether to automatically create the Swift container, and more.

Credit: youtube.com, 5 AWESOME DOCKER CONTAINERS for Synology NAS and homelabing

Here's a list of the variables you'll need to set for an OpenStack Swift object store:

  • OBJECTSTORE_SWIFT_URL: The Swift identity (Keystone) endpoint
  • OBJECTSTORE_SWIFT_AUTOCREATE (default: false): Whether or not Nextcloud should automatically create the Swift container
  • OBJECTSTORE_SWIFT_USER_NAME: Swift username
  • OBJECTSTORE_SWIFT_USER_PASSWORD: Swift user password
  • OBJECTSTORE_SWIFT_USER_DOMAIN (default: Default): Swift user domain
  • OBJECTSTORE_SWIFT_PROJECT_NAME: OpenStack project name
  • OBJECTSTORE_SWIFT_PROJECT_DOMAIN (default: Default): OpenStack project domain
  • OBJECTSTORE_SWIFT_SERVICE_NAME (default: swift): Swift service name
  • OBJECTSTORE_SWIFT_REGION: Swift endpoint region
  • OBJECTSTORE_SWIFT_CONTAINER_NAME: Swift container (bucket) that Nextcloud should store the data in

Networking

Networking is a crucial aspect of setting up a Nextcloud server with Docker. Two networks are defined, one for the frontend and reverse-proxy, and another for the frontend and backend to communicate.

The database container and Nextcloud frontend share a common network named "common", allowing them to communicate with each other. This network can be made internal, limiting the database container's access to the public internet.

To create a Docker network, you can use the command `create a docker network named net`. This network is used in the `docker-compose.yaml` file.

Here are the networks used in the setup:

  • `nextcloud_network` for the frontend and reverse-proxy
  • `common` for the database container and Nextcloud frontend
  • `net` for the reverse-proxy deployment

You can choose from different restart policies for the containers, such as `on-failure`, `unless-stopped`, or `always`.

Deployment

To deploy Nextcloud with Docker, you'll need to create a Docker network named "net". This network is used in the docker-compose.yaml file. You can create it by running the command "docker network create net".

Check this out: How to Create Onedrive

Credit: youtube.com, Make Nextcloud Using Docker-Compose in 5 Min

You can use either git or wget to download the compose files from the public GitHub repository. If you're using git, clone the whole repository and change your current directory to the Nextcloud folder.

The images used for deployment are mariadb:10.5.9 and nextcloud:21.0.0, which are the latest versions at the time of writing. You can find the compose file and env files in the repository, and it's recommended to download them to understand the setup.

To deploy Nextcloud, you'll need to create a Docker network and download the compose files. You can then use the docker-compose.yaml file to define the services and their interactions. The .env file contains the environment variables that are used in the Docker Compose instructions.

Here's a list of the environment variables that you'll need to set in the .env file:

  • TRUSTED_PROXIES: The subnet of the network, shared by the reverse proxy and this frontend.
  • OVERWRITEPROTOCOL: The overwriteprotocol parameter is used to set the protocol of the proxy. As we're using HTTPS, set this to HTTPS.

You can get the subnet using the command "docker network inspect net" and then extract the subnet from the output.

Finally, Deploying

Credit: youtube.com, Deploy - A series on deployment of code

Finally, deploying Nextcloud is a straightforward process. You'll need to start by creating a Docker container for the Nextcloud server.

The Nextcloud server consists of four containers: the nginx container, the letsencrypt companion container, the mariadb container, and the actual Nextcloud container.

You can start the container using the following command: `docker-compose up -d`. This will create a Docker network and start all the containers defined in the docker-compose.yaml file.

The list of running containers should contain the four containers defined in the docker-compose.yaml file.

Here are the four containers that make up the Nextcloud server:

  • nginx container
  • letsencrypt companion container
  • mariadb container
  • Nextcloud container

Each of these containers serves a specific purpose, and they work together to provide a fully functional Nextcloud server.

Post Deployment

After deploying your Nextcloud instance, you should see a screen similar to the one you'd see during a typical web install.

You won't see the usual web install UI that asks for database information like hostname, port, and database username and password because database environment variables are shared through the common .env file.

Uncheck the box that says "Admin user is not set" and install apps you need later from the app list.

A unique perspective: How to Check Space on Dropbox

Existing Installation Migration

Credit: youtube.com, How to use Docker and migrate your existing Apps to your Linux Server?

If you're already using Nextcloud and want to switch to a Docker setup, you're in luck. The process is relatively straightforward.

First, define your entire Nextcloud infrastructure in a compose.yaml file and run it with docker compose up -d to get the base installation, volumes, and database. This will give you a solid foundation to work from.

To restore your database, you'll need to use a mysqldump. You can do this by running a series of commands: docker compose cp ./database.dmp db:/dmp, docker compose exec db sh -c "mysql --user USER --password PASSWORD nextcloud < /dmp", docker compose exec db rm /dmp, docker compose cp ./database.dmp db:/dmp, and finally docker compose exec db sh -c "psql -U USER --set ON_ERROR_STOP=on nextcloud < /dmp".

Next, edit your config.php file to ensure everything is set up correctly.

To copy your data, run the following commands: docker compose cp ./data/ app:/var/www/html, docker compose exec app chown -R www-data:www-data /var/www/html/data, docker compose cp ./theming/ app:/var/www/html, docker compose exec app chown -R www-data:www-data /var/www/html/theming, and finally docker compose cp ./config/config.php app:/var/www/html/config.

Additional reading: Nextcloud Docker-compose

Computer server in data center room
Credit: pexels.com, Computer server in data center room

If you want to preserve the metadata of your files, you can copy the data directly on the host to the named volume using plain cp like this: cp --preserve --recursive ./data/ /path/to/nextcloudVolume/data.

Finally, if you have custom apps installed, you can copy them over using the following commands: docker compose cp ./custom_apps/ app:/var/www/html, and docker compose exec app chown -R www-data:www-data /var/www/html/custom_apps.

Here's a quick summary of the steps to migrate your existing Nextcloud installation to a Docker setup:

  1. Define your Nextcloud infrastructure in a compose.yaml file and run it with docker compose up -d.
  2. Restore your database using a mysqldump.
  3. Edit your config.php file.
  4. Copy your data, preserving metadata if necessary.
  5. Copy custom apps (if applicable).

Maintenance

Maintenance is a crucial aspect of keeping your Nextcloud Docker instance running smoothly. You can update the Nextcloud Docker image by running the command `docker-compose pull` followed by `docker-compose up -d` to apply the changes.

To ensure your data remains secure, you should regularly back up your Nextcloud instance. This can be done using the `docker-compose exec` command to run the `occ backup:backup` command, which will create a backup of your data in the /var/www/html/nextcloud/backup directory.

The Nextcloud Docker instance also logs important events, such as errors and warnings, which can be found in the /var/log/nextcloud.log file.

Updating

Credit: youtube.com, Time for a Tune-Up: Cloud SQL Maintenance and Upgrades

Updating Nextcloud is a straightforward process that can be done in two main ways.

You can update to a newer version by pulling the new image, throwing away the old container, and starting the new one.

Only one major version can be upgraded at a time, so you'll need to upgrade in steps, like from version 14 to 15, then to 16.

All your data is safely stored in volumes, so nothing gets lost during the upgrade process.

The startup script will automatically check for version mismatches and start the upgrade process if needed.

Don't forget to add all your volumes to the new container, and use the same command options you used to initially start your Nextcloud.

Updating Nextcloud using Docker Compose is another easy option.

To update, you'll first need to change to the directory where your Compose file is located.

Then, you'll need to pull the latest version of all the containers within the Compose file.

Finally, use the command to move your current running Nextcloud to the new container, and Docker will take care of the rest.

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

A fresh viewpoint: Dropbox One

Remove

Computer server in data center room
Credit: pexels.com, Computer server in data center room

Removing sensitive information from your Nextcloud container is crucial for security.

You can use Docker secrets to load sensitive information from files, rather than passing it via environment variables.

This method is supported for specific environment variables like NEXTCLOUD_ADMIN_PASSWORD, NEXTCLOUD_ADMIN_USER, and MYSQL_PASSWORD.

Any files containing secrets must be readable by the UID the container is running Nextcloud as, such as www-data or 33.

You can ignore the non-_FILE counterparts of these variables, like MYSQL_DATABASE, if you set their _FILE based values, such as MYSQL_DATABASE_FILE.

This approach ensures your sensitive information is secure and not exposed in plain text.

Frequently Asked Questions

What are the minimum requirements for Nextcloud Docker?

To run Nextcloud Docker, you'll need at least 4 GB of RAM and 128 GB of disk space, depending on your data storage needs

Is Nextcloud free self hosted?

Yes, Nextcloud is free and open-source, allowing you to host it yourself on your own domain with minimal costs. You can start using it with even the cheapest web hosting plans.

Margarita Champlin

Writer

Margarita Champlin is a seasoned writer with a passion for crafting informative and engaging content. With a keen eye for detail and a knack for simplifying complex topics, she has established herself as a go-to expert in the field of technology. Her writing has been featured in various publications, covering a range of topics, including Azure Monitoring.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.