Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 511
moviss Avatar asked Nov 09 '17 08:11

moviss


People also ask

How do I fix docker permission is denied?

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.

Can I mount a docker volume?

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.

What does mounting a volume mean in docker?

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.

How do I run a docker container as a root user?

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.

What happens when I run Docker on the same volume?

@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.

Why can’t I mount a volume in a container?

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.

Is it possible to use NFS share with docker volume create?

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.

How do I change the UID and GID in a dockerfile?

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.


2 Answers

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
like image 109
biocyberman Avatar answered Sep 18 '22 23:09

biocyberman


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?

like image 40
Fendi jatmiko Avatar answered Sep 21 '22 23:09

Fendi jatmiko