As part of my little series of self-hosted web services using Docker, in this article I describe how I installed Nextcloud running behind Traefik as a reverse proxy which is providing an encrypted HTTPS connection. As almost usual, Nextcloud is also providing an official Docker image. This image by itself is not sufficient to have a running Nextcloud instance. What is missing is a web server and a database, similar to the dockerized WordPress.

As a database, I’m using MariaDB and nginx as a web server. The database files are stored “outside” the Docker container to have them persistent when the container stops, crashes or gets deleted. Nextcloud’s files are also stored outside and additionally mounted into the web server’s container.

Setup of Docker containers to run Nextcloud

Setup of Docker containers to run Nextcloud

1. Database

Setting up a MariaDB instance used by Nextcloud does not require special configuration magic. Just specifying the image, storing its data outside the container, setting some credentials and disabling the connection to Traefik.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
services:
  db:
    image: mariadb:10.2.13
    restart: always
    volumes:
      - /path/to/nextcloud/db:/var/lib/mysql
    secrets:
      - nextcloud_db
    networks:
      - internal
    labels:
      - traefik.enable=false

secrets:
  nextcloud_db:
    file: nextcloud_db.txt

nextcloud_db.txt

1
2
3
4
MYSQL_ROOT_PASSWORD=...
MYSQL_PASSWORD=...
MYSQL_DATABASE=db
MYSQL_USER=db_user

2. Nextcloud

My Nextcloud configuration is pretty concise. I just needed to connect the database container, specify a volume for Nextcloud files, put the container into an internal Docker network and disable a connection to Traefik.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
volumes:
  nextcloud_files:
    driver: local-persist
  driver_opts:
    mountpoint: /path/to/nextcloud/files

app:
  image: nextcloud:fpm
  hostname: 'nextcloud.my-domain.com'
  links:
    - db
  depends_on:
    - db
  volumes:
    - nextcloud_files:/var/www/html
  restart: always
  networks:
    - internal
  labels:
    - traefik.enable=false

3. Web server

Since both existing containers are not reachable from outside Docker, I need to configure a web server that is accepting incoming connections forwarded by Traefik. Again, there is not much configuration necessary, just the usual suspects. Injecting an nginx configuration file and the Nextcloud files, defining networks and a connection to Traefik.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
web:
  image: nginx
  restart: always
  volumes:
    - ./nginx.conf:/etc/nginx/nginx.conf:ro
    - nextcloud_files:/var/www/html
  links:
    - app
  networks:
    - internal
    - proxy
  labels:
    - traefik.enable=true
    - traefik.backend=nextcloud
    - traefik.frontend.rule=Host:nextcloud.my-domain.com
    - traefik.docker.network=proxy

Summary

Configuring Nextcloud running in Docker is pretty straightforward. In addition to Nextclouds Docker image, a database to store various metadata and a web server to communicate with Traefik are needed, which are both running in dedicated Docker containers. As mentioned in other posts, the HTTPS encryption is handled by Traefik.