Mounting Docker Guest Filesystems on the Host

With the development of Docker, deploying web applications with tons of dependencies has been greatly simplified. 

What previously would be a day-long process installing the various application components (e.g. database, web server, OS, etc) could now be simplifed to minutes.

For myself, I typically have a complete development environment inside Docker so that if I ever need to switch machines, I can simply export / import.

In the past, I would do web development and modify files inside the container using command line utilities such as vi. This was no problem, as I could open a shell inside the container directly.

However, I recently started development using graphical editors (such as VSCODE) on the host, which required the mounting of docker container filesystem on the host.

I was a bit surprised that there was not a really straightforward way to do this. 

There is a way to mount host filesystem onto the container (guest) but no vice-versa.

With that, here's the way I was able to do so using samba.

Installing Samba into a Docker container (the example here is using Rocky 9 Docker GUEST running on MacOS HOST)

In this example, we have following Docker image:

REPOSITORY              TAG       IMAGE ID  CREATED         SIZE
rocky                   9         xxxxxxx   3 weeks ago     28.2GB

 

  • Create a new docker container from this image using the following command, This container will have the ports exposed needed for SMB/HTTP/HTTPS/SSH and Oracle services:
    • $ docker container run -d -it -p 22:22 -p 80:80 -p 443:443 -p 443:443 -p 1521:1521 -p 137:137 -p 138:139 -p 139:139 -p 445:445 --name no-reservations rocky:9 /bin/bash
  • Start up the container (my container name is called no-reservations)
    • $ docker container start no-reservations
  • Open a root shell in the docker
    • $ docker exec -it no-reservations /bin/bash

Note: The following commands are run inside the docker container:

  • inside the docker containerroot shell, install samba
    • # yum install samba samba-common -y
  • edit /etc/samba/smb.conf Go to the bottom of the file and add the directory of the container which want to share back to the host:
    • [htdocs]
         path = /usr/local/apache2/htdocs
         writable = yes
         browseable = yes
         guest ok = no
         create mask = 0755
  • The above configuration creates a cifs share of the container /usr/local/apache2/htdocs directory with a share name of htdocs
  • Create user a svc_client which be the owner of filesystem to be shared
    • # useradd svc_client
  • Change the ownership of the filesystem to be exported to svc_client
    • chown -R svc_client /usr/local/apache2/htdocs
  • Set a samba password for this user in the smb user database
    • smbpass -a svc_client
  • Start Samba services. Normally this is done using systemctl but systemd is typically not included under docker, so we'll start them manually.
    • # /usr/sbin/nmbd -D
    • # /usr/sbin/smbd -D

At this point, you should be done with the setup of Samba under the container:

If you are on windows:

You should now be able to map the container share on the host by going to Map Network Drive->\\127.0.0.1\htdocs

If you are on a Mac:

MacOS does not seem to allow drive mounts from 127.0.0.1, so you will need to create another loopback interface. Run the following command on the host.

$ sudo ifconfig lo0 127.0.0.2 alias up

You can them mount the share by Go->Connect to Server->smb://127.0.0.2/htdocs

In both cases, you will prompted for a login:

Enter svc_client and the password you set using the smbpasswd command;