Solve the problem that the directory mounted by docker cannot be read and written

  • 2021-10-25 00:10:00
  • OfStack

Use the following command to create a container and mount the local/home/dock/Downloads directory into the/usr/Downloads in the container


[root@docker ~]# docker run -it -v /home/dock/Downloads:/usr/Downloads ubuntu /bin/bash

After creating on Centos, the following problems will occur


root@637fe9ea94f0:/usr/Downloads# ls
ls: cannot open directory '.': Permission denied

Solution:

The reason is that the security module selinux in CentOS7 has banned the permissions.

There are several ways to solve the problem that the mounted directory has no permissions:

1. When running the container, give the container privileges and add the-privileged=true parameter:


docker run -i -t -v /soft:/soft --privileged=true 637fe9ea94f0 /bin/bash

2. Temporary closure of selinux:


setenforce 0

3. Add an selinux rule to change the security text of the directory to be mounted

Supplement: docker can't access the folder after mounting, and has no permission

The folder mounted after entering docker run cannot be accessed and has no permission

For example:


sudo nvidia-docker run -p 8090:8888 -p 8091:80 -p 8092:6666 --name=pytorch0.4 -v ~/workspace:/root/workspace -i --shm-size 31G -t dsksd/pytorch:0.4.1 /bin/bash

Mount the host's workspace folder with the container's the/root/workspace folder, but data cannot be written to the workspace folder on the host. Because the owner of the folder is root.

You can execute commands:


sudo chown -R $USER  Folder location (/workspace)

The folder can be read and written. However, the above command needs to be re-executed after restarting.


Related articles: