In this post I will go through how to set up NextCloud with Docker on Raspberry Pi 4.
Table of Contents
Prerequisite for NextCloud with docker
Prepare environment for Docker and docker-compose
For the setup, I am going to use docker-compose and a nginx-proxy. Nginx-proxy is not necessary if you are only going to host NextCloud and not other services on your Raspberry Pi 4. I found it is more convenient this way, as I always end up hosting a few different web services at the end.
Following the Prerequisite: install docker and docker-compose on raspberry pi step in this post, we will get the Docker environment ready.
Set up nginx-proxy, and LetsEncrypt
Next we need to get nginx-proxy and LetsEncrypt up and running. Once we have docker and docker-compose, it is a simple step. First we create a nginx-proxy directory.
mkdir nginx-proxy
cd nginx-proxy
Then, in the nginx-proxy directory, create a docker-compose.yml with the following contents:
version: "3"
services:
nginx-proxy:
image: alexanderkrause/rpi-nginx-proxy
container_name: nginx-proxy
restart: always
ports:
- "80:80"
- "443:443"
volumes:
- ./conf:/etc/nginx/conf.d
- ./vhost:/etc/nginx/vhost.d
- ./html:/usr/share/nginx/html
- ./certs:/etc/nginx/certs:ro
- /var/run/docker.sock:/tmp/docker.sock:ro
- ./nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl:ro
labels:
- "com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy"
letsencrypt:
image: alexanderkrause/rpi-letsencrypt-nginx-proxy-companion
container_name: nginx-proxy-le
restart: always
depends_on:
- nginx-proxy
volumes:
- ./conf:/etc/nginx/conf.d
- ./vhost:/etc/nginx/vhost.d
- ./html:/usr/share/nginx/html
- ./certs:/etc/nginx/certs:rw
- /var/run/docker.sock:/var/run/docker.sock:ro
networks:
default:
external:
name: nginx-proxy
After saving the file, before bringing up the instance, we should first create the nginx-proxy network.
docker network create --driver bridge nginx-proxy
In the nginx-proxy directory, we execute:
docker-compose up -d
The nginx-proxy is now up and running.
If you want to learn more about the images and configuration, in the set up nginx-proxy and LetsEncrypt section of this post you can find more details.
Setting up NextCloud with docker-compose
My next step is to compose the NextCloud with Docker. Actually, we need NextCloud and MySQL settings. Now, go back to our home directory. We will create a directory for NextCloud.
mkdir nextcloud
cd nextcloud
Then, in the nextcloud directory, create a docker-compose.yml with the following contents:
version: '3'
volumes:
files-nextcloud:
driver: local
mysql-nextcloud:
driver: local
services:
nextcloud:
image: nextcloud:latest
restart: always
expose:
- 8080
depends_on:
- db
environment:
- MYSQL_PASSWORD=<set a password A>
- MYSQL_DATABASE=nextcloud
- MYSQL_USER=nextcloud
- MYSQL_HOST=db_nextcloud:3306
- VIRTUAL_HOST=<your host address, e.g. cloud.mysite.com>
- LETSENCRYPT_HOST=<your host address, e.g. cloud.mysite.com>
- LETSENCRYPT_EMAIL=<your email>
volumes:
- files-nextcloud:/var/www/html
links:
- db:db_nextcloud
db:
image: hypriot/rpi-mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=<set a password B>
- MYSQL_USERNAME=nextcloud
- MYSQL_PASSWORD=<the password A you set previously>
- MYSQL_DATABASE=nextcloud
volumes:
- mysql-nextcloud:/var/lib/mysql
networks:
default:
external:
name: nginx-proxy
Many tutorials you can find, including the official tutorial, suggests to use MariaDB. However, there are a few issues I ran into with MariaDB, so instead I used the old MySQL image for Raspberry Pi.
That was nice and easy. Let’s start this thing:
docker-compose up -d
We may have to wait for a minute or two for nginx-proxy and LetsEncrypt to finish their work. But as long as there is no error, we should eventually be able to access our web interface at the URL you set. For example, https://cloud.mysite.com.
Resolving the database privilege issue
Once you see the web interface, the first time we will have to set up the admin user. It looks like the NextCloud instance cannot create the admin user in the MySQL database by itself. So you may see an error message when you click on the button to create admin account. The error message complains about not able to create an account – depending on your set up the account name may look different but in the error message it will says something like ‘admin’@’localhost’. Write this down, we are going to need this information to fix the issue.
To resolve this, the idea is we need to give this MySQL user (‘admin’@’localhost’ or so) the privilege to modify the database. So let’s get into the MySQL docker instance and fix it.
To find the MySQL docker container, we can run:
docker ps -aqf "name=nextcloud_db"
The output is our container ID. We can then access the container by:
docker exec -it <container_ID> bash
This will enter the container and you should see the shell. We need to get into MySQL here:
mysql -u root -p <the password B you set in docker-compose>
We now need to create the MySQL user which NextCloud will use to deal with the database operations and grant all privileges to the nextcloud database to this user.
CREATE USER ‘admin’@‘localhost’ IDENTIFIED BY ‘your_password_A’;
GRANT ALL PRIVILEGES on nextcloud.* to ‘admin’@‘localhost’ IDENTIFIED BY ‘your_password_A’;
You should replace the account name by the one you just wrote down, and here the password A should match the one we set up in docker-compose.yml.
Now if we restart NextCloud container, things should be good to go. Log out from the MySQL container, and in the nextcloud directory:
docker-compose down
docker-compose up -d
References
- https://hub.docker.com/_/nextcloud
- https://github.com/nextcloud/docker
- https://help.nextcloud.com/t/error-creating-admin-user/23943/8
nice article, thanks!
How do you access the nginx web interface?
The docker image I used here does not use nginx. The base version is apache, as explained here: https://github.com/nextcloud/docker