Search code examples
linuxubuntudockerpermissionsdocker-volume

Docker mounting volume. Permission denied


I have a problem with creating new files in mounted docker volume.

Firstly after installation docker i added my user to docker group.

sudo usermod -aG docker $USER

Created as my $USER folder:

mkdir -p /srv/redis

And starting container:

docker run -d -v /srv/redis:/data --name myredis redis

when i want to create file in /srv/redis as a user which created container I have a problem with access.

mkdir /srv/redis/redisTest
mkdir: cannot create directory ‘/srv/redis/redisTest’: Permission denied

I tried to search in other threads but i didn't find appropriate solution.


Solution

  • The question title does not reflect the real problem in my opinion.

    mkdir /srv/redis/redisTest
    mkdir: cannot create directory ‘/srv/redis/redisTest’: Permission denied
    

    This problem occurs very likely because when you run:

    docker run -d -v /srv/redis:/data --name myredis redis
    

    the directory /srv/redis ownership changes to root. You can check that by

    ls -lah /srv/redis
    

    This is normal consequence of mounting external directory to docker. To regain access you have to run

    sudo chown -R $USER /srv/redis