When starting my nextcloud container with docker-compose up I encounter the error:

db_1_7bb6e5f5b725 | Initializing database
db_1_7bb6e5f5b725 | mkdir: cannot create directory '/var/lib/mysql//mysql': Permission denied
db_1_7bb6e5f5b725 | Fatal error Can't create database directory '/var/lib/mysql//mysql'
db_1_7bb6e5f5b725 | 
db_1_7bb6e5f5b725 | The latest information about mysql_install_db is available at
db_1_7bb6e5f5b725 | https://mariadb.com/kb/en/installing-system-tables-mysql_install_db
test_db_1_7bb6e5f5b725 exited with code 1

The specified docker-compose looks like:

version: '2'


    image: mariadb
    user: www-data
    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
    restart: always
      - db:/var/lib/mysql
      - MYSQL_PASSWORD=***
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud

    image: nextcloud
      - 8080:80
      - db
      - nextcloud:/var/www/html
    restart: always
  • In your db container, does the www-data user have write access to /var/lib/mysql on the docker volume named db? – GracefulRestart May 9 at 8:36
  • @GracefulRestart Do do I check that in the container? When exiting docker exec -it <container name> /bin/bash I get the response container restarting... – temp May 9 at 8:43

If you are using the official mariadb Docker image, the docker-entrypoint.sh script requires the path /var/lib/mysql to be writable by the user you are running the container as. Otherwise, a container will not start

In your docker-compose.yml, you have defined a named volume db that is to be mounted to the db container. If you are not creating the db named volume before executing docker-compose up, then the /var/lib/mysql directory within the volume likely is being created with incorrect permissions as you are normally a privileged user when running docker-compose.

You should always be able to examine the volume from the host, you may also be able to mount the volume to a new container as read-only to look at it (minimal or blank image works best). From the host, you can run docker volume inspect db to get the file location respective to the host, run docker volume list to see if docker-compose named the volume differently. If you do not have an exotic file backend, you should be able to directly chown the necessary directories to the UID of the user from within the container.

All of this should be done before you attempt to run the docker-compose cluster, by creating the db named volume first and making sure the necessary directories have the proper permissions.

  • Thanks for the good answer. The question is how/where do I create the db volume? – temp May 14 at 13:40
  • docker volume create db. Then docker volume inspect db to find out where it is and chown it to the proper UID. – GracefulRestart May 14 at 17:49
  • The dockerfile isn't stored locally - where do I add these changes? I put it in the initfile before docker-comose up is called. Still got permission denied error. docker volume inspect db shows the volume. – temp May 15 at 6:51
  • if you can see the volume from docker volume inspect db are you then able to change the ownership to the user that will run in the container? – GracefulRestart May 15 at 23:17
  • I solved the problem, by setting the volumes in my docker-compose locally and cleaning the docker volumes with docker volume prune. But thanks fo your help. :) – temp May 16 at 6:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.