Nextcloud is an open-source software suite for storing and synchronizing data, sort of like a free alternative to Dropbox or Google Drive.

 

Plus, with Nextcloud, you get an open system architecture that gives you additional functionality and full control of your data.

 

With Nextcloud, you can:

  • Store files, contacts, calendars and more on your server, and synchronize them across various devices
  • Share your data with others to view and collaborate on
  • Expand your Nextcloud installation with apps from the Nextcloud App Store,
  • Or build your own apps and integrate them with Nextcloud.

 

Nextcloud + Docker: Prerequisites

  • A VPS running CentOS 7.

 

Step 1. Install Docker and Docker Compose

The latest version of the Docker CE is not available in CentOS 7 repository. Therefore, we need to add the Docker CE repository with the following command:

$ sudo yum update  
$ wget https://download.docker.com/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker.repo

 

Now execute the following command to install Docker CE:

$ sudo yum install docker-ce –y

 

Start the Docker service and enable it to start during system startup.

$ sudo systemctl start docker
$ sudo systemctl enable docker

 

To install the latest docker-compose using Python’s pip command, execute the following commands in the terminal:

$ sudo yum install epel-release
$ sudo yum install python-pip
$ sudo pip install docker-compose
$ docker-compose --version
docker-compose version 1.21.2, build a133471

 

Step 2. Install Nextcloud

 

Before we start defining services in the docker-compose.yml file, we create a network so that containers can communicate. Run the following command in the terminal:

$ docker network create nextcloud_network

 

Since we want to containerize Nextcloud along with other containers associated with it, we will define and knit all the services together in the docker-compose.yml file incrementally.

 

For this tutorial, we’ll define the services one by one, starting with the Nginx reverse proxy:

  • Nginx reverse proxy
  • Let’s Encrypt
  • MariaDB
  • Nextcloud

 

Create the docker-compose file where we will define all the services.

$ vi docker-compose.yml

 

Step 3. Configure the Nginx reverse proxy container

 

In the file you just created, past the following:

version: '3'  

services:

  proxy:
    image: jwilder/nginx-proxy:alpine
    labels:
      - "com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy=true"
    container_name: nextcloud-proxy
    networks:
      - nextcloud_network
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./proxy/conf.d:/etc/nginx/conf.d:rw
      - ./proxy/vhost.d:/etc/nginx/vhost.d:rw
      - ./proxy/html:/usr/share/nginx/html:rw
      - ./proxy/certs:/etc/nginx/certs:ro
      - /etc/localtime:/etc/localtime:ro
      - /var/run/docker.sock:/tmp/docker.sock:ro
    restart: unless-stopped

 

Let’s look at the configuration created in the above docker-compose.yml file in detail. The service for proxy uses the image from jwilder/nginx-proxy. The label "com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy" is used so that the Let’s Encrypt container knows which nginx proxy container to use for certificate generation.

 

Then, there is network by the name nextcloud_network, which is used by the containers to communicate among themselves. The Volumes section is used by the container to configure the Nginx virtual host and to access certificates generated by Let’s Encrypt companion container. The /etc/localtime:/etc/localtime:ro is used to duplicate the host timezone inside the container.

 

Step 4. Configure the Let’s Encrypt container

 

Now that you have nginx-proxy container set up, you can add the following to your docker-compose.yml file.

letsencrypt:
    image: jrcs/letsencrypt-nginx-proxy-companion
    container_name: nextcloud-letsencrypt
    depends_on:
      - proxy
    networks:
      - nextcloud_network
    volumes:
      - ./proxy/certs:/etc/nginx/certs:rw
      - ./proxy/vhost.d:/etc/nginx/vhost.d:rw
      - ./proxy/html:/usr/share/nginx/html:rw
      - /etc/localtime:/etc/localtime:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro
    restart: unless-stopped

 

The Lets’ Encrypt container depends on our first service (proxy) and is a part of the network nextcloud_network. The restart: unless-stopped allows the containers to be stopped gracefully unless you manually run docker stop letsencrypt or docker-compose down letsencrypt.

 

Step 5. Configure the MariaDB container

 

For Nextcloud to work correctly, we need to connect it to a MariaDB database. Fortunately, we can add that to our docker-compose.yml file as well:

  db:
    image: mariadb
    container_name: nextcloud-mariadb
    networks:
      - nextcloud_network
    volumes:
      - db:/var/lib/mysql
      - /etc/localtime:/etc/localtime:ro
    environment:
      - MYSQL_ROOT_PASSWORD=secret
      - MYSQL_PASSWORD=mysql
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
    restart: unless-stopped

 

The service section for MariaDB is pretty self-explanatory. This container is also part of the network nextcloud_network. We have also defined the environment variable for the database name, username, and password that Nextcloud uses to connect to the database.

 

Step 6. Configure the Nextcloud Docker container

 

We’re finally ready to create the Nextcloud Docker container in our docker-compose.yml file. Add the following to the bottom.

 app:
    image: nextcloud:latest
    container_name: nextcloud-app
    networks:
      - nextcloud_network
    depends_on:
      - letsencrypt
      - proxy
      - db
    volumes:
      - nextcloud:/var/www/html
      - ./app/config:/var/www/html/config
      - ./app/custom_apps:/var/www/html/custom_apps
      - ./app/data:/var/www/html/data
      - ./app/themes:/var/www/html/themes
      - /etc/localtime:/etc/localtime:ro
    environment:
      - VIRTUAL_HOST=nextcloud.YOUR-DOMAIN
      - LETSENCRYPT_HOST=nextcloud.YOUR-DOMAIN
      - LETSENCRYPT_EMAIL=YOUR-EMAIL
    restart: unless-stopped

 

