Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I mount --bind inside a Docker container?

Tags:

docker

mount

bind

I have this container based on debian:jessie (but this is not very relevant as I had the same issue with alpine:3.3). I get to the point where I need to

mount --bind /htdocs/www /home/user/example.com/www

and I get

mount: permission denied

I can't find anything in any kernel log, and -vvv yields nothing interesting. I obviously can do this on the host (with any other pair of subtree/node). In my example above /htdocs/www is the mountpoint of a Docker volume, but it doesn't appear like it's of any importance, as I can't mount --bind any pair of subtree/node inside the container.

like image 621
Morpheu5 Avatar asked Apr 11 '16 15:04

Morpheu5


People also ask

What is a bind mount in Docker?

A Bind Mount is a storage area (file/directory) on your local machine available inside your container. So any changes you make to this storage space (file/directory) from the outside container will be reflected inside the docker container and vice-versa.

How do I mount a local directory into a container?

How to Mount Local Directories using docker run -v. Using the parameter -v allows you to bind a local directory. -v or --volume allows you to mount local directories and files to your container. For example, you can start a MySQL database and mount the data directory to store the actual data in your mounted directory.


2 Answers

For using the mount system call, you need the CAP_SYS_ADMIN capability. By default, Docker drops all capabilities when spawning a container (meaning that even as root, you're not allowed to do everything). See the mount(2) man page for more information.

You can start your container with the --cap-add=SYS_ADMIN flag to add this capability to your container:

root@host > docker run --rm -it --cap-add=SYS_ADMIN debian:jessie
root@ee0b1d5fe546:/# mkdir /mnt/test
root@ee0b1d5fe546:/# mount --bind /home /mnt/test/
root@ee0b1d5fe546:/# 

Use this with caution. Do not run untrusted software in a privileged container.

like image 137
helmbert Avatar answered Oct 24 '22 07:10

helmbert


Try with --privileged flag:

docker run --rm -it --privileged=true debian
mkdir /mnt/test
mount --bind /home /mnt/test/
like image 21
Javier Avatar answered Oct 24 '22 05:10

Javier