I’m certain I’m going to sound like a giant idiot but I am struggle to understand how Docker Volumes work.
I have the following Dockerfile
FROM centos:7
MAINTAINER Giant Idiot
RUN yum -y update &&
yum -y install epel-release vim &&
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
VOLUME /opt/my_vol
RUN echo "root:root" | chpasswd &&
yum -y install openssh-server &&
mkdir -p /var/run/sshd &&
sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config &&
ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
RUN yum clean all
EXPOSE 22
COPY supervisord.conf /tmp/
RUN yum -y install supervisor &&
mkdir -p /etc/supervisor/conf.d &&
mv /tmp/supervisord.conf /etc/supervisor/conf.d
CMD /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
Which includes the VOLUME command to create /opt/my_vol, when I run it I map the Volume like bellow (at least I think that’s what I’m doing)
docker run -d -p 221:22 -v my_vol:/opt/my_vol --name my_dock_r1 my_dock
When I log in to the Image I can see the /opt/my_vol directory just fine and when I do the following commands everything looks ok
docker volume ls
DRIVER VOLUME NAME
local my_vol
docker volume inspect my_vol
[
{
"Driver": "local",
"Labels": null,
"Mountpoint": "/mnt/sda1/var/lib/docker/volumes/my_vol/_data",
"Name": "my_vol",
"Options": null,
"Scope": "local"
}
]
Which I’m happy with (or not happy with because it could be wrong) but the Mountpoint doesn’t make sense to me.
I thought it mounts it to my file system (Windows 10) and there is no such directory. In that case does it mount it in a place I can find it on the file system or is my understanding of this wrong?
I’m using the VirtualBox version of Docker, I’m not sure if that makes any difference
2
Answers
It seems that the error is due to it being the VirtualBox Docker (Docker Container it's called).
In order for the Volumes to be Mounted they have to be added to the VirtualBox settings. From the VirtualBox GUI
Settings
Shared Folders
Add Folder (little Folder with Plus symbol on the far Right)
Add the folder in the prompt
Folder Path : C:/Some/Directory
Folder Name : c/Some/Directory
Auto-mount : Checked
Make Permanent : Checked
then Restart the VirtualBox and after that the following command works
If you need to directly access the content of a volume from the host, you must use bind mount syntax
The
MountPoint
you show in thedocker volume inspect
command is an internal implementation detail. On non-Linux hosts (with Linux containers) it’s hidden inside a virtual machine; even on native-Linux hosts directly accessing any of the filesystem content in/var/lib/docker
is not a best practice.The most visible impact of putting
VOLUME
in your Dockerfile is that it will prevent any followingRUN
commands from modifying that directory tree. You don’t need it to mount content into the container and I’d highly recommend just deleting it (unless you’re 100% clear on what it does and why you want it).(Remember that anyone who has your image can trivially extract the ssh host keys and start their own ssh daemon impersonating your server. There are several significant security issues trying to run an sshd in Docker, including this, and I’d suggest avoiding it if at all possible.)