Fixing Nextcloud Permissions in a Docker Container

Nextcloud is a powerful open-source platform for file synchronization and sharing, but it can run into issues if the file permissions are not set correctly. One common issue is the “Can’t write into config directory!” error, which typically indicates a permissions problem. This blog post will guide you through the steps to fix Nextcloud permissions within a Docker container.

Steps to Fix Nextcloud Permissions

Step 1: Identify the Running Container

First, you need to identify the container running Nextcloud. You can do this by listing all running containers and finding the one for Nextcloud.

docker ps

Look for the container ID or name of the container running Nextcloud.

Step 2: Adjust Permissions

You need to adjust the permissions of the config directory within the container. The following commands will set the correct permissions and ownership for the directory.

  1. Change Directory PermissionsRun the following command to set the permissions for the config directory to 755, which allows the owner to read, write, and execute, and others to read and execute.
docker exec -u root <container_id_or_name> sh -c 'chmod 755 /var/www/html/config'
  1. Change OwnershipSet the ownership of the config directory to www-data, which is the user under which Nextcloud typically runs.
docker exec -u root <container_id_or_name> sh -c 'chown -R www-data:www-data /var/www/html/config'

Replace <container_id_or_name> with the actual container ID or name.

Step 3: Verify Permissions

After changing the permissions and ownership, verify that the changes have been applied correctly. You can do this by listing the directory contents with detailed permissions.

docker exec -u root <container_id_or_name> sh -c 'ls -lsa /var/www/html'

This command will display the permissions and ownership of the files and directories in the Nextcloud root directory. You should see the config directory with 755 permissions and www-data as the owner.

Example Commands

Here are the example commands with placeholder values replaced:

docker exec -u root nextcloud_container sh -c 'chmod 755 /var/www/html/config'
docker exec -u root nextcloud_container sh -c 'chown -R www-data:www-data /var/www/html/config'
docker exec -u root nextcloud_container sh -c 'ls -lsa /var/www/html'

Output Example

The output of the ls -lsa /var/www/html command should look something like this:

4 drwxrwxrwx 14 www-data www-data    4096 Jun 14 15:47 .
4 drwxrwxr-x  1 www-data root        4096 Jun 13 08:47 ..
8 -rw-r--r--  1 www-data www-data    5120 Jun 14 15:47 .htaccess
4 -rw-r--r--  1 www-data www-data     101 Jun 14 15:47 .user.ini
4 drwxr-xr-x 44 www-data www-data    4096 Jun 14 15:47 3rdparty
24 -rw-r--r--  1 www-data www-data   23796 Jun 14 15:47 AUTHORS
36 -rw-r--r--  1 www-data www-data   34520 Jun 14 15:47 COPYING
4 drwxr-xr-x 51 www-data www-data    4096 Jun 14 15:47 apps
4 -rw-r--r--  1 www-data www-data    2079 Jun 14 15:47 composer.json
4 -rw-r--r--  1 www-data www-data    3140 Jun 14 15:47 composer.lock
4 drwxr-xr-x  2 www-data www-data    4096 Jun 14 15:47 config
8 -rw-r--r--  1 www-data www-data    4145 Jun 14 15:47 console.php
4 drwxr-xr-x 24 www-data www-data    4096 Jun 14 15:47 core
8 -rw-r--r--  1 www-data www-data    7837 Jun 14 15:47 cron.php
4 drwxr-xr-x  2 www-data www-data    4096 Jun 14 15:47 custom_apps
4 drwxr-xr-x  3 www-data www-data    4096 Jun 14 15:47 data
24 drwxr-xr-x  2 www-data www-data   20480 Jun 14 15:47 dist
4 -rw-r--r--  1 www-data www-data     156 Jun 14 15:47 index.html
8 -rw-r--r--  1 www-data www-data    4564 Jun 14 15:47 index.php
4 drwxr-xr-x  6 www-data www-data    4096 Jun 14 15:47 lib
0 -rw-r--r--  1 root     root           0 Jun 14 15:47 nextcloud-init-sync.lock
4 -rwxr-xr-x  1 www-data www-data     283 Jun 14 15:47 occ
4 drwxr-xr-x  2 www-data www-data    4096 Jun 14 15:47 ocs
4 drwxr-xr-x  2 www-data www-data    4096 Jun 14 15:47 ocs-provider
1044 -rw-r--r--  1 www-data www-data 1065184 Jun 14 15:47 package-lock.json
8 -rw-r--r--  1 www-data www-data    7032 Jun 14 15:47 package.json
4 -rw-r--r--  1 www-data www-data    3759 Jun 14 15:47 public.php
8 -rw-r--r--  1 www-data www-data    5597 Jun 14 15:47 remote.php
4 drwxr-xr-x  4 www-data www-data    4096 Jun 14 15:47 resources
4 -rw-r--r--  1 www-data www-data      26 Jun 14 15:47 robots.txt
4 -rw-r--r--  1 www-data www-data    2523 Jun 14 15:47 status.php
4 drwxr-xr-x  3 www-data www-data    4096 Jun 14 15:47 themes
4 -rw-r--r--  1 www-data www-data     383 Jun 14 15:47 version.php

Conclusion

By following these steps, you should be able to fix the “Can’t write into config directory!” error in your Nextcloud Docker container. Ensuring the correct permissions and ownership is crucial for the smooth operation of Nextcloud. If you encounter any issues, double-check the paths and commands used, and ensure that the container is running properly.