Docker mounting volume. Permission denied
Solution 1
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
Solution 2
I think /srv/redis/redisTest
directory is created by user inside redis
container, so it belong to redis
container user.
Have you already check using ls -l
to see that /srv/redis/redisTest
directory belong to $USER
?
moviss
Updated on July 09, 2022Comments
-
moviss almost 2 years
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.
-
moviss over 6 yearsI want to create directory outside of container :P
-
moviss over 6 yearsSeems like chown works, but is it secure to chown directory which redis container save his files ?
-
biocyberman over 6 years@moviss To answer your question. When you run docker again on the volume, some files may get re-chowned to root again, or the application therein (i.e. redis) may even fail because of wrong ownership. So it is a dilemma that I don't have a perfect answer. But you may want to study this docker setup on github that I contributed to, where you can run docker with none-root user. It may give you some ideas: github.com/broadinstitute/viral-ngs-deploy/blob/master/docker. Take close look at Dockerfile and env_wrapper.sh where I used
gosu
-
Fendi jatmiko over 6 yearsyea .. but when you mount it that way, the directory under /srv/redis/ would be created automatically by the container. . like @biocyberman said. .its very likely that directory belong to root inside redis container.. even if you created it manually