Techwetrust
Techwetrust is a programming blog maintained by Alin Alexandru, a young Python developer. He addresses topics like Python tutorials, Docker tutorials and other tech-related articles.

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.

Alin Alexandru

@skykery

Hello! My name is Banuta Alexandru Alin and I am working as a full-time programmer. I have experience in data mining, data science, and data parsing/crawling. I like to travel when I have some days off. May my knowledge help you and protect your way!

Leave a Reply