Solve the problem of failure to mount files or directories using ./ relative path in docker run

Solve the problem of failure to mount files or directories using ./ relative path in docker run

Allow './' relative paths 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 will be mounted to /docker-entrypoint-initdb.d/init-mongo.js in the container and set to read-only mode;

The mongo-volume directory under the current path will be mounted to the container /data/db. If mongo-volume does not exist, the directory will be automatically created

But if it is docker run, you can't use relative path as above

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

You need to use $PWD to replace the dot '.'

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

Supplement: Docker -v has no permission for the mounted directory Permission denied

1. Problem

Today, when I used docker to mount redis, I always got an error.

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 it keeps reporting errors:

Fatal error, can't open config file '/usr/redis/redis.conf'

2. Troubleshooting process

Check the log as well.

Then I removed the place where the configuration file was used

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 into the mounted folder

cd /usr/local/etc/redis

Found an error:

cannot open directory '.': Permission denied

That is, no permission

3. Causes and solutions

3.1 Reasons

The security module selinux in centos7 has disabled permissions

3.2 Solution

There are three ways to solve it:

1. Add --privileged=true at runtime

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 turn off selinux and then turn it on again

[root@localhost tomcat]# setenforce 0
[root@localhost tomcat]# setenforce 1

3. Add linux rules and add the directory to be mounted to the selinux whitelist

The format for changing the security text is as follows

chcon [-R] [-t type] [-u user] [-r role] file or directory

Option without parameters:

-R: All directories under this directory are also modified at the same time;

-t: followed by the type field of the security document, for example httpd_sys_content_t;

-u : followed by identity identification, for example system_u;

-r: The color of the following street, for example system_r

implement:

chcon -Rt svirt_sandbox_file_t /home/redis/redis.conf

4. Some experience about docker mounting

4.1 The 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 host directory is a relative path?

We can get the answer to this question by using the docker inspect command and looking at the "Mounts" section of the container.

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Docker file storage path, modify port mapping operation mode
  • Docker file storage path, get container startup command operation
  • Docker setting windows storage path operation

<<:  Example of how to use CSS3 to layout elements around a center point

>>:  Parsing the commonly used v-instructions in vue.js

Recommend

Tutorial on installing rabbitmq using yum on centos8

Enter the /etc/yum.repos.d/ folder Create rabbitm...

The latest MySQL 5.7.23 installation and configuration graphic tutorial

The detailed installation and configuration of th...

Using CSS3 and JavaScript to develop web color picker example code

The web color picker function in this example use...

Web page production TD can also overflow hidden display

Perhaps when I name this article like this, someon...

JavaScript timer to achieve seamless scrolling of pictures

This article shares the specific code of JavaScri...

Solution to multiple 302 responses in nginx proxy (nginx Follow 302)

Proxying multiple 302s with proxy_intercept_error...

Implementation example of react project from new creation to deployment

Start a new project This article mainly records t...

JavaScript to achieve mouse drag effect

This article shares the specific code of JavaScri...

JavaScript implementation of verification code case

This article shares the specific code for JavaScr...

How to import CSS styles into HTML external style sheets

The link-in style is to put all the styles in one...

JavaScript custom plug-in to implement tab switching function

This article shares the specific code of JavaScri...