5

I successfully installed Nextcloud 13.0 using Docker and Docker Compose.

Here's the GitHub repo of the Docker image of Nextcloud: https://github.com/nextcloud/docker

By default, my Nextcloud installation stores data to the following path:

/var/lib/docker/volumes/nextcloud_nextcloud/_data/data/user/files

My /var folder is located on a small partition while /home folder has about 2Tb of storage space. I'm trying to setup Nextcloud to store uploaded files to /home/iamdocker/nextcloud/data on the host machine but it doesn't work.

The Nextcloud configuration page displays the following message :

Error

Can't create or write into the data directory /home/iamdocker/nextcloud/data/

My Nextcloud installation (running in a Docker container) doesn't have the permission to write.

.env

MYSQL_ROOT_PASSWORD=***********************
MYSQL_DATABASE=nextcloud
MYSQL_USER=nextcloud
MYSQL_PASSWORD=************************************
MYSQL_HOST=db

NEXTCLOUD_VERSION=13.0
NEXTCLOUD_ADMIN_USER=example
NEXTCLOUD_ADMIN_PASSWORD=********************************
NEXTCLOUD_TABLE_PREFIX=nc_
NEXTCLOUD_DATA_DIR=/home/iamdocker/nextcloud/data/

docker-compose.yml

version: '2'

volumes:
  nextcloud:
  db:

services:
  db:
    image: mariadb
    env_file:
      - .env
    restart: always
    volumes:
      - db:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - MYSQL_DATABASE=${MYSQL_DATABASE}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}

  app:
    image: nextcloud:${NEXTCLOUD_VERSION}
    env_file:
      - .env
    ports:
      - 8081:80
    links:
      - db
    volumes:
      - nextcloud:/var/www/html
      - ./data:/var/www/html/data
    environment:
      - NEXTCLOUD_ADMIN_USER=${NEXTCLOUD_ADMIN_USER}
      - NEXTCLOUD_ADMIN_PASSWORD=${NEXTCLOUD_ADMIN_PASSWORD}
      - NEXTCLOUD_TABLE_PREFIX=${NEXTCLOUD_TABLE_PREFIX}
      - NEXTCLOUD_DATA_DIR=${NEXTCLOUD_DATA_DIR}
    restart: always

Thank you.

iamcryptoki
  • 446
  • 2
  • 4
  • 16

2 Answers2

5

I figured out what the issue was: wrong path in NEXTCLOUD_DATA_DIR.

The environment variable NEXTCLOUD_DATA_DIR takes the path of Nextcloud's data directory in the container, not the path of the mounted volume on the host machine.

Solution: I replaced /home/iamdocker/nextcloud/data/ with /var/www/html/data in the .env file.

.env

MYSQL_ROOT_PASSWORD=***********************
MYSQL_DATABASE=nextcloud
MYSQL_USER=nextcloud
MYSQL_PASSWORD=************************************
MYSQL_HOST=db

NEXTCLOUD_VERSION=13.0
NEXTCLOUD_ADMIN_USER=example
NEXTCLOUD_ADMIN_PASSWORD=********************************
NEXTCLOUD_TABLE_PREFIX=nc_
NEXTCLOUD_DATA_DIR=/var/www/html/data
iamcryptoki
  • 446
  • 2
  • 4
  • 16
1

This solution removes the possibility of using a custom data location, this error occurs because the custom data directory does not belong to the user running the web server or PHP, in the case of Apache in Debian environments the owner user must be www-data, and in CentOS environments it should be apache, giving the correct permissions inside the container or on the host mirror directory, customizing the location of the data should work perfectly.

I always recommend removing the data directory from within the web server public directory

chown -R www-data:www-data /dir

OR

chown -R apache:apache /dir

CentOS + SELinux

For SELinux compatibility use :z at the end of the volume

services:
  app:
    volumes:
      - nextcloud:/var/www/html:z
      - ./data:/var/www/html/data:z

My problem and solution

I was having the same permissions problem, however in my case I really needed to change the default file location. But I didn't want to have two files (Dockerfile and docker-compose.yml), I wanted to keep only one for ease of copying.

In my docker-compose.yml I added command with the following statement

services:
  app:
    command: >
      bash -c 'chown www-data:www-data /nextcloud-dados
      && /entrypoint.sh apache2-foreground'

What I did is give permission to the custom file directory on container start.
Explaining:

  • bash -c makes bash run a certain command, I had to use it because I used multiple commands and parameters for them;

  • chown www-data:www-data /nextcloud-dados, this command is what fixes the permissions issue. ("dados" is data in Brazilian Portuguese);

  • /entrypoint.sh apache2-foreground is the command that actually executes and keeps the container running, this information I got from the Docker Hub, where it shows each command executed inside the image. (Lines 44 and 45)

You can find this command (/entrypoint.sh apache2-foreground) also via the docker ps --no-trunc command in the command column