The nextcloud service depends on the other three containers. To make Nextcloud’s data persistent while upgrading, and get access to backups, we use a named Docker volume nextcloud, similar to the way we used a Docker volume named db for the MariaDB data.

 

Here, we have defined the virtual host, Let’s Encrypt host, and email in the environment variables VIRTUAL_HOST, LETSENCRYPT_HOST, and LETSENCRYPT_EMAIL, respectively. The proxy service creates the subdomain and encrypts it with Let’s Encrypt certificates for the container, given you supply valid domains and emails for those three environment variables.

 

At last, we need defined volumes for both Nextcloud and MariaDB for data persistence followed by networks.

volumes:
  nextcloud:
  db:

networks:
  nextcloud_network:

 

After combining all the service definitions, your final docker-compose.yml should look like following:

 version: '3' 

services:

  proxy:
    image: jwilder/nginx-proxy:alpine
    labels:
      - "com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy=true"
    container_name: nextcloud-proxy
    networks:
      - nextcloud_network
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./proxy/conf.d:/etc/nginx/conf.d:rw
      - ./proxy/vhost.d:/etc/nginx/vhost.d:rw
      - ./proxy/html:/usr/share/nginx/html:rw
      - ./proxy/certs:/etc/nginx/certs:ro
      - /etc/localtime:/etc/localtime:ro
      - /var/run/docker.sock:/tmp/docker.sock:ro
    restart: unless-stopped
  
  letsencrypt:
    image: jrcs/letsencrypt-nginx-proxy-companion
    container_name: nextcloud-letsencrypt
    depends_on:
      - proxy
    networks:
      - nextcloud_network
    volumes:
      - ./proxy/certs:/etc/nginx/certs:rw
      - ./proxy/vhost.d:/etc/nginx/vhost.d:rw
      - ./proxy/html:/usr/share/nginx/html:rw
      - /etc/localtime:/etc/localtime:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro
    restart: unless-stopped

  db:
    image: mariadb
    container_name: nextcloud-mariadb
    networks:
      - nextcloud_network
    volumes:
      - db:/var/lib/mysql
      - /etc/localtime:/etc/localtime:ro
    environment:
      - MYSQL_ROOT_PASSWORD=toor
      - MYSQL_PASSWORD=mysql
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
    restart: unless-stopped
  
  app:
    image: nextcloud:latest
    container_name: nextcloud-app
    networks:
      - nextcloud_network
    depends_on:
      - letsencrypt
      - proxy
      - db
    volumes:
      - nextcloud:/var/www/html
      - ./app/config:/var/www/html/config
      - ./app/custom_apps:/var/www/html/custom_apps
      - ./app/data:/var/www/html/data
      - ./app/themes:/var/www/html/themes
      - /etc/localtime:/etc/localtime:ro
    environment:
      - VIRTUAL_HOST=nextcloud.YOUR-DOMAIN
      - LETSENCRYPT_HOST=nextcloud.YOUR-DOMAIN
      - LETSENCRYPT_EMAIL=YOUR-EMAIL
    restart: unless-stopped

volumes:
  nextcloud:
  db:

networks:
  nextcloud_network:

 

Step 7. Get everything running!

 

Now run the docker-compose from the terminal to create the containers:

$ docker-compose up -d
Creating nextcloud-mariadb ... done
Creating nextcloud-proxy   ... done
Creating nextcloud-letsencrypt ... done
Creating nextcloud-app         ... done

 

To confirm all the containers are running, issue the following command:

$ docker ps -a
CONTAINER ID        IMAGE                                    COMMAND                  CREATED             STATUS              PORTS                                      NAMES
92222232c4be        nextcloud:latest                         "/entrypoint.sh apac…"   9 minutes ago       Up 9 minutes        80/tcp                                     nextcloud-app
89e96fe10ee6        jrcs/letsencrypt-nginx-proxy-companion   "/bin/bash /app/entr…"   9 minutes ago       Up 9 minutes                                                   nextcloud-letsencrypt
d059517f519c        jwilder/nginx-proxy:alpine               "/app/docker-entrypo…"   9 minutes ago       Up 9 minutes        0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   nextcloud-proxy
7e0945eb6608        mariadb                                  "docker-entrypoint.s…"   9 minutes ago       Up 9 minutes        3306/tcp                                   nextcloud-mariadb

 

Wait a minute for the SSL certificate generation process to finish, and then load up the domain name you chose in your browser. Enter your chosen admin username and password. Choose MySQL as the database in the configure database section. Type in the username, password, and database name you configured via the MYSQL_USER, MYSQL_PASSWORD, and MYSQL_DATABASE environment variable from earlier. Change the hostname value from localhost to db and click Finish Setup. The system then redirects you to the Nextcloud dashboard.

 

 

The containerization of Nextcloud with Docker is complete!

 

Now you can upload files and photos to your drive hosted on your VPS and share them with others.

 

To extend the functionality of your nextcloud server, you can now consider installing any number of the available apps, such as Bookmarks, Calendar, Contacts, Tasks, Notes, and more available on the Nextcloud App Store.

 

Was this answer helpful? 2 Users Found This Useful (73 Votes)