The container management system Docker is quite useful for running services or applications in a controlled, isolated environment. In a lot of situations it is also useful to have directories inside a docker container mapped to a local folder on the host system.
Especially in a development environment, where this setup allows you to modify source files (e.g. for a web application) in the host-volume directly in your IDE, and have the running docker service find those updated files immediately without the need for a deployment.
Although Docker containers usually run Linux binaries, there is also the “Docker Desktop” suite for Windows10 which allows you to execute Linux docker containers on a Windows system.
How does this work?
To run the Linux containers, Docker Desktop uses a virtual machine running a real Linux system. To be more precise, Docker Desktop requires the Windows10 virtualization feature “Hyper-V” to be active.
Any running Linux container is then executed inside the virtual machine. You can also view this VM by opening up the Hyper-V Manager application an you see the “MobyLinuxVM” created by Docker Desktop.
Mounted Host Volumes however are added to the virtual machine via a SMB/CIFS shared folder (like a network drive). In order to provide such a Host Volume you first need to “activate” this share on the Docker Desktop settings:
This will expose the selected drives as network shares on your computer, with the privileges of your current user.
So if you have any security concerns with this, you should not use this Host Volumes feature!
Example usage
To show you a simple example, we will start an Apache Httpd webserver and make it deliver some content we have in a local folder:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
simon@WorkPC MINGW64 ~ $ cd /d/Docker simon@WorkPC MINGW64 /d/Docker $ mkdir webserver-example simon@WorkPC MINGW64 /d/Docker $ echo "<h1>Hello World" > webserver-example/index.html simon@WorkPC MINGW64 /d/Docker $ docker run --rm -v d:/Docker/webserver-example:/usr/local/apache2/htdocs -p 80:80 httpd:2.4 Unable to find image 'httpd:2.4' locally 2.4: Pulling from library/httpd 27833a3ba0a5: Pull complete 7df2f4a2bf95: Pull complete bbda6f884d14: Pull complete 4d3dcf503f89: Pull complete b2f11da8a23e: Pull complete Digest: sha256:b4096b744d92d1825a36b3ace61ef4caa2ba57d0307b985cace4621139c285f7 Status: Downloaded newer image for httpd:2.4 AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message [Fri Apr 26 11:15:05.091604 2019] [mpm_event:notice] [pid 1:tid 140371628560448] AH00489: Apache/2.4.39 (Unix) configured -- resuming normal operations [Fri Apr 26 11:15:05.091718 2019] [core:notice] [pid 1:tid 140371628560448] AH00094: Command line: 'httpd -D FOREGROUND' 172.17.0.1 - - [26/Apr/2019:11:15:14 +0000] "GET / HTTP/1.1" 200 16 |
Now you can open you web browser at http://localhost and see the text “Hello World”. Everytime you edit the created file webserver-example/index.html the change will be directly reflected in the browser after refreshing the page.
Troubleshooting
As the Docker Desktop system relies on different parts of your system (SMB network shares, Hyper-V virtual machine, virtual network drives) it is quite prone to some errors.
Problem 1: The docker system can’t connect to the internet any more, and also ports can’t be mapped any more
This seems to happen, if you restart your Windows system with the “Fast Startup” option enabled, but the MobyLinuxVM virtual machine is still running. Apparently then Windows stores the VM state and restores it (incompletely) after the start.
In order to fix this, you have multiple options:
- restart the Docker Desktop service after each restart (right-klick on the icon, and then select “Restart …”)
- disable “Fast Startup” in the control panel of you windows machine
- stop the Docker Desktop service before shutting down the computer
Problem 2: The host folder is not in sync with the Volume
Let’s say you have created a docker container with a volume mounted to a folder on the host system. But inside the docker container you can no longer see the files on your host, and also files created/edited inside the container in the volume are no longer shown on the host machine.
This can sometimes happen, if the CIFS connection between your host and the MobyLinuxVM breaks.
- you changed your username of password
- you changed your computer name
- you changed something in the “network sharing” settings of the Windows system
If the problem persists event after a reboot of your machine, you can try to reset the credentials (that are used to establish the connection).
In the same settings dialog of Docker Desktop where you configure the Shared Drives, there is also a clickable Link “Reset credentials”. This will ask for your network login again, and store the new access credentials for the VM. You probably have to enable the Shared Drives again after clicking this.
If this does not help, you have finally the option to reset the Docker Desktop system to factory defaults. This includes wiping all your images and containers, so you should backup all your data and configuration first.
As the reset also removes and recreates the MobyLinuxVM, this helped me to recreate the Shared Drives with the correct connection, after the other options failed.