Troubleshooting Failure to Mount File or Directory Using. and Relative Path in docker run
- 2021-10-25 00:10:55
- OfStack
The './' relative path is allowed in docker-compose. yml files
version: '3'
...
volumes:
- ./init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
- ./mongo-volume:/data/db
...
The init-mongo. js file in the current path is mounted to/docker-entrypoint-initdb. d/init-mongo. js in the container and set to read-only;
The mongo-volume directory under the current path is mounted into the container/data/db, which is automatically created if mongo-volume does not exist
However, if it is docker, run, it cannot use relative path as above 1
>>> docker run -d --restart always -p 27017-27019:27017-27019 -e MONGO_INITDB_DATABASE=job -e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=root -v $PWD/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro -v ./mongo-volume:/data/db --name my-mongo-container mongo
docker: Error response from daemon: create ./init-mongo.js: "./init-mongo.js" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path.
See 'docker run --help'.
Need to use $PWD instead of the dot number '.'
>>> docker run -d --restart always -p 27017-27019:27017-27019 -e MONGO_INITDB_DATABASE=job -e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=root -v $PWD/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro -v $PWD/mongo-volume:/data/db --name my-mongo-container mongo
3081e25a20fa8b2e95850897b3b32b08da298f73d7e458119fa3f2c85b45f020
Added: Docker-v No permissions on mounted directory Permission denied
1. Problems
Today, when using docker to mount redis, you always report errors
docker run -v /home/redis/redis.conf:/usr/local/etc/redis/redis.conf --name redis2 -p 6378:6379 redis redis-server /usr/local/etc/redis/redis.conf
Then 1 directly reports an error:
Fatal error, can't open config file '/usr/redis/redis.conf'
2. Screening process
The same is true of viewing logs
Then I removed the place where I used the configuration file
docker run -v /home/redis/redis.conf:/usr/local/etc/redis/redis.conf --name redis2 -p 6378:6379 redis
Then enter the container
docker exec -it redis2 /bin/bash
Then go to the mounted folder
cd /usr/local/etc/redis
Found error:
cannot open directory '.': Permission denied
That is, there is no permission
3. Causes and solutions
3.1 Reasons
The security module selinux in centos7 disables the authority
3.2 Solution
There are three ways to solve this problem:
1. Add-privileged=true at run time
docker run -v /home/redis/redis.conf:/usr/local/etc/redis/redis.conf --name redis2 --privileged=true redis redis-server /usr/local/etc/redis/redis.conf
2. Temporarily close selinux and then open it again
[root@localhost tomcat]# setenforce 0
[root@localhost tomcat]# setenforce 1
3. Add the linux rule to add the directory to be mounted to the selinux white list
Change the format of the security text as follows
chcon [-R] [-t type] [-u user] [-r E113EN] file or directory
Select parameters:
-R: All directories under this directory are also modified at the same time;
-t: followed by the type field of the security article, for example httpd_sys_content_t;
-u: followed by identification, e.g. system_u;
-r: Back street color, for example system_r
Execution:
chcon -Rt svirt_sandbox_file_t /home/redis/redis.conf
4. Lessons from docker mounting
4.1 Container directory cannot be a relative path
4.2 If the host directory does not exist, it will be automatically generated
4.3 What if the directory of the host is a relative path
We can get the answer to this question by looking at the 1 part of the container "Mounts" through the docker inspect command.