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.
If running elevated Docker commands does not fix the permission denied error, verify that your Docker Engine is running. Similar to running a docker command without the sudo command, a stopped Docker Engine triggers the permission denied error. How do you fix the error? By restarting your Docker engine.
A Docker volume is a directory somewhere in your Docker storage directory and can be mounted to one or many containers. They are fully managed and do not depend on certain operating system specifics. Before removing the Docker volume, you can open your Docker GUI and inspect the volume by clicking on the data tab.
The VOLUME command will mount a directory inside your container and store any files created or edited inside that directory on your hosts disk outside the container file structure, bypassing the union file system.
Docker containers are designed to be accessed as root users to execute commands that non-root users can't execute. We can run a command in a running container using the docker exec. We'll use the -i and -t option of the docker exec command to get the interactive shell with TTY terminal access.
@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.
The problems are significant for bind mounts when the host environment file and directory structure affect container’s environment. For example, if we create a volume and mount into /tmp in a container, Docker software manages this volume and it’s run as a root in both host and container sides.
Publishing your nfs port to the host would allow to use docker volume create. I assume your motivation is just a proof of concept, just to verify weather the approach you came up with is feasable.In theory publishing your port should allow docke volume create to leverage the nfs share, BUT: this is messy and i am afraid not realy reliable.
Assume you have a new user is set in Dockerfile then just call these commands in either Dockerfile or entrypoint.sh. If your user name and groupname were “test”, then you can use usermod and groupmod commands to modify UID and GID in the container.
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
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
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With