How to solve “cannot create directory – Permission denied” error on a docker container?

Maybe you are familiar with the next error mkdir: cannot create directory '/bitnami/mariadb': Permission denied or not.

This is caused by incorrect directory permissions.

Recently, I have tried to start a MariaDB container with a volume mounted to it.

My docker-compose file looked like this:

    version: "3.3"
    services:
      mariadb:
        image: 'bitnami/mariadb:10.3'
        volumes:
          - "./config/mariadb:/bitnami"
        environment:
          - MARIADB_ROOT_USER=example_usr
          - MARIADB_DATABASE=emxample_db
          - MARIADB_ROOT_PASSWORD=example_pwd
As you can see, ./config/mariadb is mounted as a volume, but with root as user.
root@test:../config# ls -l
total 8
drwxr-xr-x 2 root root 4096 Aug 12 11:54 mariadb
drwxr-xr-x 2 root root 4096 Aug 12 11:54 wordpress
The container itself created a new user mysql and have tried to modify a directory of the root user.
    INFO  ==> ** Starting MariaDB setup **
    INFO  ==> Validating settings in MYSQL_*/MARIADB_* env vars..
    INFO  ==> Initializing mariadb database...
    mkdir: cannot create directory '/bitnami/mariadb': Permission denied
    INFO  ==> Stopping mariadb...

How to solve it?

Just change the owner of the folder with the user mysql using the following command.
chown -R 1001:1001 /path/to/your/mariadb_data
After another listing with ls -l:
drwxr-xr-x 2 1001 1001 4096 Aug 12 11:54 mariadb
drwxr-xr-x 2 root root 4096 Aug 12 11:54 wordpress
INFO  ==> ** Starting MariaDB setup **
INFO  ==> Validating settings in MYSQL_*/MARIADB_* env vars..
INFO  ==> Initializing mariadb database...
INFO  ==> Stopping mariadb...
INFO  ==> ** MariaDB setup finished! **
If this doesn’t work, a ticket it’s opened on Github.

no responses for How to solve “cannot create directory – Permission denied” error on a docker container?

    Leave a Reply