简体   繁体   中英

How to mount the root directory of docker container as a NFS mount point

I'm new to docker, and I'm trying mount the root directory of docker container as a NFS mount point. for example, I had a NFS mount point test:/home/user/3243 , and I'm trying:

docker run -it -v "test:/home/user/3243":/ centos7 /bin/bash

absolutely, it's failed. So I tried this:

mount -t nfs test:/home/user/3243 /mnt/nfs/3243
docker run -it -v /mnt/nfs/3243:/ centos7 /bin/bash

but failed again, so how to do this? Could it be worked out?

A couple of issues here:

  1. You cannot mount to the root directory of a container. So docker run -v /foo:/ will never work.

  2. With the syntax of your first attempt, -v test:/foo:bar , Docker would see this as wanting to create a "named" volume called "test".

You should be able to first do the NFS mount, then do docker run -v /mnt/nfs/3243:/foo to have the nfs path mounted to /foo . But again, you can't mount to / .

That is currently discussed (since mid 2014) in issue 4213 .

One recent workaround by Jeroen van Bemmel (jbemmel) was:

It appears that NFS functionality depends on the underlying storage driver ( aufs, devicemapper, etc. ), as well as the sharing of file handles between processes ( see blog post " docker: devicemapper fix for “device or resource busy” ( EBUSY ) ") ie 'unshare' may have an impact on NFS mounts.

I've moved away from using the ' MOUNTPOINT=/vm/nfs ' as I am not sure if that event is even emitted.
Instead I created an upstart file like this:

cat > /etc/init/ecdn.conf << EOF
description "eCDN container"
author "Jeroen van Bemmel"
# mounted MOUNTPOINT=/vm/nfs doesn't seem to work, at least not the first time
start on started docker and virtual-filesystems
stop on starting rc RUNLEVEL=[016]
respawn
script
exec /usr/bin/docker start -a ecdn
end script

pre-stop script
/usr/bin/docker stop ecdn
# dont /usr/bin/docker rm ecdn
end script

EOF

and then create the container like this:

script -c "docker create -it --name='ecdn' --volume /vm:/usr/share/nginx/html/vm:ro image/name"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM